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!
parseString funtion
Code:
// parseAccountInfo - read a passed buffer as if it were
// an actual file - read the following
// format:
// 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
//character string
istringstream inp(pString);
//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
return !inp.fail();
}
....now main
Code:
int main(int nNumberOfArgs, char* pszArgs)
{
//get a file stream
ifstream* pFileStream = new ifstream("Accounts.txt");
if(!pFileStream->good())
{
cout << "Can't open Accounts.txt" << endl;
system("PAUSE");
return 0;
}
//read a line out of file, parse it and display results
for(;;)
{
//add a divider
cout << "========================" << endl;
// read a buffer
char buffer[256];
pFileStream->getline(buffer, 256);
if(pFileStream->fail())
{
break;
}
//parse the individual fields
char name[80];
long accountNum;
double balance;
//using debugger, I step into function below
bool result = parseString(buffer, name , 80,
accountNum, balance);
//output the results
//program goes into if statement below....result returned false
cout << buffer << "\n";
if (result == false)
{
cout << "Error parsing string\n";
continue;
}
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
ostringstream out;
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;
}
system("PAUSE");
return 0;
}
the text file i'm using is called "Accounts.txt" and is located in the same directory as executable.
File format for Accounts.txt
Code:
Chester, 12345, 56.60,\n
what the output statement looks like now:
Code:
========================
Chester, 12345, 56.60,\n
Error parsing string
========================
what should happen:
Code:
========================
Chester, 12345, 56.60
name = Chester, account = 12345, balance = 56.6
Chester, 56.6, 12345
========================