Menu to select 1 of 2 options

This is a discussion on Menu to select 1 of 2 options within the C++ Programming forums, part of the General Programming Boards category; Hi How would I go about creating a menu to select 1 of two options Here is the code Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Smile Menu to select 1 of 2 options

    Hi

    How would I go about creating a menu to select 1 of two options

    Here is the code

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    int main(void)
    {
    char name[30];
    char surname[30];
    char id[30];
    double hourrate;
    char hourratechar[30];
    int numhour;
    char numhourchar[10];
    char taxcredit[10];
    float taxoutput;
    float basicpay;
    float overtime;
    float grosspay;
    float natins;
    float netpay;
    float totalmoney;
    
    cout <<"Please enter your name"<<endl;
    do
    {
    cin.getline(name,40);
     if(strlen(name) > 30)
      {
           cout<<"Error please enter sufficient characters"<<endl;
      }
      }while(strlen(name) > 30);
    
    cout <<"Please enter your surname"<<endl;
    do
    {
    cin.getline(surname,40);
    if(strlen(surname) > 30)
      {
           cout<<"Error please enter sufficient characters"<<endl;
      }
      }while(strlen(surname) > 30);
    
    cout <<"Please enter your ID"<<endl;
    do
    {
    cin.getline(id,15);
    if(strlen(id) > 5)
      {
           cout<<"Error please enter 5 numbers or less"<<endl;
      }
      }while(strlen(id) > 5);
    
    cout <<"Please enter your hourly rate"<<endl;
    cin.getline(hourratechar,30);
    hourrate= atof(hourratechar);
    
    do
    {
    cout <<"Please enter the number of hours you work"<<endl;
    cin.getline(numhourchar,30);
    numhour= atoi(numhourchar);
    if(numhour > 60)
            {
            cout<<"Error please enter 60 hours or less"<<endl;
            }    
     }while(numhour > 60);
    
        if (numhour<=39)
    {
       basicpay=numhour*hourrate;
    }
    else 
    {
       basicpay=39*hourrate;
    }
    
        if (overtime <= 39){
      overtime = 0;
      }
    else 
         if (overtime > 39) {
           overtime=numhour*hourrate*1.5;
           }
                  
    grosspay=basicpay+overtime;
    
    cout <<"Please enter whether you get tax credits or not"<<endl;
    cin.getline(taxcredit,10);
        if (taxcredit[0]== 'y')
        {
            taxoutput=grosspay*0.22;
        }
       else
        {
            taxoutput=grosspay*0.20;
        }
    
    natins=grosspay*0.11;
    
    totalmoney=basicpay+grosspay+overtime+natins+netpay;
    
    netpay=grosspay-taxoutput-natins;
    
    ofstream outfile("result.txt");
    cout <<""<<endl;
    cout <<"The employee entered the following information:"<<endl;
    cout <<""<<endl;
    cout <<"Their name" <<endl<<name;
    
    outfile << "--- Name ---"<<endl;
    outfile <<name; 
    outfile <<endl;
    cout <<""<<endl;
    cout <<""<<endl;
    
    cout <<"Their surname" <<endl<<surname; 
    outfile << "--- Surname ---"<<endl;
    outfile <<surname;
    outfile <<endl;
    cout <<""<<endl;
    cout <<""<<endl;
    
    cout <<"Their ID" <<endl<<id;
    outfile << "---  ID ---"<<endl;
    outfile <<id;
    outfile <<endl; 
    cout <<""<<endl;
    cout <<""<<endl;
    
    cout <<"Their Hour Rate" <<endl<<hourrate; 
    outfile << "--- Hourly Rate ---"<<endl;
    outfile <<hourrate;
    outfile <<endl;
    cout <<""<<endl;
    cout <<""<<endl;
    
    cout <<"Their Working Hours" <<endl<<numhour; 
    outfile << "--- Number of Hours Worked ---"<<endl;
    outfile <<numhour;
    outfile <<endl;
    cout <<""<<endl;
    cout <<""<<endl;
    
    cout <<"Their Basic Pay" <<endl<<basicpay;
    outfile << "--- Basic Pay ---"<<endl;
    outfile <<basicpay;
    outfile <<endl;
    cout <<""<<endl;
    cout <<""<<endl;
    
    cout <<"Their Overtime Pay" <<endl<<overtime;
    outfile << "--- Overtime Pay ---"<<endl;
    outfile <<overtime;
    outfile <<endl;
    cout <<""<<endl;
    cout <<""<<endl;
    
    cout <<"Their Gross Pay" <<endl<<grosspay;
    outfile << "--- Gross Pay ---"<<endl;
    outfile <<grosspay;
    outfile <<endl;
    cout <<""<<endl;
    cout <<""<<endl;
    
    cout <<"Their Tax Credits" <<endl<<taxoutput;
    outfile << "--- Tax Credits ---"<<endl;
    outfile <<taxoutput;
    outfile <<endl;
    cout <<""<<endl;
    cout <<""<<endl;
    
    cout <<"National Insurance" <<endl<<natins;
    outfile << "--- National Insurance ---"<<endl;
    outfile <<natins;
    outfile <<endl;
    cout <<""<<endl;
    cout <<""<<endl;
    
    cout <<"Their Net Pay" <<endl<<natins;
    outfile << "--- Net Pay ---"<<endl;
    outfile <<netpay;
    outfile <<endl;
    cout <<""<<endl;
    cout <<""<<endl;
    
    cout <<"---- The Weekly Totals ----" <<endl;
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"Total Amount of Money" <<endl<<totalmoney;
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"Total Number of Hours" <<endl<<numhour;
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"Total Number of Overtime Hours" <<endl<<overtime;
    cout <<""<<endl;
    cout <<""<<endl;
    
    system("pause");
    }
    I am writing this for a school project

    For option 1 I want to input all of the details and have it output them at the end so from the

    Code:
    cout <<"Please enter your name"<<endl;
    do
    down to the

    Code:
    cout <<"Total Number of Overtime Hours" <<endl<<overtime;
    cout <<""<<endl;
    cout <<""<<endl;

    For option 2 I want to display a text file that the inputs have been saved into

    Here's the code for it

    Code:
    int main () {
      string line;
      ifstream myfile ("result.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
          cout << line << endl;
        }
        myfile.close();
      }
    
      else cout << "Unable to open file";
    I've tried creating a giant IF statement but no luck

    Apologies if this is a lot of info to take in

    Cheers for any help

    Tom Evans

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    well writting an if statement should work, you might be just doing something wrong. the way around that, to prevent a mistake would be to break your large and fairlu detailed code into functions, this makes it more cohesive since it separates the local variable declarations for each part, making it easier to debug in the first place.since you already know which code does what, it would be a simple matter of cut and paste. here's what you would do:

    Code:
    void function1 ()
    {
         /*big block of code for function 1 - relevant local variable declarations*/
    }
    
    
    void function2 ()
    {
         /*code for function 2 - relevant local variable declarations*/
    }
    
    int main ()
    {
        int choice;
        
        while (1)
        {
              printf ("Make your choice, 1 or 2 - 0 quits: ");
              scanf ("%d", &choice);
              
              if (choice == 1)
                 function1 ();
              
              if (choice == 2)
                 function2 ();
                 
              if (choice == 0)
                 break;
                 
        }
        
        return 0;
    }
    notice that you could also use a switch statement instead if you preffered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  4. Lost BIOS menu options?
    By PJYelton in forum Tech Board
    Replies: 3
    Last Post: 11-14-2004, 08:23 AM
  5. Disabling the main frame menu options
    By MPC_Engr in forum Windows Programming
    Replies: 2
    Last Post: 01-22-2003, 09:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21