if necessary, create a new project named introductory20 Project and save it in t
ID: 3817953 • Letter: I
Question
if necessary, create a new project named introductory20 Project and save it in the Cpp8Chap13 folder. Also create a new source file named introductory20.cpp. Write a program that displays the appropriate shipping based on the region code entered by the user. To be valid, the region code must contain exactly 3 characters: a letter(either A or B) followed by 2 numbers. The shipping charge for region A is $25. The shiping charge for region B is $30. Display an appropriate message if the region code is invalid. Use a sentinel value to end the program. Save and then run the program. Test the program using the following region codes: A11, B34, C7, D2A, A3N, C45, and 74TV. in C++ please. The 2 answers i have found on here did not work for me. Thanks.
Explanation / Answer
code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
string code;
while(code!="stop")
{
cout<<"enter region code:"<<endl;
cin>>code;
//cout<<!isdigit(code[2]);
if(code.size()!=3 or (code[0]!='A' and code[0]!='B') or (!isdigit(code[1]) or !isdigit(code[2])))
{
cout<<"invalid code";
continue;
}
if(code[0]=='A')
cout<<"The shipping charge is $25"<<endl;
if(code[0]=='B')
cout<<"The shipping charge is $30"<<endl;
}
}
sample output:
enter region code:
A11
The shipping charge is $25
enter region code:
B34
The shipping charge is $30
enter region code:
C7
invalid codeenter region code:
D2A
invalid codeenter region code:
A3N
invalid codeenter region code:
C45
invalid codeenter region code:
74Tv
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.