【发布时间】:2018-10-19 14:33:39
【问题描述】:
所以,这是从 txt 文件中读取矩阵 A 和 B 的代码。我发现的以下代码能够读取这两个文件。但是,矩阵 A 的输出与矩阵 B 重叠(下图) enter image description here
Matrix A -----
1 2 3
4 5 6
7 8 99
Matrix B -----
0 1 1
2 0 0
2 0 00
那么,如何避免它重叠,请帮助:)
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
{
char ch;
const char *fileName="MatrixA.txt"; // FOR MATRIX A
ifstream file;
file.open(fileName,ios::in);
if(!file)
{
cout<<"Error in opening file!!!"<<endl;
return -1;
}
while (!file.eof())
{
file >> noskipws >> ch;
cout << ch;
}
file.close();
}
{
char ch;
const char *fileName="MatrixB.txt"; // FOR MATRIX A
ifstream file;
file.open(fileName,ios::in);
if(!file)
{
cout<<"Error in opening file!!!"<<endl;
return -1;
}
while (!file.eof())
{
file >> noskipws >> ch;
cout << ch;
}
file.close();
}
return 0;
}
编辑:谢谢大家!修复了它,是的,我知道这不是读取矩阵的代码(对不起,错误信息)。我只是想让它看起来像一个,呵呵,再次感谢
【问题讨论】:
-
在两者之间使用
std::cout << std::endl? -
不要复制粘贴代码(你不修复复制代码的注释),而是创建函数。
-
@Jarod42 std::endl 不等于换行符,因为它会刷新
-
@Gladaed:这对初学者来说“更好”(之前的(调试)输出在崩溃之前打印,这有助于定位错误)。