-
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;
}
-
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.
-
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.
-
Strange... which compiler do you use?
-
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
-
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.
-
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.
-
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.