Hi to DaniWeb. I've been checking out the forums from time to time to get help with C++. I'm a total beginner in C++, the only other language I have any knowledge of would be HTML, which is getting old. Anyways, I'm in a class that is requiring an assigment using the switch. When using the switch, can the statements in the case statements be whatever code statements you want executed if that respective choice is selected, or just simple cout statments?, (this is what I have in mind)

switch(process_choice)
    {    
    case 1:                    // menu choice 1 - process transaction selected
        customer_no++;        // customer_no accum
        cout << "Customer #: "<< customer_no << endl;        // display Customer number/transaction
        cout << endl;

        cout << "Please enter customer type (I-Internet, L-Local) : " << endl;        // Select Customer Type I or L
        cin >> customer_type;
        cout << endl;
        
        switch(customer_type)    // decision making process for customer type (discout or shipping charges)
        {
            case 'i':
            case 'I':
                if(total_sales_int < 50.00)
                    {
                        shipping = 3.00;                                    // declaration of shipping charge for this sale amount
                        final_charge_int = total_sales_int + shipping;        // final charge for internet customer 
                    }
                else
                        shipping = 0.00;
                break;
            case 'L':
            case 'l':
                
                /* Loop for local customer discount calculation BEGIN */
                if(tot_chg_pre_disc > 60.00)
                {final_charge_loc =  (tot_chg_pre_disc + (tot_chg_pre_disc * DISC_10));}
                if((tot_chg_pre_disc > 30.00) && (tot_chg_pre_disc < 59.00))
                {final_charg_loc = (tot_chg_pre_disc + (tot_chg_pre_disc * DISC_5));}
                break;
                /* Loop for local customer discount calculation END */
            default:
                cout << endl << endl;
                cout << "You Entered an Incorrect Response. Try Again!" << endl;
                break;
        }
    case 2:
        cout << "you really don't know what you're doing" << endl;
        break;
    default:
        cout << "You Entered an Incorrect Response. Try Again!" << endl;
        break;
        }

If I should add any more information to my post please feel free to tell me. I'm a total novice at this. I just need a few pointers. The assignment deals with decision making, focusing on using the if, if-else, and switch statements. We are to write a program that displays a menu that asks us to either process Local or Internet customers (with a local discount program & and a shipping charge for Internet customers) or to display an overall report. The products sold are 4 books. I don't know why I just wrote all that, maybe to give whoever reads that part of the code an idea. I'm not looking for step by step help or anything like that, just if whats in my code is correct, almost correct, way off, or whatever you know. Enough rambling.

Any help would be appreciated. Thanks

TG

Hi to DaniWeb. I've been checking out the forums from time to time to get help with C++. I'm a total beginner in C++, the only other language I have any knowledge of would be HTML, which is getting old. Anyways, I'm in a class that is requiring an assigment using the switch. When using the switch, can the statements in the case statements be whatever code statements you want executed if that respective choice is selected, or just simple cout statments?, (this is what I have in mind)

switch(process_choice)
    {    
    case 1:                    // menu choice 1 - process transaction selected
        customer_no++;        // customer_no accum
        cout << "Customer #: "<< customer_no << endl;        // display Customer number/transaction
        cout << endl;
 
        cout << "Please enter customer type (I-Internet, L-Local) : " << endl;        // Select Customer Type I or L
        cin >> customer_type;
        cout << endl;
 
        switch(customer_type)    // decision making process for customer type (discout or shipping charges)
        {
            case 'i':
            case 'I':
                if(total_sales_int < 50.00)
                    {
                        shipping = 3.00;                                    // declaration of shipping charge for this sale amount
                        final_charge_int = total_sales_int + shipping;        // final charge for internet customer 
                    }
                else
                        shipping = 0.00;
                break;
            case 'L':
            case 'l':
 
                /* Loop for local customer discount calculation BEGIN */
                if(tot_chg_pre_disc > 60.00)
                {final_charge_loc =  (tot_chg_pre_disc + (tot_chg_pre_disc * DISC_10));}
                if((tot_chg_pre_disc > 30.00) && (tot_chg_pre_disc < 59.00))
                {final_charg_loc = (tot_chg_pre_disc + (tot_chg_pre_disc * DISC_5));}
                break;
                /* Loop for local customer discount calculation END */
            default:
                cout << endl << endl;
                cout << "You Entered an Incorrect Response. Try Again!" << endl;
                break;
        }
    case 2:
        cout << "you really don't know what you're doing" << endl;
        break;
    default:
        cout << "You Entered an Incorrect Response. Try Again!" << endl;
        break;
        }

If I should add any more information to my post please feel free to tell me. I'm a total novice at this. I just need a few pointers. The assignment deals with decision making, focusing on using the if, if-else, and switch statements. We are to write a program that displays a menu that asks us to either process Local or Internet customers (with a local discount program & and a shipping charge for Internet customers) or to display an overall report. The products sold are 4 books. I don't know why I just wrote all that, maybe to give whoever reads that part of the code an idea. I'm not looking for step by step help or anything like that, just if whats in my code is correct, almost correct, way off, or whatever you know. Enough rambling.

