如何打印二维数组在C ++? [英] How to print 2D Arrays in C++?
问题描述
我试图打印文本使用阵列上的文件出来在屏幕上,但我不知道为什么它没有出现在文本文件的方式。
该文本文件:
1 2 3 4
5 6 7 8
在屏幕上显示为适用丢弃功能后如下:
1
2
3
4
五
6
7
8
的code:
的#include<&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;串GT;使用命名空间std;const int的MAX_SIZE = 20;
const int的TOTAL_AID = 4;无效discard_line(ifstream的&安培;中);
无效打印(打印INT [] [4],INT大小);诠释的main()
{
//字符串evnt_id [MAX_SIZE]; //商店的事件ID
INT athlete_id [MAX_SIZE] [TOTAL_AID]; //为痤疮各类ID商店列
诠释total_records;
焦炭℃;
ifstream的章;
reg.open(C:\\\\的Result.txt); discard_line(REG);
total_records = 0; 而(!reg.eof())
{
的for(int i = 0; I< TOTAL_AID;我++)
{
章>> athlete_id [total_records] [I]; //读取援助coloumns
}
total_records ++;
reg.get(C);
} reg.close(); 打印(athlete_id,total_records); 系统(暂停);
返回0;
}无效discard_line(ifstream的&安培;中)
{
焦炭℃; 做
in.get(C);
而(C ='\\ n'!);
}无效打印(打印INT [] [4],INT大小)
{
COUT<< \\ TID \\ t AID<< ENDL;
的for(int i = 0; I<大小;我++)
{
对于(INT J = 0; J< TOTAL_AID; J ++)
{
COUT<<打印[i] [j]的<< ENDL;
}
}
}
您在每个号码后打印的std :: ENDL
。如果你想有每行1列,那么你应该每一行后打印的std :: ENDL
。例如:
的#include<&iostream的GT;INT主要(无效)
{
INT myArray的[] [4] = {{1,2,3,4},{5,6,7,8}};
INT宽度= 4,高度= 2; 的for(int i = 0; I<高度; ++ I)
{
对于(INT J = 0; J<宽度; ++ j)条
{
性病::法院LT&;< myArray的[I] [J]<< '';
}
性病::法院LT&;<的std :: ENDL;
}
}
另外请注意,文字使用命名空间std;
在文件的开头被认为是不好的做法,因为它会导致一些用户自定义的名称(类型,函数等)变得模糊。如果你想避免耗尽prefixing与的std ::
,使用命名空间std使用;
小的范围内,从而其它的功能和其他文件都不会受到影响。
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;
}
}
}
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.
这篇关于如何打印二维数组在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!