2012-12-11 57 views
-2

下面的程序我的代码应该 列出了每个学生的所有付款,显示支付金额和优秀函数getline()和读取数据从文本文件

我需要一些帮助的以下部分:

void payment() 
{ 
    // Display message asking for the user input 
    std::cout << "List all payment made by each student, show amount paid and outstanding." << std::endl; 

    // Read from text file and Display list of payment 

    std::ifstream infile;    // enable to open, read in and close a text file 
    float StudentCode;     // to store the student enrolment number 
    float Amount;      // to store the amount of money 
    float Type;       // to store information on type of payment made 
    float Outstanding;     // to store amount of money is due 

    infile.open("Payment.txt");   // open a text file called Payment 

    if (!infile)     
    { 
     std::cout << "Item list is empty" << std::endl;  // if the file is empty it output the message 
    } 
    else 
    { 
     std::cout << "List of Payment: " << std::endl; 
     std::cout << "" << std::endl; 
     std::cout << "Enrolment No." << "Amount" << "Outstanding" << std::endl; 

     // If there is Student Code that has the same number, it need to combined the amount it paid 
     // For an example 
     // Student Code: 12 Amount: 25 
     // Student Code: 12 Amount: 50 
     // so it should display the following when the program runs: 
     // Student Code: 12 Amount: 75 

     while(!infile.eof())   // output the description of the text file onto the screen 
     { 
      getline(infile,StudentCode,Amount); 

      Outstanding = Amount - 100; 

      std::cout << StudentCode << Amount << "$" << Outstanding << std::endl; 
      //iter++; 
     } 
     std::cout << "End of list\n" << std::endl; 
    } 

    infile.close();   // close the text file 
} 

什么是错的,则对getline部分:

getline(infile,StudentCode, Amount); 

而且程序不应该重复显示学生的代码,但结合它支付的金额。 当我在注释部分

// If there is Student Code that has the same number ..... 

如何做到这一点解释?

+0

的函数getline部分是相当容易地通过使用Google的API回答: http://msdn.microsoft.com/en-us/library/2whx1zkx( v = vs.71).aspx getline读取字符,而不是浮动。 – HerrJoebob

+2

做**不**使用'eof()'来检测阅读是否完成:它没有**这样工作。相反,在**阅读之后,**总是**检查**,您尝试读取输入是正确的。你从哪里得到了'eof()'应该被使用的想法?如果是一本书,请告诉我们标题;如果是你的老师,请告诉你的教导停止教这个废话! –

+0

当我在以前使用getline时,出于某种原因使用eof()。感谢您告诉我 – user1582575

回答

1

getline从流中读取一行到字符串中。你现在要做的,更多的是像这样的

while (infile >> StudentCode >> Amount) { 
    // process values 
} 

如果要总结的所有款项,必须先通过收集的值累积,之后循环,并打印出来

std::map<int, float> amountsPaid; 
int studentCode; 
float amount; 
// accumulate amounts 
while (infile >> studentCode >> amount) { 
    amountsPaid[studentCode] += amount; 
} 

// loop through map and print all entries 
for (auto i = amountsPaid.begin(); i != amountsPaid.end(); ++i) { 
    float outstanding = i->second - 100; 
    std::cout << i->first << ": " << i->second << "$, " << outstanding << '\n'; 
} 
+0

任何人都可以帮忙了解该计划不应该显示重复的学生代码,但结合其支付的金额。 如果学生编号有相同的编号,则需要合并其支付的金额 举例 学生编号:12金额:25 学生编号:12金额:50 所以它应该显示以下内容运行: 学生代码:12金额:75而不是重复两次 – user1582575

+0

@ user1582575查看修改后的答案。首先收集值,然后打印全部。 –

0

你调用getline似乎不正确。

文档状态

istream& getline (istream& is, string& str, char delim); 

但是你给它

getline(istream&, float, float); 

你应该尝试读取线为一个字符串,然后解析出2间浮动。

既然你使用C++,如果文件格式良好,你可以重定向cin,它会更容易。你可以做一些像

while (infile >> StudentCode) { 
    infile >> Amount; 
} 
1

这里有几个问题。一种是getline将一行文字读入单个std::string变量,而不是多个float字段。

对于您可以尝试

infile >> StudentCode >> Amount; 

的第二个问题是,

while(!infile.eof()) 
如果下输入是去工作,但如果 以前输入尝试失败beause它

不会检查到达文件结尾。

的标准方法是给这些组合成

while (infile >> StudentCode >> Amount) 
{ 
    // do something with the code and amount 
}