Need help with this while loop, pretty please?

This is a discussion on Need help with this while loop, pretty please? within the C++ Programming forums, part of the General Programming Boards category; >> it cant check the condition because it was never told what number was. Think about it in English. You ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> it cant check the condition because it was never told what number was.
    Think about it in English. You have to check the condition first, then get the information about the salesman, then check the condition again. You want to ask the user for the Salesperson ID just before you check the condition because you don't want to ask for the rest of the information if they choose to terminate. So you should ask for the ID, then check the condition, then get the rest of the data, then ask for the ID, then check the condition, then get the rest of the data... and so on.

    Now just find out where the while loop fits into that sequence (the check condition must be in the while part). Don't be afraid to put code before or after the loop to get it to work correctly.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> vart, the assignment shows an example run where it has to be first.
    That code does ask for the ID first. I would have chosen not to just give away the answer, though.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    thank you. so simple, im an idiot. alright, one more thing, can you tell me why my numbers are not adding up? I will post my code and the output

    the computer commission should be .10 * sales ammount
    the printer commission should be .08 * sales ammount
    the output is wrong in that part.
    also, where would be the best place to put my cout << setiosflags (ios:: fixed | ios::showpoint) << setprecision (2);

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    double commission (double actualsales, double basesales, double compercent);
    //function for the program
    
    
    int main ()
            { //the int main has begun
    
            string name;
            int number;
            double basepay, compsales, printsales, compcom, printcom, totalcom, totalpay;
    
            cout << "Welcome to the APSU Computer Company Commission Statement program!" << endl;
    
            cout << "Please Enter Salesperson's Identification or -999 to terminate." << endl;
            cin >> number;
    
            while (number != -999)
            {//start while loop
    
            cout << "Please Enter Salesperson's Name." << endl;
            cin.ignore (80, '\n');
            getline (cin, name);
    
            cout << "Please Enter Salesman Base Salary." << endl;
            cin >> basepay;
    
            cout << "Please Enter Personal Computer Sales." << endl;
            cin >> compsales;
    
            cout << "Please Enter Printer Sales." << endl;
            cin >> printsales;
    
            printcom = commission (compsales, 5000.00, 0.10);
            compcom = commission (printsales, 2000.00, .08);
    
            totalcom = printcom + compcom;
    
            totalpay = totalcom + basepay;
    
            cout << endl;
            cout << "APSU Computer Company" << endl;
     cout << "Commission Statement" << endl;
            cout << "SalesPerson " << number << endl;
            cout << name <<  endl;
            cout << "****************************************************" << endl;
            cout << "Product" <<  setw(29) << "Sales Amount" << setw(16) << "Commission" << endl;
            cout << "Personal Computers" << setw(18) << compsales << setw (16) << compcom << endl;
            cout << "Printers" << setw(28) << printsales << setw(16) << printcom << endl;
            cout << endl;
            cout << "Total Commission" << setw(36) << totalcom << endl;
            cout << "Base Pay" << setw(44) << basepay << endl;
            cout << "Total Due" << setw(43) << totalpay  << endl;
    
            cout << endl;
            cout << "Please Enter Salesperson's Identification or -999 to terminate." << endl;
            cin >> number;
    
            }//end of while loop
            }//the int main has ended. oh my!
    
    double commission (double actualsales, double basesales, double compercent)
            {//birth of a function
            double comm;
                    if (actualsales > basesales)
                            {
                            comm = actualsales * compercent;
                            //and you get to keep your job :)
                            }
                    else
                            {
                            comm = 0;
                            }
            return comm;
            }//death of a function


    OUTPUT:
    Code:
    Welcome to the APSU Computer Company Commission Statement program!
    Please Enter Salesperson's Identification or -999 to terminate.
    800 
    Please Enter Salesperson's Name.
    George Bush
    Please Enter Salesman Base Salary.
    500
    Please Enter Personal Computer Sales.
    7000
    Please Enter Printer Sales.
    8000
    
    APSU Computer Company
    Commission Statement
    SalesPerson 800
    George Bush
    ****************************************************
    Product                 Sales Amount      Commission
    Personal Computers              7000             640
    Printers                        8000             700
    
    Total Commission                                1340
    Base Pay                                         500
    Total Due                                       1840

  4. #19
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Your code indentation looks terrible...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    nevermind, i see what i did, thanks for the help everyone
    Last edited by WinterInChicago; 10-27-2006 at 11:32 AM.

  6. #21
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It is uncomfortable to read it.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #22
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471

    Question

    Quote Originally Posted by Daved
    >> vart, the assignment shows an example run where it has to be first.
    That code does ask for the ID first. I would have chosen not to just give away the answer, though.
    I don't get it

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    Quote Originally Posted by vart
    I don't get it
    he was probably saying you shouldnt have told me to put that first, but i knew to put that first, i just didnt understand its position in relation to the loop

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I was saying that vart's answer was correct IMO. However, I personally would not have written the code out like that (and I didn't, I tried to give a hint to the same solution). While it is not a big deal, in general I think students would benefit more from being given the chance to figure things out on their own. Simply posting the code that works takes away the students' opportunity to understand the thought process and come to the final solution themselves, IMO.

  10. #25
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    I tried to start with this - posted an pseudocode... But was a little bit pushed to clerify things...

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    No worries. It's not a big deal. You did start with pseudocode. I personally would try to not get pushed to clarify things by somebody other than the OP when it is a homework assignment, but it happens.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

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