Thread: Need help with this While statement

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Need help with this While statement

    Heres the statement, will explain problem after

    Code:
    //Lab 02 - 4.16 Salary Calculator
    
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
        int hoursWorked = 0;
        int hourlyRate = 0;
        int salary = 0;
        
        cout << "Enter hours worked (-1 to end): ";
        cin >> hoursWorked;
        cout << "Enter hourly rate of employee (00.00): ";
        cin >> hourlyRate;
        
        while(hoursWorked > -1)
        {
                          if(hoursWorked <= 40)
                          {
                          salary = hoursWorked*hourlyRate;
                          cout << "Salary is: " << salary << endl;
                          }
                          else
                          {
                          salary = (40 * hourlyRate) + (hoursWorked - 40)*hourlyRate;
                          cout << "Salary is: " << salary << endl;
                          }
        cout << "Enter hours worked (-1 to end): ";
        cin >> hoursWorked;
        cout << "Enter hourly rate of employee (00.00): ";
        cin >> hourlyRate;
        }
        
        system("pause"); 
    	return 0;
    }
    The problem I'm getting is that after entering the hoursWorked and hourlySalary it constantly repeats 'Enter hours worked (-1 to end): Salary is 390.00' over and over untill I turn the program off, just getting started back after not programming for a while so its probably a stupid mistake, please help! :P
    Last edited by masterofwar14; 02-06-2010 at 02:51 PM. Reason: Spelling mistakes

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You may wish to search for how to clean the input buffer. Oh so common issue. Oh so trivial solution.
    You should find an answer in no time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Just before you posted this reply I found out another way to do it, I used a do...while statement instead of while so it would check for the condition afterwards, thanks for the suggestion though, if I ever get stuck in a situation where I MUST use while, I'll try that

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Really? I wouldn't think it would have anything to do with the loop itself, but with the input buffer problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM