我一直在试图解决这个问题,现在似乎我所尝试的所有东西都让它变得更糟。我正在设计一个程序,它读取输入文件,计算并打印到输出文件。 (它是一个排序计算器)现在我只是试图让它显示输入中每行的输出中的名称和ID号。程序没有将部件打印到输出文件中C++
#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
using namespace std;
float printRecord (char name[20], char Id[20], ostream& outfile)
{
outfile << name << " " << Id << endl;
return 0;
}
int main()
{
ofstream outfile;
ifstream infile;
std::string line;
char file_nameI[21], file_nameO[21], name[20], Id[20];
float hworkgrade, grade1;
int deductions;
cout << "Please enter name of input file: ";
cin >> file_nameI;
infile.open(file_nameI);
if (!infile)
{
cout << "Could not open input file \n";
return 0;
}
cout << "Please enter name of output file: ";
cin >> file_nameO;
infile.open(file_nameO);
if (!outfile)
{
cout << "Could not open output file \n";
return 0;
}
while (getline (infile, line))
{
istringstream iss(line);
iss >> name >> Id;
cout<< name << " " << Id;
printRecord(name, Id, outfile);
cin.ignore();
}
return 0;
}
这里是输入
Truman, Tod 12388671 100 100 100
Seger,John 67894 100 100 100 100
Victoire,Susan 938442 0 0 0
Kodak,James 554668 101 100 100
Frence,Lauren 602983 -1 100 100
Hanz, Franz 58027201 100 100 100
Laufeson,Loki 7920100 34 59 24
这里是输出:
12388671
Seger,John 67894
Victoire,Susan 938442
Kodak,James 554668
Frence,Lauren 602983
58027201
Laufeson,Loki 7920100
它跳过第一和第六名。
我试图改变的变量字符串变量,改变循环的顺序,名字为两个可变(第一和最后一个)
任何帮助是极大的赞赏
你为什么不使用'的std :: string'而不是'字符XYZ [XX]'任何理由吗? – WhiZTiM
写完后关闭文件。 – Sedrick
解决这些问题的正确工具是您的调试器。在*堆栈溢出问题之前,您应该逐行执行您的代码。如需更多帮助,请阅读[如何调试小程序(由Eric Lippert撰写)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您应该\编辑您的问题,以包含一个[最小,完整和可验证](http://stackoverflow.com/help/mcve)示例,该示例再现了您的问题,以及您在调试器。 –