182k views
3 votes
Write a C++ program that displays the appropriate shipping charge based on the region code entered by the user. To be valid, the region code must contain exactly three characters: a letter (either A or B) followed by two numbers. The shipping charge for region A is $25. The shipping 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.

User Gusridd
by
3.6k points

1 Answer

3 votes

Answer:

Following are the code to the given question:

#include <iostream>//header file

#include <string>//header file

using namespace std;

int main()//main method

{

string region;//defining a string variABLE

while(true)//defining a while loop for input value

{

cout << "Enter the region code(-1 to quit): ";//print message

cin >> region;//input string value

if(region == "-1")//defining if to checks string value

{

break;//use break keyword

}

else//else block

{

if(region.length() != 3)//defining if to check string value length not equal to 3 {

cout<<"Invalid region code. Region code must contain exactly three characters."<<endl;//print message

}

else

{

if(region.at(0) == 'A' || region.at(0) == 'B')//defining at method that checks string value

{

if((region.at(1) >= '0' && region.at(1) <='9') && (region.at(2) >= '0' && region.at(2) <='9'))//defining at method that checks string value

{

if(region.at(0) == 'A')//defining at method that checks string value

{

cout<<"The shipping charge for region is $25."<<endl;//print message

}

else

{

cout<<"The shipping charge for region is $30."<<endl;//print message

}

}

else

{

cout<<"Invalid region code. Region code should start with A or B followed by two numbers."<<endl;//print message

}

}

else

{

cout<<"Invalid region code. Region code should start with A or B."<<endl;//print message

}

}

}

}

return 0;

}

Output:

Please find the attached file.

Step-by-step explanation:

In this code, inside the main method a string variable "region" is declared that use while loop to the input value and use the conditional statement that checks the input value and print the value as per the condition and print the value.

Write a C++ program that displays the appropriate shipping charge based on the region-example-1
User Webbexpert
by
3.3k points