Why doesnt this work?

This is a discussion on Why doesnt this work? within the C++ Programming forums, part of the General Programming Boards category; Ive written a small program that stops after computing after it outputs "retroactive pay for 6 months". Why doesnt it ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    5

    Angry Why doesnt this work?

    Ive written a small program that stops after computing after it outputs "retroactive pay for 6 months". Why doesnt it continue ? Here's the code:

    #include <iostream>
    // comments
    using namespace std; //introduces namespace std
    int main( void )
    { const float raise = .076;

    int salary;
    int Retropay;
    int wholesalary;
    cout << "Enter previous salary: \n";
    cin >> salary;
    cout << "Retroactive pay for 6 months is " << ((salary/12)*raise*6);
    cin >> Retropay;
    cout << "New annual salary is " <<(salary*(Retropay+Retropay));
    cin >> wholesalary;
    cout << "New monthly salary is " <<(wholesalary / 6) << endl;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Doesn't it continue? That must be something with your compiler. Do you mean that the app doesn't compile?

    Anyway, I saw some other problems with the source. For example:
    Dividing 'salary' with '12' will result in an integer result because both are integers. That may create problems.
    // Gliptic

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    5

    Angry

    Well, it should spit out cout Retroactive pay for 6 months is ,New annual salary is , New monthly salary is , when you run the program yes? It stops after retroactive pay for 6 months is, i dont understand why.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Strange... which compiler do you use?
    // Gliptic

  5. #5
    Unregistered
    Guest
    Hi

    it stops becouse it waits to be entered value for Retropay variable

    --> cin >> Retropay;

    Is the expresion '((salary/12)*raise*6)' compute the value of the Retropay variable?
    So, if it is then try to assign it's value in 'classic' way:

    Retropay = (salary/12.0)*raise*6;

    damyan

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    5
    Im using Codewarrior from metroworks. Its for an intro to C++ class, so im really baffled. I thought the Cin >> retropay would just remember that number as "retropay" for the next computation.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    I agree with Unregistered that the problem is with the cin >> Retropay; line. cin takes input from standard input (usually the keyboard) and stores it in an array (called input buffer) until the enter key is pressed at which time it empties the input buffer into the designated variable(s) (spot(s) in memory really but variable means the same thing). Once it has emptied the buffer there is nothing left in the buffer for cin to "remember". Actually, there MAY be something in the input buffer yet, but it's reliability is zilch.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Add some couts to prompt for the cins. You might want to make your variables doubles or floats instead of ints to eliminate typecasting problems. Also, your float raise will be converted to a double; try: const float raise = 0.076f if you want to keep it a float. You need that f at the end.
    You could also prompt for and enter all numbers at once. cin will read multiple variables in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 08:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

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