Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Saving to file, need help!

I am new to c++, and i am writing a rows and columns program. And now i need to be able to save the values to file and then retrieve that file. i am lost, as i know this is a very basic program. I just want to get this accomplished so any help would be much apreciated. thanks!!

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>

#include "CinReader.h"

using namespace std;

CinReader reader;

struct myCell
{
string currentValueOf;
string previousValueOf;
int myRow;
int myColumn;
myCell* referencesTo[100];
int sizeOfRef;
int theRefsUsed;
};

struct myRow
{
myCell* cells;
};

myRow* rows;

int sizeOfRow = 5;
int countOfRows = 5;
void cellContents (myCell& aCell);
void personalMainMenu ();
void editCell (myCell& aCell);
void print (myRow* rows, int size);
void print (myCell* myRow, int size);
void print (myCell aCell);
int main ()
{
rows = new myRow[countOfRows];
for (int j=0; j< countOfRows; j++)
{
rows[j].cells = new myCell[sizeOfRow];
for (int i=0; i< sizeOfRow; i++)
cellContents(rows[j].cells[i]);
}

personalMainMenu();

delete [] rows;

return 0;
}
void personalMainMenu ()
{
int myRow = 0;
int myCell = 0;
int usersChoice = 0;
bool quit = false;
do
{
print(rows, countOfRows);
cout << "[1] Edit your cell\n";
cout << "[2] Save your spreadsheet\n";
cout << "[3] Retrieve your spreadsheet\n";
cout << "[0] Exit your spreadsheet \n";
cout << "Enter your choice: \n\n";
usersChoice = reader.readInt(true, 0, 3);
switch (usersChoice)
{
case 0:
quit = true;
break;
case 1:
cout << "Please enter a row number of your choice (1 - " << countOfRows << "): ";
myRow = reader.readInt(true, 1, countOfRows);
cout << "Please enter a cell number of your choice (1 - " << sizeOfRow << "): ";
myCell = reader.readInt(true, 1, sizeOfRow);
editCell(rows[myRow-1].cells[myCell-1]);
break;
case 2:// save to file
break;

case 3:// retreive from file
break;

}
} while (!quit);
}
void editCell (myCell& aCell)
{
int myRow = 0;
int myCell = 0;
bool quit = false;
cout << "[1] Data cell\n";
cout << "[2] Calculated cell\n";
cout << "your choice: ";
switch (reader.readInt(true, 1, 2))
{
case 1:
aCell.previousValueOf = aCell.currentValueOf;
cout << "\n\nCurrent value is -> \"" << aCell.currentValueOf << "\"\n\n";
cout << "Enter new data for the cell ->\t";
aCell.currentValueOf = reader.readString();
for (int i=0; i<aCell.currentValueOf.length(); i++)
{
if (aCell.currentValueOf[i] == '\t')
aCell.currentValueOf[i] = ' ';
}
break;
case 2:
if (aCell.theRefsUsed < aCell.sizeOfRef)
{
do
{
cout << "Enter your row #: ";
myRow = reader.readInt(true, 1, countOfRows);
cout << "Enter your cell #: ";
myCell = reader.readInt(true, 1, sizeOfRow);
aCell.referencesTo[aCell.theRefsUsed] = &rows[myRow-1].cells[myCell-1];
aCell.theRefsUsed++;
cout << "Would you like to edit another Cell (y/n)? ";
if (toupper(reader.readChar("YyNn")) == 'N')
quit = true;
} while (!quit && aCell.theRefsUsed < aCell.sizeOfRef);
}
break;
}

}
void print (myRow* rows, int size)
{
for (int i=0; i < size; i++)
print(rows[i].cells, sizeOfRow);
}
void print (myCell* myRow, int size)
{
for (int i=0; i<size; i++)
print(myRow[i]);
cout << "\n\n";
}
void print (myCell aCell)
{
stringstream spreadSheet;
int theResultOf = 0;
if (aCell.theRefsUsed > 0)
{
for (int i=0; i<aCell.theRefsUsed; i++)
theResultOf += atoi(aCell.referencesTo[i]->currentValueOf.c_str());
spreadSheet << theResultOf;
aCell.currentValueOf = spreadSheet.str();
}
string valueOfYourOutput = aCell.currentValueOf;
if (valueOfYourOutput.length() >= 10)
valueOfYourOutput = valueOfYourOutput.substr(0, 10);
else
{
int whatYaGotLeftOver = 10 - valueOfYourOutput.length();
stringstream strungOut;
int cushion = whatYaGotLeftOver/2;
for (int i=0; i < cushion; i++)
strungOut << " ";
if (whatYaGotLeftOver%2 == 1)
strungOut << " ";
strungOut << valueOfYourOutput;
for (int i = 0; i < cushion; i++)
strungOut << " ";
valueOfYourOutput = strungOut.str();
}
cout << "+ " << setw(10) << valueOfYourOutput << " +";
}

void cellContents (myCell& aCell)
{
aCell.currentValueOf = "";
aCell.previousValueOf = "";
aCell.myRow = 0;
aCell.myColumn = 0;
aCell.sizeOfRef = 100;
aCell.theRefsUsed = 0;
for (int i=0; i<aCell.sizeOfRef; i++)
aCell.referencesTo[i] = NULL;
}
[5554 byte] By [aLittleLost] at [2007-11-11 9:57:31]