C++
If necessary, create a new project named Introductory20 Project and save it in the Cpp8\Chap13 folder. Also create a new source file named Introductory20.cpp. Write a 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.
Hi. Below is the code that i had compiled in visual studio for C++.
***************************************************************************
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
char code[10];
char ch;
int length;
int flag;
do
{
cout << "\nenter the region
code :";
cin >> code;
length = 0;
flag = 0;
if (code[0] != 'A' &&
code[0] != 'B')
{
flag = 1;
}
for (int i = 0; code[i] != '\0';
i++)
{
length++;
if (length >
3)
{
flag = 1;
break;
}
int ascii =
code[i];
if (i > 0
&& !(ascii >= 48 && ascii <= 57)) //ascii
value of digits 0-9 is 48 to 57
{
flag = 1;
break;
}
}
if (length < 3)
{
flag = 1;
}
if (flag == 0)
{
int charge =
(code[0] == 'A') ? 25 : 30;
cout <<
"\n\nThe shipping charge is : $" << charge;
}
else
{
cout <<
"\nInvalid region code!!!";
}
cout << "\n\twant to enter
more (y/n)?";
cin >> ch;
} while (ch != 'n');
return 0;
}
********************************************************************
output :
feel free to ask if you have any doubt :)
Get Answers For Free
Most questions answered within 1 hours.