Thread: Need help with this while loop, pretty please?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    60

    Need help with this while loop, pretty please?

    I have to set my cin's and cout's to work in a while loop. The way it is supposed to work is, the loop will end once someone enters "-999" as the int number... as you can see below. I have yet to add in the function call and other things, but I think I have come far enough to be able to ask this. I think if I put the cin << number; in the loop, it will not terminate because it will not check until it has gone thru everything. Can someone tell me what to do please?


    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;
    
            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;
    
    
    
            }//the int main has ended. oh my!
    
    double commission (double actualsales, double basesales, double compercent)
            {//birth of a function
            double comm;
                    if (actual sales > basepay)
                            {
                            comm = actualsales * compercent;
                            //and you get to keep your job :)
                            }
                    else
                            {
                            comm = 0;
                            }
            return comm;
            }//death of a function

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    So? Check the number just after it is entered and exit the future loop if exit condition is matched.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    One option is to check everything in the condition:
    Code:
    while ( cin>> number && number != -999) {
      // Da da da
    }
    Another is to loop infinitely and break when a condition is met:
    Code:
    for ( ; ; ) {
      cin>> number;
    
      if ( number == -999 )
        break;
    
      // Da da da
    }
    I'm not sure I understand the problem you think you're having.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    if i put :

    Code:
    cin >> number;
    while (number != -999)
    it will never go back to the cin >> number;

    if i put:

    Code:
    while (number != -999)
    cin >> number;
    it cant check the condition because it was never told what number was.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This SHOULD be good. At least it works,
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    double commission (double actualsales, double basesales, double compercent);
    
    int main (){
    	//the int main has begun
    	string name;
    	string buff;
    	int number;
    	double basepay, compsales, printsales, compcom, printcom, totalcom, totalpay;
    	cout << "Welcome to the APSU Computer Company Commission Statement program!" << endl;
    	while(true){
    		cout << "Please Enter Salesperson's Identification or -999 to terminate." << endl;
    		getline(cin,buff);
    		number=strtol(buff.c_str(),NULL,10);
    		if(number==-999){
    			break;
    		}
    		cout << "Please Enter Salesperson's Name." << endl;
    		getline (cin, name);
    		cout << "Please Enter Salesman Base Salary." << endl;
    		getline(cin,buff);
    		basepay=strtod(buff.c_str(),NULL);
    		cout << "Please Enter Personal Computer Sales." << endl;
    		getline(cin,buff);
    		compsales=strtod(buff.c_str(),NULL);
    		cout << "Please Enter Printer Sales." << endl;
    		getline(cin,buff);
    		printsales=strtod(buff.c_str(),NULL);
    	}
    	return 0;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    the prof said it had to be a while loop, no ifs

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    though I may be able to do a bool statement... not sure... shes not good at explaining

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    the prof said it had to be a while loop, no ifs
    Oh dear! What else stupid can they think of?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    well i believe you guys when you post code for me, i just want to stay away from stuff she hasnt covered

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you always can use something like that:
    Code:
    //enter number
    ...
    while(number != -999)
    {
          //do things
          ...
          //enter number
          ...
    }

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    cout << "Please Enter Salesperson's Identification or -999 to terminate." << endl;
    It seems that it must be the first input??? I would've used while(number != -999) when it would've been the last input...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    yes this is a copy of the assignment.. but the setw() parts wont show up in this forum

    CSCI 1010 Programming Assignment 6



    A. Purpose- Write a program that uses a function, loop, strings, output formatting.

    B. Due Date- Due by 3 p.m. on Friday 10/27/06

    C. Program Description – Name the program a6yourlastname.cpp




    Write a C++ program to run on cscilinux2 for the following:

    1) Read a three-digit salesperson identification number, a salesperson’s full name, a double base salary, and a double actual sales amount for each of the four categories.

    2) Using a function, compute the commission earned in each of the two categories. The function should be passed the actual sales amount, the base sales, and the commission rate as a double. The function should return the commission which could be zero if the actual sales do not exceed the base sales.

    3) Display the identification number, the salesperson’s full name, actual sales and base salary.

    4) For each of the two categories, display the description, the sales amount, the commission amount.

    5) Display the total commission paid to the employee and the total paid. All of the above must be in column format with the decimal points aligned.

    6) Continue looping until -999 is inputted for Salesperson’s Number

    Example:

    Please Enter Salesperson’s Identification or -999 to Terminate
    150

    Please Enter Salesperson’s Name
    Jane Doe

    Please Enter Salesman Base Salary
    1234.56

    Please Enter Personal Computer Sales
    5000.00

    Please Enter Printer Sales
    1000.00






    APSU Computer Company

    Commission Statement

    SalesPerson 150

    Jane Doe

    ********************************

    Product
    Sales Amount
    Commission

    Personal Computers
    5000.00
    100.00

    Printers
    1000.00
    0.00





    Total Commission

    100.00

    Base Pay

    1234.56

    Total Due

    1334.56




    Please Enter Salesperson’s Identification or -999 to Terminate -999

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    You've learned while loop but not if? How's that possible? It's like you're learning square root and you haven't learned adding.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It seems that it must be the first input??? I would've used while(number != -999) when it would've been the last input...
    O! my... so hard to change the first input to the last one?

    Code:
    int main (int argv, char* argv[])
    {
    
            string name;
            int number;
    
            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)
            {
            	double basepay, compsales, printsales, compcom, printcom, totalcom, totalpay;
            	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;
            	//do your stuff here
            	//...
    
    
            	cout << "Please Enter Salesperson's Identification or -999 to terminate." << endl;
            	cin >> number;
           }
            return 0;
    }

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    There's a sample run in the assignment.
    Last edited by maxorator; 10-27-2006 at 11:06 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

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