重复最后一次输入两次

时间:2015-02-05 23:25:50

标签: c++

#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;
}

我正在尝试从文件中取出数据并让它读取并告诉该人是否已被录取。一切都有效,除了最后一次数据,它给我最后一次数据两次如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您需要在读取数据的行之后立即检查读取是否成功。

而不是

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

使用

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