Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C+中打印二维数组+;?_C++_Arrays_Multidimensional Array_Revision - Fatal编程技术网

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

C++ 如何在C+中打印二维数组+;?,c++,arrays,multidimensional-array,revision,C++,Arrays,Multidimensional Array,Revision,我正在尝试使用数组在屏幕上打印一个文本文件,但我不确定它为什么不像在文本文件中那样显示 文本文件: 1 2 3 4 5 6 7 8 应用丢弃功能后,屏幕显示如下: 1 2 3 4 5 6 7 8 代码: #include <iostream> #include <fstream> #include <stdlib.h> #include <string> using namespace std; const int MAX_SIZE = 2

我正在尝试使用数组在屏幕上打印一个文本文件,但我不确定它为什么不像在文本文件中那样显示

文本文件:

1 2 3 4
5 6 7 8
应用丢弃功能后,屏幕显示如下:

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;
        }           
    }
}    
#包括
#包括
#包括
#包括
使用名称空间std;
const int MAX_SIZE=20;
const int TOTAL_AID=4;
作废废弃_行(ifstream&in);
无效打印(整数打印[][4],整数大小);
int main()
{
//字符串evnt_id[MAX_SIZE];//存储事件id
int atternate_id[MAX_SIZE][TOTAL_AID];//存储删除id的列
国际总记录;
字符c;
ifstream-reg;
注册打开(“C:\\result.txt”);
丢弃_线(reg);
记录总数=0;
而(!reg.eof())
{
对于(int i=0;i>运动员id[总记录][i];//阅读辅助栏
}
记录总数++;
注册获得(c);
}
注册关闭();
打印(运动员id、总记录);
系统(“暂停”);
返回0;
}
无效丢弃_行(ifstream&in)
{
字符c;
做
in.get(c);
而(c!='\n');
}
无效打印(整数打印[][4],整数大小)
{    

cout您正在打印每个数字后的
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;
    }
}
#包括
内部主(空)
{
int myArray[][4]={{1,2,3,4},{5,6,7,8};
内部宽度=4,高度=2;
对于(int i=0;istd::cout错过“endl”并不是唯一的错误。
由于调用函数discard_line(reg),程序也将跳过源文件中的第一行,因此您只能获取其他数据(5 6 7 8)。根本不需要使用该函数。

此外,请确保初始化数组并检查数组的边界,例如MAX_SIZE,以确保输入数据不会溢出数组。

您可以这样做

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

}

我对此还是新手,因此我的文本文件的布局和输出在我的问题中没有正确显示。文本文件是列格式的,我编译后得到的输出是垂直的。我希望我是有意义的。我没有看到任何问题。你希望输出看起来像什么?输出应该完全像文本文件但由于某些原因,它垂直出现在屏幕上。我的代码有问题吗?如果有,请任何人帮我更正。我用你的代码编译并检查它,我的输出只是打印了5,6,7,8而不是1,2,3,4,5,6,7,8“endl”在你的CUT上的字符调用打印新行……-> CUT仍然是初学者,通过远程学习学习C++,到目前为止我还没有遇到STD::CUT。IM使用DEV作为IDE,所以如果我不使用“使用NAMESPASSTD”,我会遇到错误。但是非常感谢您的建议。与头文件相比,在cpp文件中使用时效果并不差。
1
2
3
4
5
6
7
8