1

I am currently learning C++. I am to read in data from a file into the table below. I am able to read the data in but in the table it does not like this:

StdID A1 A2 A3


030302 9 5 6

but rather as

030302

9

5

6

etc.

How do I format this correctly into the table? I tried setw but it didn't solve the problem.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
  int StdID=0, A1=0, A2=0, A3=0;

  ifstream fin;
  fin.open("data.txt");
  if(!fin)
    cout << "The file does not exist.";
  else{
    cout << "Std-ID     A1     A2     A3     Min     Max     Avg\n";
    cout << " ---------------------------------------------------\n";
    while (!fin.eof())
      {
        fin >> StdID >> A1 >> A2 >> A3;
        cout << setw(10) << StdID << endl;
        cout << setw(10) << A1 << endl;
        cout << setw(10) << A2 << endl;
        cout << setw(10) << A3 << endl;
      }
  }



    return 0;
}
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
JOHNSMITH8338
  • 85
  • 2
  • 7

3 Answers3

2

Each time you are displaying a variable, you are displaying a new line as well (by using each of the << endl;).

This should work:

while (!fin.eof())
  {
    fin >> StdID >> A1 >> A2 >> A3;
    cout << setw(10) << StdID << setw(10) << A1 << setw(10) << A2 << setw(10) << A3 << endl;

    // You may also break it down like this:
    //cout << setw(10) << StdID;
    //cout << setw(10) << A1;
    //cout << setw(10) << A2;
    //cout << setw(10) << A3 << endl; // one endl on each iteration
  }

Advice: You may need to learn Why is iostream::eof inside a loop condition considered wrong. Better use this condition instead:

while (fin >> StdID >> A1 >> A2 >> A3)

Edit:

The Tab key \t would still work with strings, similar way to this:

cout << "Std-ID\t\t\tA1\t\t\tA2\t\t\tA3\t\t\tMin\t\t\tMax\t\t\tAvg\n";

Also you better use if (fin.is_open()) to check if the file was opened successfully.

Community
  • 1
  • 1
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • If I wanted to line up the StdID and A1 above since it isn't line up perfectly would I use \t? But I don't think it would work since it's in a string? How would you use proceed? And I'm going to give your link a read, thanks. – JOHNSMITH8338 Sep 04 '16 at 21:46
  • See edited. You would not need the `\t` though. Just do a simple trial and error with `setw()`. Also see the proper wait of checking if `fin` opened the file. – Khalil Khalaf Sep 04 '16 at 21:57
1

You are adding a newline on each print with << endl. Remove that from all but the last one and it should work.

cout << setw(10) << StdID;
cout << setw(10) << A1;
cout << setw(10) << A2;
cout << setw(10) << A3 << endl;

Or you could combine them all onto one line.

Also, you should avoid using namespace std. See this question for details.

Community
  • 1
  • 1
js441
  • 1,134
  • 8
  • 16
  • Thank you. This worked. I'm trying to teach myself C++ as I know a different language. When I didn't use setw it also didn't work. So using setw it helped create the table but then why is there no need for a new line after? I'm just trying to understand the concept. – JOHNSMITH8338 Sep 04 '16 at 21:43
  • @JOHNSMITH8338 Good, welcome to C++. endl creates a new line (like pressing the enter key). You don't want them inside the table, only at the end of the rows. – js441 Sep 04 '16 at 21:45
  • @JOHNSMITH8338 Also see my edit for a warning about namespace std. – js441 Sep 04 '16 at 21:46
0

At the end of each of your cout statements, you write this:

cout /*...*/ << endl; //<-- This creates a new line

Every time you write that, you are creating a new line. Instead, only keep that on the last line, or put all the statements on the same one. Also:

while (!fin.eof())
  {
    fin >> StdID >> A1 >> A2 >> A3;
    cout << setw(10) << StdID << endl;
    cout << setw(10) << A1 << endl;
    cout << setw(10) << A2 << endl;
    cout << setw(10) << A3 << endl;
  }

can be simplified to:

while (fin >> StdID >> A1 >> A2 >> A3)
  {
    cout << setw(10) << StdID << endl;
    cout << setw(10) << A1 << endl;
    cout << setw(10) << A2 << endl;
    cout << setw(10) << A3 << endl;
  }
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88