为什么这部分代码仍在执行?

时间:2016-04-02 14:19:33

标签: c++ c

为什么我的其他人,cout<< "您输入了错误的代码"我输入r或R后仍然执行并写入屏幕并完成计算和对话。当我输入p或P并完成我的程序的那部分时,同样的情况也不会发生。抱歉这个令人难以置信的nooby问题。

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

int main()
{
    char service;
    int number;
    int minutes;
    int dayMinutes;
    int nightMinutes;
    double bill;
    double dayCharge;
    double nightCharge;
    double const REG_FEE = 10.00;
    double const PREM_FEE = 25.00;
    double const REG_MIN = 0.20;
    double const PREM_DAY = 0.10;
    double const PREM_NIGHT = 0.05;

    cout << "Please enter your account number: ";
    cin >> number;
    cout << "Please enter your service type (regular or premium): ";
    cin >> service;

    if (service == 'r' || service == 'R')
{
        cout << "How many minutes have been used for this service?: ";
        cin >> minutes;

        if (minutes <= 50)
        {
            bill = REG_FEE;
            cout << fixed << showpoint << setprecision(2);
            cout << "Your bill is $" << bill << "." << endl;
            cout << "The account number entered was: " << number << "." << endl;
            cout << "The service type entered was: " << service << "." << endl;
            cout << "You used: " << minutes << " minutes." << endl;
        }
        else
        {
            bill = ((minutes - 50) * REG_MIN) + REG_FEE;
            cout << fixed << showpoint << setprecision(2);
            cout << "Your bill is $" << bill << "." << endl;
            cout << "The account number entered was: " << number << "." << endl;
            cout << "The service type entered was: " << service << "." << endl;
            cout << "You used: " << minutes << " minutes." << endl;
        }

}
    if (service == 'p' || service == 'P')
{
        cout << "How many minutes were used during the day?: ";
        cin >> dayMinutes;
        cout << "How many minutes were used during the night?: ";
        cin >> nightMinutes;

        if (dayMinutes > 75)
        {
            dayCharge = ((dayMinutes - 75) * PREM_DAY);
        }
        if (nightMinutes > 100)
        {
            nightCharge = ((nightMinutes - 100) * PREM_NIGHT);
        }
            bill = dayCharge + nightCharge + PREM_FEE;
            cout << fixed << showpoint << setprecision(2);
            cout << "Your bill is $" << bill << "." << endl;
            cout << "The account number entered was: " << number << "." << endl;
            cout << "The service type entered was: " << service << "." << endl;
            cout << "You used: " << dayMinutes + nightMinutes << " minutes." << endl;

}
    else
    {
            cout << "You have entered an invalid service code." << endl;
            cout << "The account number entered was: " << number << "." << endl;
            cout << "The service type entered was: " << service << "." << endl;
    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

那是因为你需要这个 -

 if (service == 'r' || service == 'R'){
  // your code
 }
 else if(service == 'p' || service == 'P'){
  //your code
 }
 else {
  //your code 
 }  

现在您的代码问题是,如果您输入'r''R',由于if else条件'p''P'变为{{ 1}}和false部分被执行。

这就是为什么你需要使用else格式,以便输入只执行一个部分。