Question
I am trying to create a program that displays shipping charges for two region codes. The region code has to have exactly 3 characters (the letter A or B followed by two numbers). Display a message if invalid region code is entered. use a sentinel value to exit program. shipping for region A is $25 and region B is $30
Here is what I have so far.
<code>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string regionCode = "";
cout << "Please enter region code (-1 to exit): ";
getline(cin, regionCode);
while (regionCode != "-1")
if (regionCode.find('A', 0))
{
cout << "Shipping cost is $25" << endl;
}
else if (regionCode.find('B', 0));
{
cout << "Shipping cost is $30" << endl;
}
else if
{
cout << "Please enter a valid region code" << endl;
} //end if
cout << "Please enter region code (-1 to exit): ";
getline(cin, regionCode);
system("PAUSE");
return 0;
} //end of main function
</code>
Here is what I have so far.
<code>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string regionCode = "";
cout << "Please enter region code (-1 to exit): ";
getline(cin, regionCode);
while (regionCode != "-1")
if (regionCode.find('A', 0))
{
cout << "Shipping cost is $25" << endl;
}
else if (regionCode.find('B', 0));
{
cout << "Shipping cost is $30" << endl;
}
else if
{
cout << "Please enter a valid region code" << endl;
} //end if
cout << "Please enter region code (-1 to exit): ";
getline(cin, regionCode);
system("PAUSE");
return 0;
} //end of main function
</code>
Answers
Steve
so, have you compiled and run it?
does it do the job?
does it do the job?
Anon
It won't compile because it's saying the second "else if" isn't valid. I have tried changing things around and the best I can get is when I enter a value starting with"A" it returns nothing and a value starting with "B" returns and endless loop of $25, but it should be $30 and then wait for the user to enter a key to exit