Thread: Can someone look at my simple program and tell me what is wrong with it?

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    Smile Can someone look at my simple program and tell me what is wrong with it?

    I'm in an Intro to Comp Science class and I'm writing a program in C++

    Here is the question:

    Workers at a particular company have won a 7.6% pay increase retroactive for six months. Write a program that takes an employee's previous annual salary as input, and outputs the amount of retroactive pay due the employee, the new annual salary, and the new monthly salary. Use a variable declaration with the modifier const to express the pay increase. Your program should allow the calculation to be repeated as often as the user wishes.

    Here is my program:
    Code:
    #include <iostream>
    using namespace std;
    int main( )
    {
       const double PAY_INCREASE = 7.6;
       double original_salary, new_salary, pay_raise;
    
       cout << "Enter original salary:\n";
       cin >> original_salary;
    
       pay_raise = ((original_salary*PAY_INCREASE)/100)/2;
       new_salary = original_salary+pay_raise;
    
       cout << "Your new yearly salary is " << new_salary
            << " dollars for this year.\n";
    
       cout << "Would you like to input a new salary? (answer y or n)"
    
       if (x == y)
       {
            cout << "Enter original salary:\n";
            cin >> salary_problem;
       }
       else
       {    cout << "Good bye!\n";
       }
       return 0;
    }
    Basically I want to give the person their salary for the year and then have it ask them if they want to enter a new salary. The first part (getting the salary) is compiling and running, but I can't get it to repeat etc

    Any help is VERY appreciated!!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Sooooooooo, put the whole thing in a loop?

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    How would I write that exactly? (sorry I'm new to C++)

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You can wrap the part you want to repeat inside an infinite loop (either while (1) or for (;;)), and put a break statement at the point you want to jump out of the loop to end the program.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Could you maybe write out how to do that? (if that's too much to ask, I understand) but it would be a great help :-)

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I thought it was pretty self-explanatory. Have you covered loops, and break and continue statements, yet? It would be possible to do it with a goto statement, but they're considered bad practice when used unnecessarily and you definitely don't need one here.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    We touched on loops for a day - so hardly at all, we haven't done break and continue statements yet and we were told to stay away from goto statements haha

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    OK now I'm at this:

    Code:
    #include <iostream>
    using namespace std;
    int main( )
    {
       const double PAY_INCREASE = 7.6;
       double original_salary, new_salary, pay_raise;
    
       cout << "Enter original salary:\n";
       cin >> original_salary;
    
       while (new_salary > 0)
       {
       pay_raise = ((original_salary*PAY_INCREASE)/100)/2;
       new_salary = original_salary+pay_raise;
    
    
       cout << "Your new yearly salary is " << new_salary
            << " dollars for this year.\n";
       }
    
       return 0;
    }
    am I getting closer? What am i still doing wrong? It is compiling but not runningt

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If a "break;" statement occurs in a loop, it immediately jumps out of the loop. If a "continue;" statement occurs in a loop, it immediately starts the next iteration of the loop (and if it's a for loop, the iteration statement is executed first, so the loop variable takes its next value). For example,
    Code:
    int i = 0;
    while (1) {
      std::cout << i << '\n';
      ++i;
      if (i == 4) break;
    }
    prints

    0
    1
    2
    3

    In your case, the part you want to repeat goes inside the while (1) loop, and at the point inside the loop where you want to exit the loop, you use the "break;" statement. When it jumps out of the loop, the program exits (assuming the return statement is the only thing after the loop).

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You haven't initialized new_salary before first accessing it (in the while loop condition).

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    how to I access it in the while loop condition?

    (i apologize again and thank you all so much for your help)

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    new_salary is an automatic variable, so its value is undefined before you assign it a value. The only place you do this is INSIDE the loop ("new_salary = original_salary+pay_raise;") but this happens AFTER the first time you try to read it ("while (new_salary > 0)") so the first time you try to read it, it's undefined. Also, your loop would never terminate, since new_salary is increasing, so if it starts out as > 0, it stays that way forever.

    Edit: Actually, new_salary doesn't increase inside the loop, since the expression you're assigning to it (original_salary + pay_raise) never changes. So it stays constant. The result is the same - the loop never terminates.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    so how would I get it to terminate?

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Basically, what I want to know is, how do I get it to just keep asking the above question?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Your original code was closer to what you need. Basically, each time you input original_salary, you compute new_salary ONCE, then print it, ask whether to start over, and if the answer is no, do a break. All of the aforementioned code goes inside an infinite loop (either while or for).

Popular pages Recent additions subscribe to a feed