私の質問には事前にお詫び申し上げます。私の初めてのプログラミング経験は、私の現在のコースでした。質問には、なぜ私のコードはこのループをスキップしますか?
なぜ私のコードは2番目のwhileループをスキップしていますか?入力にpまたはPを選択すると、最初のwhileループに関する入力を求められますが、2番目の入力は必要ありません。例:分/分の代わりに分。 @Vaughnカトーとして
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char service;
int number;
int minutes;
int dayMinutes;
int nightMinutes;
double bill;
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;
while (service == 'r' || '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 << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << minutes << " minutes." << endl;
cout << "Your bill is $" << bill << "." << endl;
}
else
{
bill = ((minutes - 50) * REG_MIN) + REG_FEE;
cout << fixed << showpoint << setprecision(2);
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << minutes << " minutes." << endl;
cout << "Your bill is $" << bill << "." << endl;
}
return 0;
}
while (service == 'p' || '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)
{
bill = ((dayMinutes - 75) * PREM_DAY) + PREM_FEE;
}
if (nightMinutes > 100)
{
bill = ((nightMinutes - 100) * PREM_NIGHT) + PREM_FEE;
bill = bill + PREM_FEE;
cout << fixed << showpoint << setprecision(2);
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << dayMinutes + nightMinutes << " minutes." << endl;
cout << "Your bill is $" << bill << "." << endl;
}
else
{
cout << "You have entered an invalid service code.";
}
return 0;
}
return 0;
}
ループに 'return'があり、終了します。 –
whileループの目的は何ですか?ループの使用が冗長であるため、各ループを一度だけ実行するように見えます。 –
whileループが壊れています。それは((service == 'r')||(service == 'R))でなければなりません。そうでなければ、条件は常に真です – Andreas