Any help would be appreciated. Thanks

TG

Hello and welcome,

Yes any code before the break of a given case in a switch condition can contain any code you like. The nature of switch is to continue executing either until a break is encountered or the end of the switch condition which is ended with the "}" character. For example you can do something like this:

char id = 'a';
switch (a) {
   case 'a' :
       // This serves no purpose but it is valid
       switch (id) {
           case 'a' :
                cout << "It is a!!!!" << endl;
                break;
       }
       break;
   case 'b' :
       cout << "It is b!!!!" << endl;
       break;
   case 'c' :
       cout << "It is c!!!!" << endl;
       break;
   default : // this is optional
       cout << "It is nothing!!!! :(" << endl;
       break;
}

Good luck, LamaBot

Wow, that extremely quick. Thanks a lot for that. Anyways, I'll definitely keep that in mind as I'm working on it. If I run into any problems, hope not, but I know I probably will I'll check back in. Thanks again.

A switch is basically a nice clean version of an if-elseif-else chain. Consider the following:

switch(youVar)
{
  case 0:
    foo();
    // do some more stuff
    break;
  case 1:
    // do stuff
    break;
  case 2:
    // do more stuff
    break;
  default:
    // handle accordingly
    break; // not so important for the last one
}

This code is logically the same as above:

if(yourVar == 0) {
  foo();
  // do some more stuff
} else if(yourVar == 1) {
  // do stuff
} else if(yourVar == 2) {
  // do more stuff
} else /* default case */ {
  // handle accordingly
}

There's an intricacy to switches involving case fall-through, but we won't go there yet ;)

Thanks for that information. Much appreciated.
TG

i think I need more help, but I don't know whether to post my question here or to start a new thread? I'm gonna do both, sorry in advance if i'm not supposed to do that. Thanks again.

Anyways, I have the bulk of the code done, I just need a few tweaks on the coding, or at least I think. All the output is fine, except that I don’t know if I’m setting up the counters & accumulators correctly. When I process the first transaction, the totals don’t calculate correctly in the final reports and in the current sale report. I don’t know what to do. Here is my code.

