elpichi
June 14th, 2008, 01:06 AM
i'm having a hard time finding the right way to convert from string to a float.
I'm making a program that makes a file that logs the profits of a person
the problem comes when i try to get the number which could be anywhere in the file except it has "The ending balance for the account is: $399.09" or any other float. i managed to make a loop that searches through the entire file for those exact words, but i get a string and i need that info in a float variable.
Any advice would be super helpful, thanks.
float balance=0;
string name, line, clean, number;
stringstream streamer;
string match = "The ending balance for the account is: ";
cout << setprecision(2);
//check if file exist and create a new file if there isn't any
ifstream inbudget;
inbudget.open("budget.dat", ios::ate);
if(inbudget.is_open())
{
while(inbudget)
{
int pos = -1;
getline(inbudget, line);
pos = line.find('$');
clean = line.substr(0, pos);
line = line.substr(pos+1);
if(clean == match)
number = line;
}
cout << number << endl;
streamer >> number;
inbudget.close();
}
while displaying the number before converting it shows exactly what i want, but somehow it turns into a 1.8 or 1.9 after streamer does its job.
I'm making a program that makes a file that logs the profits of a person
the problem comes when i try to get the number which could be anywhere in the file except it has "The ending balance for the account is: $399.09" or any other float. i managed to make a loop that searches through the entire file for those exact words, but i get a string and i need that info in a float variable.
Any advice would be super helpful, thanks.
float balance=0;
string name, line, clean, number;
stringstream streamer;
string match = "The ending balance for the account is: ";
cout << setprecision(2);
//check if file exist and create a new file if there isn't any
ifstream inbudget;
inbudget.open("budget.dat", ios::ate);
if(inbudget.is_open())
{
while(inbudget)
{
int pos = -1;
getline(inbudget, line);
pos = line.find('$');
clean = line.substr(0, pos);
line = line.substr(pos+1);
if(clean == match)
number = line;
}
cout << number << endl;
streamer >> number;
inbudget.close();
}
while displaying the number before converting it shows exactly what i want, but somehow it turns into a 1.8 or 1.9 after streamer does its job.