用户输入在循环c ++中不断询问

时间:2014-12-02 21:48:59

标签: c++ for-loop while-loop cout cin

我正在制作一个程序,该程序需要多个学生姓名,并根据输入的成绩计算最终成绩。我遇到的问题是,当它循环到第二个学生时,它不会让我输入成绩,我不知道为什么。有什么建议吗?

#include <algorithm>                                                            
#include <iomanip>                                                              
#include <ios>                                                                  
#include <iostream>                                                             
#include <string>                                                               
#include <vector>                                                               

using std::cin;     using std::setprecision;                                    
using std::cout;    using std::string;                                          
using std::endl;    using std::streamsize;                                      
using std::sort;    using std::vector;

int main() {
     vector<string> names;                                                       
     vector<double> finals;                                                      
     typedef vector<double>::size_type vec_sz;                                   
     vec_sz sizeStud;                                                            
     string name;                                                                
     double sum, grade;                                                          
     int counter;                                                                

     cout<<"Please enter the student's names \"end-of-input\" to end:\t";        
     while(cin>>name&&name!="end-of-input") {                                    
         names.push_back(name);                                                  
     }                                                                           
     sizeStud = names.size();                                                    
     if(sizeStud == 0) {                                                         
         cout<<"No students entered! Please try again."<<endl;                   
         return 1;                                                               
     }                                                                           

     for(int i = 0; i < sizeStud; i++) {                                         
         sum=0;                                                                                            
         counter=0;                                                              
         cout<<"Please enter the grades for "<<names[i]                          
             <<" \"end-of-input\" to end:\t";                                    
         while(cin>>grade) {                                                     
             counter++;                                                          
             sum+=grade;                                                         
         }                                                                       
         if(counter==0) {                                                        
             cout<<"No grades entered! Please try again."<<endl;                 
         }                                                                       
         else {                                                                  
             cout<<endl;                                                         
             finals.push_back(sum/counter);                                      
         }                                                                       
     }                                                                           
     for(int i =0 ; i < sizeStud; i++) {                                         
         streamsize prec = cout.precision();                                     
         cout<<names[i]<<"'s final:\t\t"<<setprecision(4)                        
             <<finals[i]<<setprecision(prec)<<endl;                              
     }                                                                           

     return 0;              
}

示例输入:

fred sally joe end-of-input
10 20 end-of-input
30 end-of-input

示例输出(来自ideone.com here):

Please enter the student's names "end-of-input" to end: Please enter the grades for fred "end-of-input" to end: 
Please enter the grades for sally "end-of-input" to end:    No grades entered! Please try again.
Please enter the grades for joe "end-of-input" to end:  No grades entered! Please try again.
fred's final:       15
sally's final:      1.719e-312
joe's final:        1.316e-312

1 个答案:

答案 0 :(得分:1)

你的问题就在这一行:

while(cin>>grade)

因为gradedouble,如果您的输入是&#34;输入结束&#34; while循环将退出,但cin流将设置为错误状态,并且不会尝试进一步输入(下一个for循环迭代将到达while(cin>>grade)并立即掉落)。您可以使用cin.clear()清除错误状态,并cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n')使用该行的其余输入。然后输入如......

fred sally joe end-of-input
10 20 30 any-non-numeric-content
10 20 sally's scores
30 end-of-input

...将&#34;工作&#34;。