jeudi 1 janvier 2015

C++ Do/While loop not working [on hold]

I have a practice project I'm working on that a friend gave me to learn C++ and I've ran into a little road block in completing the project. The project teaches loops and if/else statements. With the project, I'm supposed to get a response from the user about each successive question that would help determine the proper calculation for the program to do and then output it. If the user inputs garbage or a response that does not fit into the choices, I'm supposed to test the validity of the input, respond with an error message, and have the user try again until the user inputs a choice that is given. I have a loop that is giving me problems though, the user is asked if they want to ship a parcel and once all the questions about the parcel are given, a calculation is done and the user is asked if they want to ship any other packages. I can't seem to get the program to work right if the user inputs yes to do the program again. What is the best way to make it loop the entire program when the user inputs yes? I'll apologize ahead of time for the length of the code.



#include <iostream>
#include <string>
#include <locale>
using namespace std;

int main()
{
string yes = "y";
string uyes = "Y";
string no = "n";
string uno = "N";
string answer = " ";
string standard = "st";
string express = "ex";
string sameday = "sd";
string shipanswer = " ";
string weight = "w";
string numofitems = "noi";
string wornoinum = " ";
int wnumber = 0;
int noinumber = 0;
double wcost = 0;
double noicost = 0;
string againanswer = " ";
//do //Have this test if someone wants to do this again.
//{
//system("CLS");
do //put this at the top to test the answer. If the answer is bogus, have it repeat.
{
system("CLS");
cout << "Welcome to the Red Fern Shipping Company where we ship your packages your way!\n";
cout << "Do you want to ship a parcel? Y or N?\n";
cin >> answer;
if (answer == yes || answer == uyes)
{
do
{
do
{
cout << "How would you like your package shipped? Type 'st' for standard, 'ex' for express, and 'sd' for same day delivery.\n";
cin >> shipanswer;
if (shipanswer == standard)
{
do
{
cout << "Will this package go to Alaska or Hawaii? 'y' or 'n'?\n";
cin >> answer;
if (answer == yes || answer == uyes)
{
do
{
cout << "Would you like your packages shipped by weight or number of items?\n";
cout << "Enter 'noi' for number of items and 'w' for weight.\n";
cin >> wornoinum;
if (wornoinum == weight)
{
do
{
cout << "What is the weight of the parcel?\n";
cin >> wnumber;
if (!wnumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
wcost = (wnumber * 1.45) + 2.50;
cout << wcost << endl;
}
} while (!wnumber);
}
else if (wornoinum == numofitems)
{
do
{
cout << "How many items do you have?\n";
cin >> noinumber;
if (!noinumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
noicost = (noinumber * 3) + 2.50;
cout << noicost << endl;
}
} while (!noinumber);
}
else
{
cin.clear();
cin.sync();
cout << "That is an invalid entry. Please enter 'noi' or 'w' as explained above.\n";
}
} while (wornoinum != numofitems && wornoinum != weight);
}
else if (answer == no || answer == uno)
{
do
{
cout << "Would you like your packages shipped by weight or number of items?\n";
cout << "Enter 'noi' for number of items and 'w' for weight.\n";
cin >> wornoinum;
if (wornoinum == weight)
{
do
{
cout << "What is the weight of the parcel?\n";
cin >> wnumber;
if (!wnumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
wcost = (wnumber * 1.45);
cout << wcost << endl;
}
} while (!wnumber);
}
else if (wornoinum == numofitems)
{
do
{
cout << "How many items do you have?\n";
cin >> noinumber;
if (!noinumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
noicost = (noinumber * 3);
cout << noicost << endl;
}
} while (!noinumber);
}
else
{
cin.clear();
cin.sync();
cout << "That is an invalid entry. Please enter 'noi' or 'w' as explained above.\n";
}
} while (wornoinum != numofitems && wornoinum != weight);
}
else
{
cin.clear();
cin.sync();
cout << "This is an invalid entry. Please try again.\n";
}
} while (answer != yes && answer != no && answer != uyes && answer != uno);
}
else if (shipanswer == express)
{
do
{
cout << "Will this package go to Alaska or Hawaii? 'y' or 'n'?\n";
cin >> answer;
if (answer == yes || answer == uyes)
{
do
{
cout << "Would you like your packages shipped by weight or number of items?\n";
cout << "Enter 'noi' for number of items and 'w' for weight.\n";
cin >> wornoinum;
if (wornoinum == weight)
{
do
{
cout << "What is the weight of the parcel?\n";
cin >> wnumber;
if (!wnumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
wcost = (wnumber * 2.5) + 5;
cout << wcost << endl;
}
} while (!wnumber);
}
else if (wornoinum == numofitems)
{
do
{
cout << "How many items do you have?\n";
cin >> noinumber;
if (!noinumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
noicost = (noinumber * 4) + 5;
cout << noicost << endl;
}
} while (!noinumber);
}
else
{
cin.clear();
cin.sync();
cout << "That is an invalid entry. Please enter 'noi' or 'w' as explained above.\n";
}
} while (wornoinum != numofitems && wornoinum != weight);
}
else if (answer == no || answer == uno)
{
do
{
cout << "Would you like your packages shipped by weight or number of items?\n";
cout << "Enter 'noi' for number of items and 'w' for weight.\n";
cin >> wornoinum;
if (wornoinum == weight)
{
do
{
cout << "What is the weight of the parcel?\n";
cin >> wnumber;
if (!wnumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
wcost = (wnumber * 2.5);
cout << wcost << endl;
}
} while (!wnumber);
}
else if (wornoinum == numofitems)
{
do
{
cout << "How many items do you have?\n";
cin >> noinumber;
if (!noinumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
noicost = (noinumber * 4);
cout << noicost << endl;
}
} while (!noinumber);
}
else
{
cin.clear();
cin.sync();
cout << "That is an invalid entry. Please enter 'noi' or 'w' as explained above.\n";
}
} while (wornoinum != numofitems && wornoinum != weight);
}
else
{
cin.clear();
cin.sync();
cout << "This is an invalid entry. Please try again.\n";
}
} while (answer != yes && answer != no && answer != uyes && answer != uno);
}
else if (shipanswer == sameday)
{
do
{
cout << "Will this package go to Alaska or Hawaii? 'y' or 'n'?\n";
cin >> answer;
if (answer == yes || answer == uyes)
{
do
{
cout << "Would you like your packages shipped by weight or number of items?\n";
cout << "Enter 'noi' for number of items and 'w' for weight.\n";
cin >> wornoinum;
if (wornoinum == weight)
{
do
{
cout << "What is the weight of the parcel?\n";
cin >> wnumber;
if (!wnumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
wcost = (wnumber * 3) + 8;
cout << wcost << endl;
}
} while (!wnumber);
}
else if (wornoinum == numofitems)
{
do
{
cout << "How many items do you have?\n";
cin >> noinumber;
if (!noinumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
noicost = (noinumber * 5.5) + 8;
cout << noicost << endl;
}
} while (!noinumber);
}
else
{
cin.clear();
cin.sync();
cout << "That is an invalid entry. Please enter 'noi' or 'w' as explained above.\n";
}
} while (wornoinum != numofitems && wornoinum != weight);
}
else if (answer == no || answer == uno)
{
do
{
cout << "Would you like your packages shipped by weight or number of items?\n";
cout << "Enter 'noi' for number of items and 'w' for weight.\n";
cin >> wornoinum;
if (wornoinum == weight)
{
do
{
cout << "What is the weight of the parcel?\n";
cin >> wnumber;
if (!wnumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
wcost = (wnumber * 3);
cout << wcost << endl;
}
} while (!wnumber);
}
else if (wornoinum == numofitems)
{
do
{
cout << "How many items do you have?\n";
cin >> noinumber;
if (!noinumber)
{
cin.clear();
cin.sync();
cout << "This entry will not work. Please enter a positive whole number.\n";
}
else
{
noicost = (noinumber * 5.5);
cout << noicost << endl;
}
} while (!noinumber);
}
else
{
cin.clear();
cin.sync();
cout << "That is an invalid entry. Please enter 'noi' or 'w' as explained above.\n";
}
} while (wornoinum != numofitems && wornoinum != weight);
}
else
{
cin.clear();
cin.sync();
cout << "This is an invalid entry. Please try again.\n";
}
} while (answer != yes && answer != no && answer != uyes && answer != uno);
}
else
{
cin.clear();
cin.sync();
cout << "That is an invalid entry. Please try again.\n";
}
} while (shipanswer != standard && shipanswer != express && shipanswer != sameday);
cout << "Would you like to ship something else?\n";
cin >> againanswer;
if (againanswer == no || againanswer == uno)
{
cout << "Thank you for doing business with us! We look forward to serving you and your shipping needs soon!\n";
}
else if (againanswer == yes || againanswer == uyes)
{
cout << "Great!\n";
}
else
{
do
{
cin.clear();
cin.sync();
cout << "That is an invalid response. Please try again.\n";
} while (againanswer != yes && againanswer != uyes && againanswer != no && againanswer != uno);
}
} while (againanswer == yes && againanswer == uyes);
}
else if (answer == no || answer == uno)
{
cout << "Have a nice day! Thanks for doing business with us!\n";
break;
system("PAUSE");
return 0;
}
else
{
cin.clear();
cin.sync();
cout << "That is not a valid entry. Please try again.\n";
}
} while (answer != yes && answer != no && answer != uyes && answer != uno);
system("PAUSE");
return 0;
}

Aucun commentaire:

Enregistrer un commentaire