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

Why is this portion of code still executing?

本文关键字:执行 代码 这部分 为什么      更新时间:2023-10-16

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

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

那是因为你需要这个-

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

现在代码的问题是,如果您甚至输入'r''R',由于if else条件,'p''P'变成falseelse部分被执行。

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