0

hi there all , how do i loop this back to the main menu after i have used ctrl+d to give me the final total ...i looked everywhere and tried a few things but no success , my book doesnt say much either
thanks .

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <string>
#include <iomanip>
#include <ctype.h>
using namespace std;

struct CodeInfo
{
string productCode;
double price; // or use double if you wish
double subtotal;
};

int main(int argc, char **argv){


char choice;


cout<< "SELECT DEPARTMENT:\n";
cout<< " 1. Screens:\n" ;
cout<< " 2. Awnings:\n" ;
cout<< " 3. Verticals:\n" ;
cout<< " 4. Rollers:\n";
cout<< " 5. Venetians:\n";
cout<< " 6. for quiting...\n";
cin >> choice;

cout<<"\n";

switch(choice){

case '1':
cout<< "SELECT PRODUCT:\n";
cout<< " 1. Screen Door \n";
cout<< " 2. Security Screen Door\n" ;
cout<< " 3. CrimeSafe\n" ;
cout<< " 6. Quit\n";	
cin >> choice;

}

switch(choice) {

case '1': 
{	

CodeInfo items[3] = { {"rb38ssw500x500", 200},{"rb38ssw600x600", 300},{"rb38ssw700x700", 400}};

double subtotal;

cout<< "SCREEN DOORS\n" ;
cout<< " Please Input Sizes:\n";

do {

string code;
size_t cnt = -1;

cout << "Enter product code (or ctrl-D to exit): ";
cin >> code;

for (size_t i = 0; i < sizeof(items) / sizeof(CodeInfo); i++) {
if (code == items[i].productCode) {
cnt = i;
cout << "setting\n";
} 
}

if (cnt != -1) {

cout << "Price is: " << items[cnt].price << endl;
subtotal += items[cnt].price;
} 

else { 
cout << "\nSubtotal = " << subtotal << endl; // setprecision(2) << subtotal << endl;
break;
}

} while (!cin.eof());

return 0;

} 


case '2' :
{	

CodeInfo items[3] = { {"ssd500x500", 200},{"ssd600x600", 300},{"ssd700x700", 400}};


double subtotal;

cout<< "SECURITY SCREEN DOORS\n" ;
cout<< " Please Input Sizes:\n";
do {
string code;
size_t cnt = -1;

cout << "Enter product code (or ctrl-D to exit): ";
cin >> code;

for (size_t i = 0; i < sizeof(items) / sizeof(CodeInfo); i++) {
if (code == items[i].productCode) {
cnt = i;
cout << "setting\n";
}
}

if (cnt != -1) {

cout << "Price is: " << items[cnt].price << endl;
subtotal += items[cnt].price;
} 

else { 
cout << "\nSubtotal = " << subtotal << endl; // setprecision(2) << subtotal << endl;
break;
}

} while (!cin.eof()); }

return 0;

}	


}

sorry if looks messy , im still learning

2
Contributors
1
Reply
4
Views
8 Years
Discussion Span
Last Post by Murtan
1

Note 1: Indenting is your friend, make much better use of him.

Note 2: if you're in your main() and you return, your program is done

I'm not sure what's up with the two-level menu, where you only handle one case from the first menu and two cases from the second menu. (Please at least put place-holders in that say "Not Implemented" so you can see your program structure.

Note 3: functions are your friends too, it is generally bad form to have all of your code in main().

Making the user type the eof character on the keyboard to indicate 'done' is also bad form. A more common implementation would be to quit if the user entered a blank line, or entered "quit" or "done" or some other specified input rather than looking for eof().

Back to your original question, if you want the menu to loop, you have to put it in a loop. Your code as written would need some work, but you could put a do { before you display the menu and put a } while (choice != 6); after all of the menu processing.

Or pre-initialize choice to a zero and make the loop while (choice != 6) { and end with }

Votes + Comments
Say "yes" to indentation!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.