HOW TO DISPLAY THREE PER LINE?

The output section only print one state per line. How can i print 3 states per line?
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
  getline(cin, filename);
	fin.open(filename, ios::binary);

//check file
	if (fin.fail())
	{
		cout << "ERROR: Cannot open the file..." << endl;
		exit(0);
	}

	else

//read
		fin.read((char*)&temp, struct_size);
	while (!fin.eof())
	{
		tempo.statename[count] = temp.name;
		tempo.admission[count] = temp.order;
		tempo.density[count] = temp.population / temp.area;
		count++;
		fin.read((char*)&temp, struct_size);
	}
	

//output

	for (int y = 0; y < count; y++)
	{
			cout << tempo.statename[y] <<" "<< tempo.admission[y] <<"  " << tempo.density[y] << endl;
	}
		cout << endl;

	cout << endl;

	return 0;
}
Last edited on
closed account (48T7M4Gy)
pseudocode:

for y = 0 up to (size of array - 3), step 3 at a time
    cout << y[i].stuff << y[i+1] .stuff<< y[i+2].stuff << newline

Last edited on
Hello john99999999999999999999,

The output section only print one state per line. How can i print 3 states per line?

You have mostly answered your own question. If you want to print three per line then tell the program to print three per line. kemont's suggestion is a good start or you might try combining his answer with this:

1
2
3
cout << tempo.statename[y] <<" "<< tempo.admission[y] <<"  " << tempo.density[y];
cout << tempo.statename[y + 1] <<" "<< tempo.admission[y + 1] <<"  " << tempo.density[y + 1];
cout << tempo.statename[y + 2] <<" "<< tempo.admission[y + 2] <<"  " << tempo.density[y + 2] << endl;


or include the header file iomanip and use setw() before the variables to set the spacing of the output:

1
2
3
cout << setw(10) << tempo.statename[y] << setw(10) << tempo.admission[y] << setw(10) << tempo.density[y]; // the " " will not be needed when the size of 10 is set correctly.
cout << tempo.statename[y + 1] <<" "<< tempo.admission[y + 1] <<"  " << tempo.density[y + 1]; // use the same setw() in this line.
cout << tempo.statename[y + 2] <<" "<< tempo.admission[y + 2] <<"  " << tempo.density[y + 2] << endl; // use the same setw() in this line. Use endl only on the last line to keep everything on one line. 


Use these links if you need help with setw().

http://www.cplusplus.com/reference/iomanip/
http://www.cplusplus.com/reference/iomanip/setw/
http://www.cplusplus.com/reference/iomanip/setfill/

Hope that helps,

Andy
Topic archived. No new replies allowed.