如何在C++中打印二维数组?

9
我将尝试使用数组在屏幕上打印文本文件,但我不确定为什么它看起来与文本文件中的内容不同。
文本文件:
1 2 3 4
5 6 7 8

应用discard函数后,在屏幕上显示如下内容:
1
2
3
4
5
6
7
8

代码:

#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;
        }           
    }
}    

我还很新,因此我的文本文件布局和输出在我的问题中未正确显示。该文本文件为列格式,编译后得到的输出是垂直的。希望我的表达有意义。 - Nick
我没有看到任何问题。你希望输出的样子是什么? - vsz
1
输出应该与文本文件完全相同,但由于某种原因它在屏幕上垂直显示。我的代码有什么问题吗?如果有,请问是否有人可以帮我纠正它。 - Nick
我使用了你的代码进行编译和测试,但我的输出只打印了5、6、7、8,而没有1、2、3、4、5、6、7、8。 - Rapptz
在你的cout调用中,“endl”字符会打印一个新行...尝试 -> cout << print [i] [j]; 并指定新行应该放在哪里。 - Daniel B. Chapman
嗨,丹尼尔,我刚刚按照你的建议做了,但现在数字只印在一行上,就像12345678这样。 - Nick
3个回答

20
你正在每个数字后打印 std::endl。如果你想让每行只有一个数字,那么应该在每行后打印 std::endl。例如:

你正在每个数字后打印 std::endl。如果你想让每行只有一个数字,那么应该在每行后打印 std::endl。例如:

#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;
    }
}

请注意,在文件开头写 using namespace std; 被认为是一种不良实践,因为它会导致某些用户定义的名称(类型、函数等)变得模糊不清。如果你想避免频繁地在代码中加上前缀 std::,可以在小范围内使用 using namespace std;,以免影响其他函数和文件。


我还是一个初学者,正在通过远程学习学习C++,到目前为止我还没有接触过std::cout。而且我正在使用DEV作为我的IDE,所以如果我不使用“using namespace std”,就会遇到错误。但是非常感谢你的建议。 - Nick
3
在cpp文件中使用"using namespace std"并不那么糟糕,与头文件相比。 - evpo

1

你错了不只是漏掉了"endl"。 由于调用了discard_line(reg)函数,程序还会跳过源文件的第一行,因此你只能获取其他数据(5 6 7 8)。实际上,根本没有必要使用这个函数。 此外,确保初始化数组并检查数组的边界,例如MAX_SIZE,以确保输入数据不会溢出数组。


0

你可以像这样做

#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;
    }

}

输出

1
2
3
4
5
6
7
8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接