I need to string names from a text file which the name will output no more than 29 characters and no commas. My output displays no commas, but i cant figure out how to count the characters and output no more than 29 characters in the name.
I think I just need to add a way to count the characters in the while loop.
Its the: VOID GETDATA(FSTREAM& IN, STRING& NAME, DOUBLE& ACRES, INT& JARS) funtion that i am have my problem.

Can somebody help me please....

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
#include <E:\\CS-1410 (Fall2009)\\Bonus Project\\Popcorn.h>


using namespace std;

int main()
{
	fstream InFile;
	string Name;
	int Jars;
	double Acres;
//print header
	PrintHeader();
//Open file to read
	InFile.open("E:\\CS-1410 (Fall2009)\\Bonus Project\\BonusProj.txt",ios::in);
	if (!InFile)
	{
		cout<<"Can't Open the input file.";
		return 1; 
	}
//continue if Target is a character
	while (!InFile.eof())
	{
//get data
	GetData(InFile, Name, Acres, Jars);
//print name
	//cout<<setw(29)<<Name;
//print bar chart
//	PrintStars(Jars/Acres);
//	cout<<endl;
	}
	InFile.close();
cin.get();
return 0;
}
void PrintHeader(void)
{
	cout<<setw(23)<<"Pop CoOp"<<endl;
	cout<<"Farm Name"<<setw(30)<<"Production"<<endl;
	cout<<setw(41)<<"Thousands of"<<endl;
	cout<<setw(47)<<"Pint Jars per Acre"<<endl;
	cout<<setw(53)<<"   1   2   3   4   5   6"<<endl;
	cout<<setw(52)<<"---|---|---|---|---|---"<<endl;
}

/*void PrintStars(double NumStars)
{
	if (NumStars>0)
	{
		cout<<'*'<<endl;


}

int Round(double x)
{
	return x=((Jars/Acres)/TickMark+.5)
}*/

void GetData(fstream& In, string& Name, double& Acres, int& Jars)
{
	char x=' ';
	int count=0;

	In.get(x);
	while (x < 29 || x != ',' )
	{
		Name = Name + x;			
		In.get(x);
	}

	if (x == ',' || x >= 29)		
		{			
			In>>Acres>>Jars;			
			cout<<Name<<endl;			
			Name=" ";			
			In.get(x);			
			count=0;			
			Jars=0;			
			Acres=0;		
		}

}

Recommended Answers

All 5 Replies

What are you going to do with the rest of the line if the Name is more than 29 characters? Is it supposed to ignore the rest of the Name characters (skip over the excess characters) and read in the remaining characters? If that is true, then after line 76 just read all remaining charcters up to the comma

while( x != ',' && In.get(x))
    ; // do nothing line here

Now the program is read to read the float and integer (line 79). Note that the if statement on line 77 is not needed.

Thank you for your help... I would make it end on a comma, but when I string the names, one name has more than 29 characters. On my output screen, there is a bar graph and that name wont fit next to it unless it outputs only 29 characters. I have to include this in my function code. So I still need to figure out how to count the characters to where it ends at the comma or only uses 29 characters. I've been messing with it for a while and done alot of reading and research, but cant figure it out.
Do you know how i could do this?
Thanks...

Why bother with get()? Just use getline(IN, Name, ',') then if Name is greater than 29 just chop it off at the 29th character

if( Name.length() > 29)
   Name = Name.substr(0,29);

You rock for helping me... I just want to thank you again! There needs to be another loop to count the characters because my txt file looks like this:

Orville'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
Jolly Good Plantation, 183.2 104570
Organically Grown Inc., 45.5 14683

I cant get the whole line because I need to assign acres to the first set of numbers and Jars to the last set of numbers on a line. So I make it stop at a comma or less than or equal to 29 characters, then I will have the numbers left over to assign Acres and Jars to create a graph for the function void PrintStars ( double NumStars) function and int Round ( double x ) function.

I'm not sure what your last post is trying to say. The char by char way to limit the name part of the line to 29 char after the full name has been received could be something like this:

string fullString = //whatever; 
string clippedString;
int len = fullString.length()
if(len > 29)
{
  //clip fullString after the first 29 char
  for(int index = 0; index < 29; ++index)
    clippedString += fullString[index];
}

though using substr() as demonstrated by Ancient Dragon is probably prefered when using STL strings. I forget whether you specifically have to assign some memory to clippedString before you call the += (concatenation operator) or not and I don't have a compiler here to check.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.