Hi folks!
I am beginning an intermediate c++ class. For this exercise, I am writing some code that reads a text file line by line, performs a calculation, then outputs the first string on the line and the result of the calculation based off the number at the end of the line.
Specifically, on each line I want to:
- Extract the first string
- Ignore the second string
- Read in the numerical value at the end of the line for a calculation.
The only way I was able to read in a line of with ifstream was the simple method of using a while loop then telling the ifstream to assign the values to corresponding variables.
The input file looks like this:
One JunkString 125
Two MoJunkString 171
Three IgnoreStringJunk 255
Four BlahBlah 252
|
I want to only take the first string on each line and the value at the end.
I think there is a solution using a while(getline(myifStreamFile, stringName)) and myifStreamFile.ignore(256,' '); but I cannot find any additional information on how to "ignore" the string after the first white space and accomplish the thirdobjective.
In the end my output file would look like this:
One 49062.50
Two 91816.74
Three 204178.50
|
I was able to achieve this through myifStreamFile >> firstName >> lastName >> radius but then I thought this wasnt so elegant or viable when the files get more sophisticated (longer second names). basically i am streaming in data knowing that there are two strings and then magically "losing" the lastName information after the function is called. I'm not even sure if that's an acceptable practice in programming?
I'm not sure about how, or even if, splicing the line would be a better solution. I also have found some answers involving istreamstring from <sstream> but thats not anything we have learned about. I am only able to use these preprocessor directives.
You can find my solution regarding this dilemma in my definition where it says
" bool getData(ifstream &inFile "...below main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<string>
using namespace std;
bool getData(ifstream &inFile, string &firstName, string lastName, double &radius);
double calcArea(const double &radius);
void writeResults(ofstream &outFile, const string &firstName, const double &area);
double const PI = 3.14;
int main()
{
ifstream inFile;
ofstream outFile;
string firstName, lastName;
double radius = 0, result = 0;
inFile.open("inputdata.txt");
if (!inFile)
{
cerr << "inputdata.txt" << " is missing!";
exit(1);
}
//else //stubb
// cout << "good!" << endl;//stub
inFile.close();
inFile.open("outputdata.txt");
if (!inFile)
{
cerr << "outputdata.txt" << " is missing!";
exit(1);
}
//else //stubb
// cout << "good!" << endl;//stub
inFile.close();
inFile.open("inputdata.txt");
outFile.open("outputdata.txt");
while (getData(inFile, firstName, lastName, radius))
{
result = calcArea(radius);
writeResults(outFile, firstName, result);
}
inFile.close();
outFile.close();
}
//This is the getData function
//Task: to stream in text file data and assign to variables
// and also drops lastName
bool getData(ifstream &inFile, string &firstName, string lastName, double &radius)
{
if (inFile >> firstName >> lastName >> radius)
{
//cout << "reading..." << endl; // stub
return true;
}
else
{
//cout << "end of stream!" << endl; //stub
return false;
}
}
//This is the calcArea function
//Task: to calculate area of circle
double calcArea(const double &radius)
{
cout << "calculating... ";
double area;
area = PI * radius * radius;
return area;
}
//This is the writeResults function
//Task: to write line to outputfile.txt
void writeResults(ofstream &outFile, const string &firstName, const double &area)
{
cout << "writing..." << endl; //stub
outFile << left << setw(10) << firstName << right << setw(10) << fixed << setprecision(2) << area << endl;
}
|
Thank you for your help in advance!