-1

(C++) I want to display the first part, which works fine, then the dollar sign on another line, then the price. Here's what I have:

string mystring;
ofstream oFile( "C:\\beer.txt" );
oFile << "Heineken#$7.99" << endl;

fstream File( "C:\\beer.txt" , ios :: in | ios :: out );
getline(File, mystring, '#');
stringstream mystringstream;
mystringstream.str(mystring);
cout << "The list contains: " << mystring << endl;

/////////////////////////////////////////////////////////////////
// Everything above this line works just fine
// now I want to display the dollar sign

string mystring2;
getline (File, mystring2, '7');
stringstream mystringstream2;
mystringstream2.str(mystring2);
char dollar = mystring.at(mystring2.length()-1);
cout << "The tender is: " << dollar << endl ;
// this does not work

///////////////////////////////////////////////////////////
//now, I want to display the price, in xx.xx format
//I've been looking for proper functions, but nothing works. Any help 
//is greatly appreciated.
JCoder
  • 189
  • 1
  • 3
  • 17
  • It doesn't work because you never defined `mydollar`, not sure exactly what you expect there. – Red Alert Mar 04 '15 at 23:51
  • You should close the output file before you read it. If you want to write and read, open it like your 2nd one, but before you write to it. – Thomas Matthews Mar 05 '15 at 00:43

1 Answers1

0

For the second part, this should display the amount:

string mystring2;
getline (File, mystring2);
std::cout << "Amount: " << mystring2 << endl ;

You have already read the part before the amount with your first getline call, so the next getline will read the rest of the string. Just print it out.

  • It doesn't display anything after Amount. and for some reason, the first part no longer display anything after "contains" – JCoder Mar 05 '15 at 00:02