cin.get()的c ++问题在断开循环时递增

时间:2014-05-24 08:04:12

标签: c++ stream iostream

所以我想写一个加油泵程序。它的编译和工作,但我唯一的问题是当我来打印出最后的加仑量和充电量。当我按q然后输入退出循环并得到答案时,它再增加一次。看一下displaycalc()函数。

#include <iostream>
using namespace std;


class GasPump
{
    private:
        double gallons;
        double charge;
        double gasInTank ;
        double costPerGallonReg;
        double costPerGallonPlus;
        double costPerGallonSup;
        double costPerGallonChoice;


    public:
        void regularSetting();
        void setPricePerGallon(double,double,double);
        double getPricePerGallon();
        void displayCalc();
};

//first function
void GasPump::regularSetting()

{
  gallons = 0;
  charge = 0;
  gasInTank = 10.0;



  cout<<"Price "<<charge<<endl;
  cout<<"Gallons "<<gallons;
}

void GasPump::setPricePerGallon(double reg, double plus, double super)
{   
    cout<<"\n\nPlease choose type of gas you want to Purchase\n";
    cout<<"\nRegular: "<<reg<<" Plus: "<<plus<<" Premium: "<<super;
    //setting prices
    costPerGallonReg = reg;
    costPerGallonPlus = plus;
    costPerGallonSup = super;
}
double GasPump::getPricePerGallon()
{
    char choice;
    cout <<"\nEnter r/R for Regular, p/P for Plus, or s/S for Super.\n";

    cin >>choice;

    if ((choice == 'R' || choice == 'r'))
    {
        return  costPerGallonChoice = costPerGallonReg ; 
    }
    else if ((choice == 'P'|| choice == 'p'))
    {
        return  costPerGallonChoice = costPerGallonPlus ;
    }
    else if ((choice == 'S'|| choice == 's'))
    {
        return  costPerGallonChoice = costPerGallonSup ;
    }
    else
    {
        cout<<"Invalid";
    }

}
void GasPump::displayCalc()
{
     cout.setf(ios::fixed);
     cout.setf(ios::showpoint);
     cout.precision(2);

    char choice;
    system("cls");
    cout<<"Press Enter to start filling and q to quit\n";

    cout<<"Price "<<charge<<endl;
    cout<<"Gallons "<<gallons<<endl;

    while(gasInTank > 0)
   { 

    choice = cin.get();

     if((choice =='Q'||choice =='q'))
     break;

     system("cls");

     cout<<"$"<<charge<<" ";
     cout<<"Gallons "<<gallons<<endl;
     charge += costPerGallonChoice * 0.1;
     gallons += 0.1;
     gasInTank -= 0.1;

  }
    cout<<"\nThank you for your purchase\n";
    cout<<"Gallons: "<<gallons<<" $"<<charge;       
}









int main()
{
    //naming class
    GasPump pump;

    cout<<"Gas pump Program\n\n";

    pump.regularSetting();
    pump.setPricePerGallon(1.69,1.79,1.89);
    pump.getPricePerGallon();
    pump.displayCalc();


    getchar();getchar();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

您的代码几乎是正确的,有两个问题:刷新cin流并在while循环中移动cout,代码如下:

 cout<<"$"<<charge<<" ";
 cout<<"Gallons "<<gallons<<endl;

在while循环结束时,如

cin.ignore(1000, '\n');
cin.get();
cout<<"$"<<charge<<" ";
cout<<"Gallons "<<gallons<<endl; // if you really want to display the 0 
while(gasInTank > 0)
{ 
     choice = cin.get();

     if((choice =='Q'||choice =='q'))
         break;

     system("cls");

     // Move these 2 lines at the end
     // cout<<"$"<<charge<<" ";
     // cout<<"Gallons "<<gallons<<endl;
     charge += costPerGallonChoice * 0.1;
     gallons += 0.1;
     gasInTank -= 0.1;
     // moved here, now it's ok
     cout<<"$"<<charge<<" ";
     cout<<"Gallons "<<gallons<<endl;
}

否则在递增之前显示。基本上,当您按下第一个ENTER时,您已将气体放入油箱,变量会增加,但在原始代码中,您显示的是增量前的数量。