重复最后一次输入两次

问题描述:

#include <iostream> 
#include <fstream> 
#include <iomanip> 
#include <string> 
using namespace std; 

int main() 
{ 
    bool keeprunning = true; 
    double GPA; 
    char parents; 
    char major; 
    int math; 
    int verbal; 
    int applications=0; 
    int LAaccepted=0; 
    int Maccepted=0; 
    int L=1, M=2, N=3, Y=4; 

    ifstream inFile("stunumbers.txt"); 

    while (keeprunning) 
    { 
     if (inFile) 
     { 
      inFile >> major >> GPA >> math >> verbal >> parents; 

      int combined= math+verbal; 
      applications +=1; 
      cout<<"Acceptance to College by John Fortuna" <<endl <<endl; 
      cout<<"Applicant #: " <<applications <<endl; 
      cout<<"School = " <<major <<" GPA = " <<GPA <<" math = " 
       <<math <<" verbal = " <<verbal <<" alumnus = " <<parents <<endl; 
      if (major == 'L') 
      { 
       cout<<"You are applying to Liberal Arts College" <<endl; 
       if (parents == 'Y') 
       { 
        if (1000 <= combined) 
        { 
         if (LAaccepted <=4) 
         { 
          LAaccepted +=1; 
          cout<<"You have been accepted to the Liberal Arts College!!" <<endl <<endl <<endl <<endl <<endl <<endl; 
         } 
         else if (LAaccepted >= 5) 
          cout<<"You have been denied addmission to the Liberal Arts College due to spots full." <<endl <<endl <<endl <<endl <<endl <<endl; 
        } 
        else if (1000 > combined) 
         cout<<"You have been denied addmission to the Liberal Arts College due to a low SAT." <<endl <<endl <<endl <<endl <<endl <<endl; 
       } 

       else if (parents == 'N') 
       { 
        if (1200 <= combined) 
        { 
         if (LAaccepted <= 4) 
         { 
         LAaccepted +=1; 
         cout<<"You have been accepted to the Liberal Arts College!!" <<endl <<endl <<endl <<endl <<endl <<endl; 
         } 
         else if (LAaccepted >5) 
          cout<<"You have been denied addmission to the Liberal Arts College due to spots full." <<endl <<endl <<endl <<endl <<endl <<endl; 
        } 
        else if (1200 > combined) 
         cout<<"You have been denied addmission to the Liberal Arts College due to a low SAT." <<endl <<endl <<endl <<endl <<endl <<endl; 
       } 
      } 
      else if (major == 'M') 
      { 
       cout<<"Applying to Music" <<endl; 
       if (500 <= math && 500 <= verbal) 
       { 
         if (Maccepted <= 2) 
         { 
          Maccepted +=1; 
          cout<<"You have been accepted to the College of Music!!" <<endl <<endl <<endl <<endl <<endl <<endl; 
         } 
         else if (LAaccepted >= 3) 
          cout<<"You have been denied addmission to the College of Music due to spots full." <<endl <<endl <<endl <<endl <<endl <<endl; 
       } 
        else if (math <= 500 || 500 <= verbal) 
        { 
         cout<<"You have been denied addmission to the College of Music due to a low SAT." <<endl <<endl <<endl <<endl <<endl <<endl; 
       } 
      }  

      else 
       keeprunning = false; 
      } 
    } 

    return 0; 
} 

我试图从一个文件中取出数据并让它读取并告诉该人是否已被录取。一切工作除了最后的数据,它不断给我最后的数据两次,我该如何解决这个问题?重复最后一次输入两次

+2

每个人的份上,请花营造[小例子]时间(http://stackoverflow.com/help/mcve)。在这个过程中,你可以自己发现问题的可能性很大。 – 2015-02-05 23:28:55

您需要将读取成功与否的检查移动到读取数据的行后面。

而不是

if (inFile) 
    { 
    inFile >> major >> GPA >> math >> verbal >> parents; 

使用

inFile >> major >> GPA >> math >> verbal >> parents; 
    if (inFile) 
    { 
+0

其实,只要用if(inFile >> major >> GPA >> math >> verbal >> parents)''。在英语中,“如果阅读专业,GPA,数学......成功了” – MSalters 2015-02-06 09:35:19