Thread: program using switch and looping (noob error)

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    8

    program using switch and looping (noob error)

    Hello,
    I'm trying to write my first program but having a hard time figuring out how to put together :
    1) get specific student id to output specific total amount
    2) make this simpler?

    Please help me. Correct me so that I can learn.
    FYI- I'm not taking any class. Just learning it via online sources, and asking people around for their hws.

    I'm supposed to come up with a program that compute and print the amount of funds raised by that student using a switch to determine the profit.
    -outputs the running total and the final totals.


    this is what I have:
    (WARNING----this code is ugly )
    but had no lucks. Online tutorials are limited and I don't know where to seek help besides here. I would genuinely appreciate it if you can guide me through. Thanks
    __________________________________________________ ______
    Code:
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    
    
    #define FundName "Allen's Fundraiser Program"   // Fundraise program name
    #define Starbucks 1.00             // Cost of one Starbucks card
    #define Togos 1.50             // Cost of one Togos card
    #define Chipotle 2.00             // Cost of one Chipotle card
    #define S Starbucks         // assign S as Starbucks
    #define s Starbucks       // assign s as Starbucks
    #define T Togos           // assign T as Togos
    #define t               // assign t as Togos
    #define C Chipotle       // assign C as Chipotle
    #define c Chipotle      // assign c as Chipotle
    
    
    int main()
    {
        int studentid[4]{11111,22222,33333,44444},      // student ID number
            cardtype,      // Type of card ordered
            qtycard,      // Quantity of card ordered
                
        double subsb,   // Subtotal of starbucks ordered 
               subtgs,   // Subtotal of togos ordered
               subchp,   // Subtotal of chipotle ordered
               total;     // Order total
        char runprog;     // Run program yes/no flag
        
         // Calculate the order totals
            subsb = qtycard * Starbucks;
            subtgs = qtycard * Togos;
            subchp = qtycard * Chipotle;
            total = subsb + subtgs + subchp;
    
    
        cout << fixed << setprecision(2); // Sets real number decimal place display
    
    
        cout << FundName << " Fundraising Program\n";
    
    
        // Begin order entry do-while loop
        do
        {
            // Input the customer order
            cout << "\nPlease enter the student ID number: ";
            cin >> studentid;
        
        switch (studentid)
        {
            case 1:
                studentid = 11111;
                cout <<"\nPlease enter the type of card sold:" << "\n"; 
                cin >> cardtype;
                cout << "Please enter number of cards sold: ";
                cin >> qtycard;
                if (cardtype == S,s)
                {
                cout << setw(4) << qtycard << "   Starbucks Card   " << Starbucks
                << setw(13) << subsb << "\n";
                }
                else
                {
                cout << "Student did not purchase this card"<<"\n";    
                }
                break;
            case 2:
                studentid = 22222;
                cout <<"\nPlease enter the type of card sold:" << "\n"; 
                cin >> cardtype;
                cout << "Please enter number of cards sold: ";
                cin >> qtycard;
                if (cardtype == C,c)
                {
                cout << setw(4) << qtycard << "   Chipotle Card   " << Chipotle
                << setw(13) << subchp << "\n";
                }
                else
                {
                cout << "Student did not purchase this card"<<"\n";
                }
                break;
            case 3:
                studentid = 33333;
                cout <<"\nPlease enter the type of card sold:" << "\n"; 
                cin >> cardtype;
                cout << "Please enter number of cards sold: ";
                cin >> qtycard;
                if (cardtype == T,t)
                {
                cout << setw(4) << qtycard << "   Togo's Card   " << Togos
                << setw(13) << subtgs << "\n";
                }
                else
                {
                cout << "Student did not purchase this card"<<"\n";
                }
                break;
            case 4:
                studentid = 44444;
                cout <<"\nPlease enter the type of card sold:" << "\n"; 
                cin >> cardtype;
                cout << "Please enter number of cards sold: ";
                cin >> qtycard;
                if (cardtype == C,c)
                {
                cout << setw(4) << qtycard << "   Chipotle card   " << Chipotle
                << setw(13) << subchp << "\n";
                }
                else
                {
                cout << "Student did not purchase this card"<<"\n";
                }
                break;
            default:
                cout << "\n  <!> Sorry, ID is invalid.";
        }
    
    
           
    
    
            // Output the order invoice
            cout << "\n";
            cout << "         " << FundName << "\n";
            cout << "               Order Invoice\n\n";
            cout << "   Student ID: " << studentid << "\n";
            cout << "  -------------------------------------------\n";
            cout << "  " << setw(4) << qtycard << "   Starbucks Card  @ " << Starbucks
              << setw(13) << subsb << "\n";
            cout << "  " << setw(4) << qtycard << "    Togo's Card @ " << Togos
              << setw(13) << subtgs << "\n";
            cout << "  " << setw(4) << qtycard << "    Chipotle Card @ " << Chipotle
              << setw(13) << subchp << "\n";  
            cout << "  -------------------------------------------\n";
            cout << "                          Total $" << setw(11) << total << "\n\n";
            cout << "           Thank you for your order!\n\n";
    
    
    
    
            // Prompt to enter another order
            cout << "Do you have a student sale to enter? [Y/N]? ";
            cin >> runprog;
        } while (runprog == 'y' || runprog == 'Y');
        // End do-while loop
    
    
        cout << "\n";
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Don't use the pre-processor for constants. In C++ either use const or constexpr if you have a modern compiler

    Code:
    #define FundName "Allen's Fundraiser Program" 
    const string FundName = "Allen's Fundraiser Program";
    
    #define Starbucks 1.00             // Cost of one Starbucks card
    const double Starbucks = 1.00;
    Don't encrypt your code like this:

    #define S Starbucks // assign S as Starbucks
    #define s Starbucks // assign s as Starbucks
    #define T Togos // assign T as Togos
    #define t // assign t as Togos
    #define C Chipotle // assign C as Chipotle
    #define c Chipotle // assign c as Chipotle

    Imagine you start a new project and come back to this later in a week. Will you still remember what if (cardtype == S,s) means?

    Much better to use consts or enums instead.
    const int STARBUCKS = 1;
    if (cardtype == STARBUCKS) is much more obvious.

    Might be good idea to get a good book about C++.
    Have a look at this list:
    c++ faq - The Definitive C++ Book Guide and List - Stack Overflow

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Delete (or comment out) cases 2,3,4 until you've managed to get case 1 compiling and working.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2018
    Posts
    8
    I've tried. Only problem is that I can figure out what I'm doing wrong.Which is a big problem.
    I need guidance but limited by text. I came here so that I can get a bit of help.

  5. #5
    Registered User
    Join Date
    Mar 2018
    Posts
    8
    I'm limited to only #define. But thank you.
    Your explanation made it easier for me to understand.

    I have also noticed that I've used array for storing ids but did not use it on switch statement.
    I was wondering if the way I wrote it is correct? Or is there a different way of storing multiple variables, ids, etc then calling it out later?

    What else am I doing wrong here?

    Thank you for the book list.

  6. #6
    Guest
    Guest
    Hello nedlig. I don't fully understand the intent behind the studentid variable, but If you just wanted a way to differentiate between several students using a number, you would simply use an int, e.g.
    Code:
    int student_id;
    cout << "\nPlease enter the student ID number: ";
    cin >> student_id;
    
    switch (student_id)
    {
    case 1:
        // Do stuff for student 1
        break;
    case 2:
        // Do stuff for student 2
        break;
    default:
        cout << "\n  <!> Sorry, ID is invalid.";
    }
    And then build your program up from there.

  7. #7
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    It's not easy to guess what this switch statement is for since you do basically the same of every studentid.
    Have a look at this code. Maybe it gives you some ideas. I have tried to keep as simple as possible since you are not allowed to use 'real' C++ like enum, const...
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    #define FundName "Allen's Fundraiser Program"
    #define STARBUCKS_COST 1.00
    #define TOGOS_COST 1.50
    #define CHIPOTLE_COST 2.00
    
    #define STARBUCKS_ID 1
    #define TOGOS_ID 2
    #define CHIPOTLE_ID 3
    
    int main()
    {
      int studentids[4] = { 11111,22222,33333,44444 };      // student ID number
      int cardtype;   // Type of card ordered
      int cardQuantity;      // Quantity of card ordered
      int id;
      
      char runprog;     // Run program yes/no flag
    
      std::cout << std::fixed << std::setprecision(2); // Sets real number decimal place display
    
      std::cout << FundName << "\n";
    
      // Begin order entry do-while loop
      do
      {
        double subtotalStarbucks = 0.0;
        double subtotalTogos = 0.0;
        double subtotalChipotle = 0.0;
        double total = 0;     // Order total
        int numStarbucks = 0;
        int numTogos = 0;
        int numChipotle = 0;
        std::cout << "\nPlease enter the student ID number(1-4): ";
        std::cin >> id;
    
        // TO DO check that id is valid
        std::cout << "\nPlease enter the type of card sold (Starbucks = 1, Togos = 2, Chipotle = 3): ";
        std::cin >> cardtype;
        // TO DO check that type is valid
        std::cout << "Please enter number of cards sold: ";
        std::cin >> cardQuantity;
        switch (cardtype)
        {
        case STARBUCKS_ID:
          numStarbucks = cardQuantity;
          subtotalStarbucks += STARBUCKS_COST * cardQuantity;
          break;
        case TOGOS_ID:
          numTogos = cardQuantity;
          subtotalTogos += TOGOS_COST * cardQuantity;
          break;
        case CHIPOTLE_ID:
          numChipotle = cardQuantity;
          subtotalChipotle += CHIPOTLE_COST * cardQuantity;
          break;
        default:
          std::cout << "\n  <!> Sorry, ID is invalid.";
        }
        total = subtotalChipotle + subtotalStarbucks + subtotalTogos;
        // Output the order invoice
        // TO DO fix output
        std::cout << "\n";
        std::cout << "         " << FundName << "\n";
        std::cout << "               Order Invoice\n\n";
        std::cout << "   Student ID: " << studentids[id-1] << "\n";
        std::cout << "  -------------------------------------------\n";
        std::cout << "  " << std::setw(4) << numStarbucks << "   Starbucks Card  @ "
                  << std::setw(13) << subtotalStarbucks <<  "\n";
    
        std::cout << "  " << std::setw(4) << numTogos << "    Togo's Card @ " 
                  << subtotalTogos << "\n";
    
        std::cout << "  " << std::setw(4) << numChipotle << "    Chipotle Card @ " 
                  << subtotalChipotle << "\n";
          
        std::cout << "  -------------------------------------------\n";
        std::cout << "                          Total $" << std::setw(11) << total << "\n\n";
        std::cout << "           Thank you for your order!\n\n";
        // Prompt to enter another order
        std::cout << "Do you have a student sale to enter? [Y/N]? ";
        std::cin >> runprog;
      } while (runprog == 'y' || runprog == 'Y');
      // End do-while loop
    
      std::cout << "\n";
    
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Mar 2018
    Posts
    8
    Thank you. With your help, this is what I came up with.
    My problem now is that I can't get the subtotals to add up. and my total revenue won't work.
    Any suggestions?
    Code:
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    
    
    #define STARBUCKS 1.00 
    #define TOGOS 1.50            
    #define CHIPOTLE 2.00           
    
    
    
    
    int main()
    { 
    
    
        int yes_no,
            id_number,
            qty_sb,
            qty_tg,
            qty_chi,
            tot_sb = 0,
            tot_tg = 0,
            tot_chi = 0,
            tot_qty,
            i,
            k = 0;
            
        char type_card;
        
        double sub_total,
               sb_subtotal,
               tg_subtotal,
               chi_subtotal,
               tot_sbprofit,
               tot_tgprofit,
               tot_chiprofit,
               total,
               profit_total=0;
               
        
        cout << "Do you have a student sale to enter? (1=yes, 0=no)?  ";
        cin >> yes_no;
        
        while (yes_no == 1)
        {
            id_number = k;
            cout << "\nPlease enter the student ID number:\t\t\t\t" << fixed << setw(8);
            cin >> id_number;
        
            cout << "Please enter the type of card sold\n";
            cout << "(S for Starbucks, T or Togo's, and C for Chipotle):\t\t" << fixed << setw(8); 
            cin >> type_card;
        
            
            switch (type_card)
            {
                case 'S':
                case 's':
                    cout << "Please enter number of cards sold:\t\t\t\t" << fixed << setw(8);
                    cin >> qty_sb;
                    sb_subtotal = qty_sb * STARBUCKS;
                    cout << "\nStudent " << id_number << " raised the following amount for the school:" << fixed << setprecision(2) << setw(12) << sb_subtotal << "\n"; break;
                
                case 'T':
                case 't':
                    cout << "Please enter number of cards sold:\t\t\t\t" << fixed << setw(8);
                    cin >> qty_tg;
                    tg_subtotal = qty_tg * TOGOS;
                    cout << "\nStudent " << id_number << " raised the following amount for the school:" << fixed << setprecision(2) << setw(12) << tg_subtotal << "\n"; break;
                
                case 'C':
                case 'c':
                    cout << "Please enter number of cards sold:\t\t\t\t" << fixed << setw(8);
                    cin >> qty_chi;
                    chi_subtotal = qty_chi * CHIPOTLE;
                    cout << "\nStudent " << id_number << " raised the following amount for the school:" << fixed << setprecision(2) << setw(12) << chi_subtotal << "\n"; break;
            }
        k++ ;
            
        cout << "Do you have a student sale to enter? (1=yes, 0=no)?  ";
        cin >> yes_no;   
        }
        
        if (k != 0)
        {
            
        cout << "\n**********************End of Run Report*******************";
        cout << "\nTotal number of starbucks gift cards sold\t" << qty_sb;
        cout << "\nTotal number of Togo's gift cards sold\t" << qty_tg;
        cout << "\nTotal number of Chipotle gift cards sold\t" << qty_chi;
        cout << "\nTotal number of all gift cards sold\t" << tot_qty;    
        cout << "\nTotal revenue from Starbucks gift cards\t" << tot_sbprofit;
        cout << "\nTotal revenue from Togo's gift cards\t" << tot_tgprofit;
        cout << "\nTotal revenue from Chipotle gift cards\t" << tot_chiprofit;
        cout << "\nTotal revenue raised\t\t" << profit_total;
        
        for (i = 0; i < k; i++)
        {
            
        tot_sb += qty_sb;
        tot_tg += qty_tg;
        tot_chi += qty_chi;
        total += sb_subtotal + tg_subtotal + chi_subtotal;   
        profit_total += total;
        }
    
    
        return 0;
    }
    }

  9. #9
    Guest
    Guest
    If possible, you should try to break down your program into smaller problems. Having ~20 variables in the same scope makes things very hard to keep track of. Better names would help too. E.g. "k" is not descriptive at all.

    Initialize your variables, especially those that might not be used. What if no student sells Togos cards?
    Code:
    qty_sb = 0,
    qty_tg = 0,
    qty_chi = 0,
    You should also be consistent in your indentation, for instance the latter section should look more like this:
    Code:
        // ...
    
        if (k != 0)
        {
            cout << "\n**********************End of Run Report*******************"; // Moves below the loop I assume?
            cout << "\nTotal number of starbucks gift cards sold\t" << qty_sb;
            cout << "\nTotal number of Togo's gift cards sold\t" << qty_tg;
            cout << "\nTotal number of Chipotle gift cards sold\t" << qty_chi;
            cout << "\nTotal number of all gift cards sold\t" << tot_qty;
            cout << "\nTotal revenue from Starbucks gift cards\t" << tot_sbprofit;
            cout << "\nTotal revenue from Togo's gift cards\t" << tot_tgprofit;
            cout << "\nTotal revenue from Chipotle gift cards\t" << tot_chiprofit;
            cout << "\nTotal revenue raised\t\t" << profit_total;
    
            for (i = 0; i < k; i++)
            {
                tot_sb += qty_sb; // Adds starbucks cards for each time you entered anything (raised "k")?
                tot_tg += qty_tg;
                tot_chi += qty_chi;
                total += sb_subtotal + tg_subtotal + chi_subtotal;
                profit_total += total;
            }
    
            return 0; // This return looks misplaced with correct indentation
        }
    } // end of main()
    Divide and conquer, test one section at a time, fix one problem at a time.
    Last edited by Guest; 03-29-2018 at 10:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch case error in program
    By vead in forum C Programming
    Replies: 3
    Last Post: 01-08-2018, 03:15 AM
  2. Need help looping this switch statement
    By nick753 in forum C++ Programming
    Replies: 12
    Last Post: 01-18-2010, 03:36 AM
  3. Need help looping this switch statement
    By ravens199 in forum C++ Programming
    Replies: 29
    Last Post: 01-16-2008, 03:06 PM
  4. Looping a Switch Case
    By dan g in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2007, 06:36 PM
  5. Please find an error in my noob program
    By alexpos in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 02:55 PM

Tags for this Thread