I am a beginning c++ student and need to read in a text file as shown below:
Orvilles's Acres, 114.8 43801
Hoffman's Hills, 77.2 36229
Jiffy Quick Farm, 89.4 24812
Houston Bayou Texas Home Grown Popcorn Inc., 124.7 65687
The criteria is to read characters to comma then read the double and int and compute a bar chart based on the numbers. I am stuck currently on the problem of reading in to a comma and limiting characters to 29 or less. Below is a sample of code fragment I have tried without success:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char x=' ';
string Name=" ";
int count=0, Jars=0;
double Acres=0.0;
fstream In;
In.open("F:\\BonusProj.txt", ios::in);
if (!In)
{
cout<<"Error opening file";
return 1;
}
while(!In.eof())
{
In.get(x);
if (x != ',')
{
Name = Name + x;
count++;
}
if((x==',') || (count >= 29))
{
In>>Acres>>Jars;
cout<<Name<<endl;
Name=" ";
In.get(x);
count=0;
Jars=0;
Acres=0;
}
}
In.close();
cin.get();cin.get();
return 0;
}
I attempted to use getline then parse the array to strip out the comma, double and int but was having problems getting valid numbers assigned. If someone could steer me in the correct direction it would be of immense help.