【问题标题】:c++ issue with cin.get() incrementing when breaking out of loop跳出循环时cin.get()递增的c ++问题
【发布时间】:2014-07-13 14:36:23
【问题描述】:

所以我正在尝试编写一个气泵程序。它的编译和工作,但我唯一的问题是当我打印出最终数量的加仑和费用时。当我按 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;
}

【问题讨论】:

    标签: c++ stream iostream


    【解决方案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 时,您已经将气体放入罐中,变量增加了,但在您的原始代码中,您显示的是增量之前的数量。

    【讨论】:

    • 是的,但如果我按照你的方式去做,它就不会从零开始。
    • 好的,现在可以了,输入流的刷新也有问题。我将cin.ignore(1000, '\n'); 放在while 之前。现在,如果您在开始时按q,它会显示 0 加仑(水箱中没有放入任何东西)。一旦你按下第一个输入,它就会开始填充水箱。
    • 但是你GasPump::getPricePerGallon()有问题,如果你输入了一个无效的字符,函数就存在而不返回任何东西,这是不行的。
    • 非常感谢您,是的,getpricepergallon 功能仍在开发该程序。我要修复它。我试图让主要的事情首先工作。
    猜你喜欢
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多