Hey Guys (and Gals),
I am currently coding a receipt software. If the user gives bad input I have to make them give good input. They cant leave the program until good input is put. The input is Y/N, anything other than that is a wrong input. I know I have to use a "while" statement for this type of problem, i just dont know how. if you can help me I would highly appreciate it. THANKS!!!
Code:#include <iostream> #include <iomanip> #include <fstream> #include <cstdlib> using namespace std; int main() { float saleAmount; const float SALETAX = 0.07; float percentTip; float tipAmount; char input = ' '; cout << "Would you like to calculate a receipt? Y/N : "; cin >> input; switch (toupper(input)) { case 'Y': cout << endl << "WELCOME TO HAPPY TREE FRIENDS RESTAURANT " << endl << endl; cout << "Please enter sale amount: $ "; cin >> saleAmount; cout << "Please enter tip percent: % "; cin >> percentTip; //Receipt cout << endl << setw(49) << "Happy Tree Friends Restaurant Customer Receipt " << endl; cout << endl << setw(43) << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl << endl; cout << setw(38) << "Sale amount.............. " << setprecision(4) << saleAmount << endl; cout << setw(38) << "Tip percent.............. " << percentTip << endl; cout << setw(38) << "Tip amount............... " << percentTip * 100 << endl; cout << setw(38) << "Tax Amount............... " << setprecision(2) << SALETAX << endl; cout <<endl << setw(38) << "Total.................... " << saleAmount + tipAmount + SALETAX << endl; cout << endl << setw(43) << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl; break; case 'N': cout << "Thank you anyway. Please revisit the HAPPY TREE FRIENDS RESTAURANT " << endl; break; } ofstream outData; outData.open("output.txt"); outData << endl << setw(49) << "Happy Tree Friends Restaurant Customer Receipt " << endl; outData << endl << setw(43) << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl << endl; outData << setw(38) << "Sale amount.............. " << setprecision(4)<< saleAmount << endl; outData << setw(38) << "Tip percent.............. " << percentTip << endl; outData << setw(38) << "Tip amount............... " << percentTip * 100 << endl; outData << setw(38) << "Tax Amount............... " << SALETAX << endl; outData <<endl << setw(38) << "Total.................... " << saleAmount + tipAmount + SALETAX << endl; outData << endl << setw(43) << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl; system("PAUSE"); return EXIT_SUCCESS;
Here's a snippet, I'm sure you'll get the idea and build upon it.
Code:#include <iostream> using namespace std; int main() { char keepgoing = 'Y'; cout << "Do you want to continue <Y>es or <N>o : "; cin >> keepgoing; while (keepgoing == 'Y' || keepgoing == 'y') { cout << "\nDo stuff in here....."; cout << "\nDo you want to continue <Y>es or <N>o : "; cin >> keepgoing; } cout << "\n\nNow out of loop..."; }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks