extra output from a function

I tossed this code together for debugging reasons only so the format is not great but the problem I am having is with the function finalOutput(). I have coded this function to output 6 pieces of data from an array.

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

struct marketType
	{
	public:
		string market;
		string name;
		string street;
		string city;
		string state;
		string country;
 };

void readMarkets (ofstream &outfile,ifstream &infile, marketType marketArray[]);
void finalOutput(ofstream &outfile, marketType marketArray[]);
void openFile2(ofstream &outfile,ifstream &infile);

//======================================================================
int main()
{
	 ifstream infile;  // set a name for ifstream
	 ofstream outfile; // set a name for ofstream

 marketType MyMarket; // set a name for MarketType
 marketType marketArray[10]; // make an array to hold the market information

outfile.open ("report.txt"); //open the final output file

//============== FUNCTIONS ===================

openFile2(outfile, infile);

readMarkets (outfile, infile, marketArray);

finalOutput(outfile, marketArray );
//========= END FUNCTIONS ====================

 system("pause");
 return 0;
}
//=======================================================================
void readMarkets (ofstream &outfile,ifstream &infile, marketType marketArray[]) //==============
{
 int i = 0;
	  while(!infile.eof() && i < 10)
		 {
				getline(infile,marketArray[i].market, '\t');
				getline(infile,marketArray[i].name, '\t');
				getline(infile,marketArray[i].street, '\t');
				getline(infile,marketArray[i].city, '\t');
				getline(infile,marketArray[i].state, '\t');
				getline(infile,marketArray[i].country, '\t');
				 
				i++;
		 }
	infile.close();
}
 

void finalOutput(ofstream &outfile, marketType marketArray[])
{
	outfile << marketArray[0].market <<endl;
	outfile << marketArray[0].name <<endl;
	outfile << marketArray[0].street <<endl;
	outfile << marketArray[0].city  <<endl;
	outfile << marketArray[0].state <<endl;
	outfile << marketArray[0].country <<endl;
}

void openFile2(ofstream &outfile,ifstream &infile) //=================
{  
 infile.open("markets.txt");
 if(!infile)
  {
  cout << "Unable to open input book file!" << endl ;
  system ("pause");
  }
}


When it runs it outputs 7 pieces of data from an array.
it is acting like it is reading the next data "block" in the linear memory. Like

1
2
3
4
5
6
7
8
9
10
void finalOutput(ofstream &outfile, marketType marketArray[])
{
	outfile << marketArray[0].market <<endl;
	outfile << marketArray[0].name <<endl;
	outfile << marketArray[0].street <<endl;
	outfile << marketArray[0].city  <<endl;
	outfile << marketArray[0].state <<endl;
	outfile << marketArray[0].country <<endl;
	outfile << marketArray[1].market <<endl; // <---- its reading this even though I don't have it coded there.
}


In the original code I have it looping and it outputs 1 extra "block" of data on every iteration. so on the second iteration it starts with the information that should be in marketArray[1].name (where it left off) and the third starts with the information that should be in marketArray[2].street.
Any ideas what might be going on?

here is the market.txt file that is being read. its TAB delim

1
2
3
4
5
6
market	name	street-address	city	state	country
amazon	Amazon Corporation	345 Bezo Ave	Sioux City	IA	USA
alibris	Alibris LLC	5332 Nottingham St	London	Coventry	Great Britian
half	Half Corporation	87 North Cresent Blvd	Los Angeles	CA	USA
ebay	ebay Corporation	33 Whitman Dr	Meg	IL	USA
abebooks	AbeBooks	345 Homebase Ct	Marvin	TX	USA
I think the problem is there is no tab after the country in the infile, just a newline.

The program is not printing out the next field but the value it gets for marketArray[0].country which is in fact "USA\nalibris".
Last edited on
that is what it was. What a mean trick for my instructor to play on me. Thank you very much. I would have been here fretting over that for weeks an may have never figured it out. Thanks again :]
What a mean trick for my instructor to play on me.

I think that's a bit strong. The file looks quite typical and normal to me. If you save a tab-separated file from a spreadsheet it would look much like this.

It's all part of the learning process, naturally you don't know everything at the start, that's what the programming exercises are for.
The file is ok.

Just need to possibly change getline(infile,marketArray[i].country, '\t');

to getline(infile,marketArray[i].country, '\n');
Topic archived. No new replies allowed.