Reputation: 43
I'm reading in values from a text file and printing the to screen as strings. The idea is to read in each individual string and print them to screen along with the running average of strings read in printed next to it. i have this for my string to float declaration
int main()
{
string inputfile, intstring;
float counter;
counter = 0;
float average;
average = 0;
float stringconv = stof(intstring);
cout << "Enter the name of a file to open\n";
cin >> inputfile;
ifstream inFile;
inFile.open(inputfile.c_str());
and later on to calculate the average
while (!inFile.eof())
{
getline(inFile, intstring, ' ');
cout << intstring <<","<<average<< endl;
//increments counter to keep average output correct
counter = counter +1;
//goes to next line at each space encountered in text file
average = (counter + stringconv) /2;
}
I've included this just in case my issue lies there. Can anyone tell me how to properly declare my conversion? and here is a full version which compiles
#include <math.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string inputfile, intstring;
float counter;
counter = 0;
float average;
average = 0;
float dividor;
dividor = 1;
cout << "Enter the name of a file to open\n";
cin >> inputfile;
ifstream inFile;
inFile.open(inputfile.c_str());
if (!inFile)
{
cout << "Error opening file " << inputfile << endl;
char stopchar;
cin >> stopchar;
return -1;
}
while (!inFile.eof())
{
//goes to next line at each space encountered in text file
getline(inFile, intstring, ' ');
cout << intstring <<","<<average<< endl;
float stringconv;
stringconv = stof(intstring);
average = (counter + stringconv)/dividor ;
dividor = dividor +1;
//increments counter to keep average output correct
}
inFile.close();
char stopchar;
cin >> stopchar;
}
Upvotes: 0
Views: 235
Reputation: 99094
Here:
string inputfile, intstring;
...
float stringconv = stof(intstring);
You can't do that. I mean, you can, but it doesn't do what you think it does. You think you're creating a macro or function or something, so that you can change intstring
and then stringconv
will automatically change. But what you're actually doing is converting the uninitialized string into an integer once, and never changing it again. You must do the conversion inside the read loop.
EDIT: If you're not required to use stof()
, then you can save yourself a lot of headaches by using the stream input operator:
float number;
inFile >> number; // this is the basic form
while(inFile >> number) // this is how to do it as a loop
...
Upvotes: 1
Reputation: 75062
In C++, float stringconv = stof(intstring);
won't meen automatic conversion like assign
in Verilog.
Call stof()
each time you need to convert.
Try this:
while (!inFile.eof())
{
getline(inFile, intstring, ' ');
cout << intstring <<","<<average<< endl;
//increments counter to keep average output correct
counter = counter +1;
//goes to next line at each space encountered in text file
stringconv = stof(intstring); // add this here
average = (counter + stringconv) /2;
}
Upvotes: 0