// tgiddens_assig2.cpp
  // Assignment 2 Decision Making
  // Timothy Giddens
  // IS 2043.002 Spring 07
   
   
  #include <iostream>
  #include <iomanip>
  #include <cstdlib>
   
  using namespace std;
   
  int main()
  {
         // individual book price constants
         const double PR_LASSI = 7.25,     
                              PR_ROBIN = 9.75,
                              PR_OLIVE = 6.75,
                              PR_LITTL = 5.50,
                              DISC_5 = 0.05,      // %5 discount for locals
                              DISC_10 = 0.10;     // %10 discount for locals
   
         // integer counter variables
         int count_customer_no = 0,
                count_total_books = 0,
                count_lassie_bk = 0,
                count_robin_bk = 0,
                count_olive_bk = 0,
                count_littl_bk = 0;
         
         // Display menu selector   
         int selection;                           // user enters 1 or 2
   
   
         // various charges variables
         double sales_las = 0.00,
                   sales_rob = 0.00,
                   sales_oli = 0.00,
                   sales_lit = 0.00,
                   grand_total_sales = 0.00,
                   total_sales = 0.00,
                   tot_chg_pre_disc = 0.00,
                   final_charge = 0.00,
                   discount = final_charge - total_sales,
                   shipping = 0.00;               //shipping charge for internet users
   
         // selector variables
         char response = ' ',       // main menu selector input; user enters 'Y'
                 book_selection = ' ',     // book menu switch selector input; user enters 'Y'
                 book_response = ' ',
                 select_las = '1',
                 select_rob = '2',
                 select_oli = '3',
                 select_lit = '4',
                 customer_type;
         
         // set output display format
         cout << setprecision(2)
                 << setiosflags(ios::fixed)
                 << setiosflags(ios::showpoint);
         do
         {
         // display welcome menu & recv input for menu choice (1 or 2)
         cout << "Welcome to Johnson's Bookstore" << endl;
         cout << "==============================" << endl;
         cout << "1 - Process Transaction" << endl;
         cout << "2 - Display Overall Report" << endl << endl;
         cout << "Please enter your selection: ";
         cin >> selection;
         cout << endl;
                
         //decision making process for menu choice and sub menu transactions
         if(selection == 1)
                {      
                       // menu choice 1 - process transaction selected
                       count_customer_no++;       // customer_no accum
                       cout << "Customer #: "<< count_customer_no << endl;           // display Customer number/transaction
                       cout << endl;
   
                       cout << "Please enter customer type (I-Internet, L-Local) : ";             // Select Customer Type I or L
                       cin >> customer_type;
                       cout << endl;
   
                       switch(customer_type)      // decision making process for customer type (discout or shipping charges)
                       {
                             case 'i':
                             case 'I':
                                    /* internet customer shipping calculations BEGIN */
                                    if(total_sales < 50.00)
                                           {
                                                  shipping = 3.00;                                                            // declaration of shipping charge for this sale amount
                                                  final_charge = total_sales + shipping;          // final charge for internet customer 
                                           }
                                    else
                                                  shipping = 0.00;
                                    break;
                                    /* internet customer shipping calculations END */
                             case 'L':
                             case 'l':
                                    
                                    /* local customer discount calculation BEGIN */
                                    if(total_sales > 60.00)
                                           {
                                           final_charge = (total_sales - (total_sales * DISC_10));
                                           }
                                    else if((total_sales > 30.00) && (total_sales < 59.99))
                                           {
                                           final_charge = (total_sales - (total_sales * DISC_5));
                                           }
                                    break;
                                    /* local customer discount calculation END */
                             default:
                                    cout << endl;
                                    cout << "Incorrect Customer Type! Try Again!" << endl;
                                    break;
                       }
                do                                       // loop for displaying books
                {
                // display list of books
                cout << "Available Titles" << endl;
                cout << "================" << endl;
                cout << "1 - Lassie ($7.25)" << endl;
                cout << "2 - Robinson Crusoe ($9.75)" << endl;
                cout << "3 - Oliver Twist ($6.75)" << endl;
                cout << "4 - Little Women ($5.50)" << endl;
                cout << endl;
                cout << "Please enter your selection: ";
                cin.get();
                book_selection = cin.get();
   
                switch(book_selection)
                       {
                       case '1':
                             cout << "Enter the number of Lassie books sold: ";
                             cin >> count_lassie_bk;
                             break;
                       case '2':
                             cout << "Enter the number of Robinson Crusoe books sold: ";
                             cin >> count_robin_bk;
                             break;
                       case '3':
                             cout << "Enter the number of Oliver Twist books sold: ";
                             cin >> count_olive_bk;
                             break;
                       case '4': 
                             cout << "Enter the number of Little Women books sold: ";
                             cin >> count_littl_bk;
                             break;
                       default:
                             cout << "Incorrect selection! Try Again." << endl;
                             break;
                       }
                
                // Prompt user to continue with another book
                cout << "Do you want to continue with another book? (Y to continue or S to stop): ";
                cin >> book_response;
                cout << endl;
                
                }
                while(book_response == 'Y' || book_response == 'y');
                
                // perform calculations
                sales_las = (count_lassie_bk * PR_LASSI);
                sales_rob = (count_robin_bk * PR_ROBIN);
                sales_oli = (count_olive_bk * PR_OLIVE);
                sales_lit = (count_littl_bk * PR_LITTL);
                count_total_books = (count_lassie_bk + count_robin_bk + count_olive_bk + count_littl_bk);
                total_sales = (sales_las + sales_oli + sales_rob + sales_lit);
                grand_total_sales += total_sales;
                
                // display current sale report
                if(book_response == 's' || book_response == 'S')
                       {
                             cout << "The customer has purchased:" << endl;
                             cout << "(" << count_lassie_bk << ")" << " Lassie" << endl;
                             cout << "(" << count_robin_bk << ")" << " Robinson Crusoe" << endl;
                             cout << "(" << count_olive_bk << ")" << " Oliver Twist" << endl;
                             cout << "(" << count_littl_bk << ")" << " Little Women" << endl << endl;
                             cout << "The sale is: $" << total_sales << endl;
                             cout << "The shipping cost is: $" << shipping << endl;
                             cout << "The discount given is: $" << discount << endl;
                             cout << "The final charge is: $" << final_charge << endl << endl;
                       }
                
                // Prompt user to continue with another transaction
                cout << "Do you want to continue with the program? (Y to continue):";
                cin >> response;
                cout << endl;
                }
         else 
                if(selection == 2)
                {      
                cout << "Final Report:" << endl;
                cout << endl;
                cout << "            Title  Total  Sales" << endl;
                cout << "===============================" << endl;
                cout << "           Lassie" << setw(7) << count_lassie_bk << setw(7) << sales_las << endl;
                cout << "  Robinson Crusoe" << setw(7) << count_robin_bk << setw(7) << sales_rob << endl;
                cout << "     Oliver Twist" << setw(7) << count_olive_bk << setw(7) << sales_oli << endl;
                cout << "     Little Women" << setw(7) << count_littl_bk << setw(7) << sales_lit << endl;
                cout << "===============================" << endl;
                cout << "      Grand Total" << setw(7) << count_total_books << setw(7) << grand_total_sales << endl;
                }
         else
                cout << "You Entered an Incorrect Response. Try Again!" << endl;
         }
   
         while(response == 'Y' || response == 'y');
  return 0;
  }

If this is too much to look over, I understand. I just can’t figure out how to get the totals correctly. Any suggestions would be great.

TG

It's 'cause you're calculating the shipping and total cost before the loop. You need to loop and get all the books before calculating the costs. A little re-arranging will probably fix it up.

[edit:] and for the record, it's better to reuse a thread if it's on topic. Otherwise it'll seem like a double post, which is bad.

cool man, thanks again...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.