parseString funtion (stream I/O)
In the code provided below, I have written a program (word for word) from my text book. This program reads the text from a file named Accounts.txt and parses the string input to a different format (shown below as well).
My only problem is that when the program executes, I receive the unexpected error "Error, parsing string" (also shown below). After I used my debugger, I stepped into the function parseString() and put a watch on all the values. The value balance was the only one not receiving the correct input (something really wacky....like 5.1233242523532e-308....should be 56.6).
I need to know why I am getting this error.
The text book I am currently using has a few typo's here and there, but I'm still new to C++ and do not know if I could pick any out of the displayed code.
If anyone has any questions about the code or my question, please ask.
thanks in advance!
// parseAccountInfo - read a passed buffer as if it were
// an actual file - read the following
// name, account balance
// return true if all worked well
bool parseString(char* pString, char* pName, int arraySize,
long& accountNum, double& balance)
//associate an istrstream object with the input
//read up to the comma seperator
inp.getline(pName, arraySize, ',');
//now the account number
inp >> accountNum;
//balance not receiving correct value...
//and the balance
inp >> balance;
//return the error status
int main(int nNumberOfArgs, char* pszArgs)
//get a file stream
ifstream* pFileStream = new ifstream("Accounts.txt");
cout << "Can't open Accounts.txt" << endl;
//read a line out of file, parse it and display results
//add a divider
cout << "========================" << endl;
// read a buffer
//parse the individual fields
//using debugger, I step into function below
bool result = parseString(buffer, name , 80,
//output the results
//program goes into if statement below....result returned false
cout << buffer << "\n";
if (result == false)
cout << "Error parsing string\n";
cout << "name = " << name << ","
<< "account = " << accountNum << ", "
<< "balance = " << balance << endl;
//put the fields back together in a different
//order (inserting the 'ends' makes sure the
//buffer is null terminated
out << name << ", "
<< balance << " "
<< accountNum << ends;
//output the result - istringstream also works with
//the string class but I have been staying with
//character arrays until the discussion of the templates
string oString = out.str();
cout << oString << "\n" << endl;
the text file i'm using is called "Accounts.txt" and is located in the same directory as executable.
File format for Accounts.txt
Chester, 12345, 56.60,\n
what the output statement looks like now:
what should happen:
Chester, 12345, 56.60,\n
Error parsing string
Chester, 12345, 56.60
name = Chester, account = 12345, balance = 56.6
Chester, 56.6, 12345