Hi all,
My cosin give me your site today when I called him, he kind of had the same problem. I have a file and i'm trying to read from this file as follow ID, first, last name and from the numbers (buyer.type) two things type, and how many pound, so I can use the number to calculate how many the costumer bought and the total of his purchase through the time .my code below read the id , first and last then reads trash numbers like 0012B180 with no clue what is it?I don't know why I'm getting trash. I don't know if my algorithm right or not
help please


example from the file :

1 ROBERT DAVIS 1 12 5 10 3 6 1 5 4 2 1 10 4 1 0
2 BARBARA MILLER 3 10 4 9 5 1 3 1 4 3 1 3 1 1 4 10 2 1 1 14 3 3 5 9 3 14 5 14 0
3 MICHAEL WILSON 4 8 4 7 5 10 2 8 5 12 5 15 3 2 0
8 ELIZABETH MOORE 2 9 2 8 5 3 1 6 3 8 0
9 WILLIAM TAYLOR 4 8 5 8 2 2 4 5 1 6 3 9 2 13 3 13 3 13 0
10 JENNIFER ANDERSON 5 1 2 3 3 14 2 8 3 2 4 11 4 1 1 8 1 2 1 1 0
12 DAVID THOMAS 4 10 5 1 4 2 2 12 0

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int size = 200;	
struct costumer
{
	int ID;
	string first, last;
	int type[10];
};

bool getdata(fstream& fin, costumer & buyer);
int fillarray(fstream& fin, costumer buyer[]);
void Print(costumer buyer[], int count, ofstream & fout);

int main()
{
	fstream fin;
	ofstream fout;
	costumer buyer[size];
	int count;

	fin.open("coffee.txt"); 
	if (fin.fail())
	{
		cout << "There is no input file." << endl;
		exit(1); 
	}
	fout.open("output.txt");
	if (fout.fail())
	{
		cout << "There is no output file." << endl;
		exit(2);
	}

	count = fillarray(fin, buyer);
	Print(buyer, count, fout);
	fout.close();
	fin.close();
	
return 0;
}

bool getdata(fstream& fin, costumer & buyer)
{ 
	fin >> buyer.ID>> buyer.first >> buyer.last;
	while (buyer.type)
	{
	int T;//type.
	int P;//pound.
	while (T > 0)
		{
fin>>T>>P;
 // I want to seperate buyer.type to two things which is type of item(5 items) and how many pounds. 
// likewise : 2 11 5 8 3 2 1 3 5 2 5 13 4 9 2 4 4 7 4 5 3 14 0
 //		    : 1 12 5 10 3 6 1 5 4 2 1 10 4 1 0

	
	if (!fin.fail())
		return true;
	else 
		return false;
}
int fillarray(fstream& fin, costumer buyer[])
	{
		int count = 0; 
		while (count < size && getdata(fin,buyer[count]))
			count++;
		cout <<"the program read "<<count <<" students"<<endl;
		return count;
	}
void Print(costumer buyer[], int count, ofstream & fout)
{
	for( int n = 0; n < count; n++)
	{
		fout << buyer[n].ID<<'	'<<buyer[n].first<<'	'<<buyer[n].last<<endl;
	
	
		int cont = 0;
		for (int x=0;x<10;x++)
		{
			fout <<'	'<<buyer[x].type;
			cont++;
		}
	}
}

Line 53: T is uninitialized so its value is any old random value, including 0. You need to initialize it to some known value on line 51. Using a while statement to read integers here is not a good idea because the >> operator does not stop at end-of-line. Better solution is to call getline() to read the rest of the line into a single string then parse the string to extract the numbers. You can use stringstream to do that very easily.

Line 53: T is uninitialized so its value is any old random value, including 0. You need to initialize it to some known value on line 51. Using a while statement to read integers here is not a good idea because the >> operator does not stop at end-of-line. Better solution is to call getline() to read the rest of the line into a single string then parse the string to extract the numbers. You can use stringstream to do that very easily.

thanks Ancient Dragon
i did this b'cas i want to make two value from the numbers. I think I have trouble understading algorithm in your idea.
could you please support your thought with some code.

Scroll down to the 6th post here

I saw this code but in this case i can't b'cas I'm using the numbers as intgers . I used string for first and last name but i want my code to read till get to 0 (so I used a while loop)then move to the next customer.(which were the T and P)
the other thing is when I'm done from the array I have to split them to two things type and pound.
sorry I think I didn't explain my problem right.

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.