Довольно новый для программирования здесь.Я не могу заставить мой код по умолчанию в операторе switch работать.Я хочу, чтобы он отображал ошибку и выходил, как только вводится любой ввод данных, который не находится в диапазоне.По какой-то причине он переходит к следующей строке cout
после оператора switch
.
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
char choice;
const double P=8.00, A=10.50, B=12.50;
double t,w,price;
cout<<"\nPlease enter the state code: ";
cin>>choice;
cout<<"\nPlease enter the Parcel's weight in KG: ";
cin>>w;
switch (choice)
{
case 'P':
case 'p':
t=w*P;
price=P;
break;
case 'A':
case 'a':
t=w*A;
price=A;
break;
case 'B':
case 'b':
t=w*B;
price=B;
break;
default:
cout<<"Error. Please Re-enter. Exiting";
break;
}
cout<<setw(80)<<setfill('*')<<"*"<<endl;
cout<<"\nState code: "<<choice<<endl;
cout<<"Weight <KG>: "<<w<<endl;
cout<<"Price <RM>: "<<price<<endl;
std::cout<<std::fixed;
std::cout<<std::setprecision(2);
cout<<"Total Price <RM>: "<<t<<endl;
cout<<"\nThank you and Please Come Again"<<endl;
cout<<setw(80)<<setfill('*')<<"*"<<endl;
return 0;
}