2017-02-20 52 views
-2

我一直在试图解决这个问题,现在似乎我所尝试的所有东西都让它变得更糟。我正在设计一个程序,它读取输入文件,计算并打印到输出文件。 (它是一个排序计算器)现在我只是试图让它显示输入中每行的输出中的名称和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 

它跳过第一和第六名。

我试图改变的变量字符串变量,改变循环的顺序,名字为两个可变(第一和最后一个)

任何帮助是极大的赞赏

+2

你为什么不使用'的std :: string'而不是'字符XYZ [XX]'任何理由吗? – WhiZTiM

+0

写完后关闭文件。 – Sedrick

+1

解决这些问题的正确工具是您的调试器。在*堆栈溢出问题之前,您应该逐行执行您的代码。如需更多帮助,请阅读[如何调试小程序(由Eric Lippert撰写)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您应该\编辑您的问题,以包含一个[最小,完整和可验证](http://stackoverflow.com/help/mcve)示例,该示例再现了您的问题,以及您在调试器。 –

回答

1

所不同的是,那两条输入行,你名字之间有一个空格。

比较

Truman, Tod 12388671 100 100 100 
Seger,John 67894 100 100 100 100 

由于istringstream::operator>>直到空间读取第一个字符串时,它会在那个空间停止,并会收到名称的第二部分第二个字符串,而不是数量最多读取。

该解决方案使您的数据一致,或者在解析行时允许存在或不存在空间。

您还有一个错误,它会使您的示例代码在这里不可用。你永远不会打开输出文件。你打开输入文件两次!

infile.open(file_nameO); =>outfile.open(file_nameO);

一些搞掂代码

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 

using namespace std; 

void printRecord (const string& name, const string& Id, ostream& outfile) { 
    outfile << name << " " << Id << endl; } 

int main() { 
    string line; 
    string file_nameI, file_nameO; 

    cout << "Please enter name of input file: "; 
    //cin >> file_nameI; 
    file_nameI = "grades.txt"; 
    cout << file_nameI << '\n'; 
    ifstream infile(file_nameI.c_str()); 
    if (!infile) 
    { 
     cout << "Could not open input file \n"; 
     return 0; 
    } 

    cout << "Please enter name of output file: "; 
    //cin >> file_nameO; 
    file_nameO = "gradesout.txt"; 
    cout << file_nameO << '\n'; 

    ofstream outfile(file_nameO.c_str()); 
    if (!outfile) 
    { 
     cout << "Could not open output file \n"; 
     return 0; 
    } 

    while (getline (infile, line)) 
    { 
     string name, id; 
     istringstream iss(line); 
     iss >> name >> id; 
     cout << name << " " << id; 
     printRecord(name, id, outfile); 
     cin.ignore(); 
    } } 
+0

非常感谢!此外,输入不一致的原因是因为我最初在那里有我的名字,当我在这里发布它时,我明显改变了它,但是我没有仔细检查。不过,我添加了其他建议,现在我的代码完美无缺地工作了! – Morgan