9

I am trying to print a text file out on screen using arrays, but I'm not sure why it does not appear the way it is in the text file.

The text file:

1 2 3 4
5 6 7 8

Displayed on the screen as follows after applying discard function:

1
2
3
4
5
6
7
8

The code:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

const int MAX_SIZE = 20;
const int TOTAL_AID = 4;

void discard_line(ifstream &in);
void print(int print[][4] , int size);

int main()
{
    //string evnt_id[MAX_SIZE]; //stores event id
    int athlete_id[MAX_SIZE][TOTAL_AID]; //stores columns for athelete id
    int total_records;
    char c; 
    ifstream reg;
    reg.open("C:\\result.txt");

    discard_line(reg);
    total_records = 0;

    while( !reg.eof() )
    {
        for (int i = 0; i < TOTAL_AID; i++)
        {
            reg >> athlete_id[total_records][i] ;//read aid coloumns
        }
        total_records++;
        reg.get(c);
    }

    reg.close();

    print(athlete_id, total_records);

    system("pause");
    return 0;
}

void discard_line(ifstream &in)
{
    char c;

    do
        in.get(c);
    while (c!='\n');
}

void print(int print[][4] , int size)
{    
    cout << " \tID \t AID " << endl;
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < TOTAL_AID; j++)
        {
            cout << print[i][j] << endl;
        }           
    }
}    
Littm
  • 4,923
  • 4
  • 30
  • 38
Nick
  • 91
  • 1
  • 1
  • 2
  • Im still new at this therefore the layout of my text file and the output is not showing correctly in my question. The text file is in column format and the output that iam getting after compiling is verticle. I hope iam making sense. – Nick Sep 07 '12 at 03:03
  • I don't see any question. What do you want the output to look like? – vsz Sep 07 '12 at 03:05
  • 1
    The output should look exactly like the text file but for some reason it appears vertically on the screen. Is there something wrong with my code. If so please if anyone can help me correct it. – Nick Sep 07 '12 at 03:13
  • I used your code to compile it and check it out and my output just printed 5,6,7,8 not 1,2,3,4,5,6,7,8 – Rapptz Sep 07 '12 at 03:14
  • the "endl" character on your cout call prints a newline... try -> cout << print[i][j]; and specify where the newlines should fall. – Daniel B. Chapman Sep 07 '12 at 03:17
  • Hi Daniel, I just did what you recommended but now instead the numbers are printing in one line only; like 12345678. – Nick Sep 07 '12 at 03:20

3 Answers3

20

You are printing std::endl after each number. If you want to have 1 row per line, then you should print std::endl after each row. Example:

#include <iostream>

int main(void)
{
    int myArray[][4] = { {1,2,3,4}, {5,6,7,8} };
    int width = 4, height = 2;

    for (int i = 0; i < height; ++i)
    {
        for (int j = 0; j < width; ++j)
        {
            std::cout << myArray[i][j] << ' ';
        }
        std::cout << std::endl;
    }
}

Also note that writing using namespace std; at the beginning of your files is considered bad practice since it causes some of user-defined names (of types, functions, etc.) to become ambiguous. If you want to avoid exhausting prefixing with std::, use using namespace std; within small scopes so that other functions and other files are not affected.

LihO
  • 41,190
  • 11
  • 99
  • 167
  • Im still a beginner and learning C++ through distant learning and so far i didnt came across std::cout. And im using DEV as my IDE so if i dont use "using namespase std", i encounter errors. but thank you so much for the advice. – Nick Sep 07 '12 at 03:31
  • 3
    "using namespace std" is not so bad when used in cpp files as opposed to headers. – evpo Sep 07 '12 at 05:36
1

It is not only mistake that you miss the "endl". The program will also skip the first line in the source file because of calling the function discard_line(reg), so you only can get the others data(5 6 7 8). It is not necessary to use the function at all. in addition, make sure that you init the array and check boundary of array, such as MAX_SIZE, to guarantee the input data not to overflow the array.

MichaelGD
  • 11
  • 1
0

you can do it like this

#include <iostream>

int your_array[2][4] = { 
  {1,2,3,4}, 
  {5,6,7,8}  
};

using namespace std;

int main() {

    // get array columns and rows
      int rows =  sizeof your_array / sizeof your_array[0]; 
      int cols = sizeof your_array[0] / sizeof(int); 
      
      // Print 2d Array
     cout << "your_array data "<<endl<<endl;
    for (int i = 0; i < rows; ++i)
    {
        for (int j = 0; j < cols; ++j)
        {
            std::cout << your_array[i][j] << std::endl;
        }
     //   std::cout << std::endl;
    }

}

output

1
2
3
4
5
6
7
8
user889030
  • 4,353
  • 3
  • 48
  • 51