I have a program started but I\'m stuck on how to make it display a shipping cha
ID: 3569326 • Letter: I
Question
I have a program started but I'm stuck on how to make it display a shipping charge. I have to create a program that displays the correct shipping charge based on the zip code entered by the user. Program needs to state whether zip is a valid 5-digit zip. But it also needs to display if the zip starts with "605" the shipping charge displays $25 or if the zip starts with "606" the shipping charge is $30. Also use a sentinel value to end program. Below is what I have come up with so far...
#include <iostream>
#include <string>
using namespace std;
int main()
{
const string VALID_MSG = "Valid ZIP";
const string INVALID_MSG = "Invalid ZIP";
string zipCode = "";
cout << "Five-character ZIP code (-1 to end): ";
cin >> zipCode;
while (zipCode != "-1")
{
if (zipCode.length() == 5)
cout << VALID_MSG << endl;
else
cout << INVALID_MSG << endl << endl;
//end if
cout << "Five-character ZIP code (-1 to end): ";
cin >> zipCode;
} //end while
//system("pause");
return 0;
} //end of main function
Thanks
:-)
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main()
{
const string VALID_MSG = "Valid ZIP";
const string INVALID_MSG = "Invalid ZIP";
string zipCode = "";
cout << "Five-character ZIP code (-1 to end): ";
cin >> zipCode;
while (zipCode != "-1")
{
if (zipCode.length() == 5){
cout << VALID_MSG << endl;
if(zipCode[0]=='6' && zipCode[1]=='0' && zipCode[2]=='5')
cout<<"shipping charge 25$ ";
if(zipCode[0]=='6' && zipCode[1]=='0' && zipCode[2]=='6')
cout<<"shipping charge 30$ ";
}
else
cout << INVALID_MSG << endl << endl;
//end if
cout << "Five-character ZIP code (-1 to end): ";
cin >> zipCode;
} //end while
//system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.