Problem with code, how to fix?

I am trying to set ls-os to never equal zero. But I have some errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <math.h>
using namespace std;

// Precondition: Last year's salary - previous year's salary can never equal zero//

double compute_change(double ns, double ls, double os)
{		
if (ls-os) = 0 
    return "error";
    
    else
        return(((ns-ls)/(ls-os))*100);
}

int main()
{
        double new_sal, old_sal, last_sal;
        cout << "New salary: ";
        cin >>new_sal;
        cout << "\nLast year's salary: ";
        cin >>last_sal;
        cout <<"\nPrevious year's salary: ";
        cin >> old_sal;
        cout <<compute_change(new_sal,last_sal,old_sal);
        return 0;
}




Errors:
HelloWorld.cpp 9 12 expected primary-expression before '=' token
HelloWorld.cpp 10 5 expected primary-expression before 'return'
HelloWorld.cpp 10 5 expected ';' before 'return'
HelloWorld.cpp 14 1 control reaches end of non-void function
If statements need to have their condition parenthesized. For example:
1
2
if(x + y > 2)
    // do stuff... 

Also, == does comparison, while = does assignment. I think you want the former.

Finally, you can't return a string from a function that returns a double (like you are on line 10). You'll need to do something else, like return a bad value (like 0) or throw an exception (though that is advanced).
When you see a line of compiler errors, look at the line number of the first syntax error. That's usually the offending line- everything else is just noise.

Try this for line 9:
if (ls-os == 0)
When you use one equal sign, you're setting it equal to that item. When you use two, you're checking to see. It's sort of the difference between asking and telling.
Ex:
if(a == b)
do stuff;
vs. a = 5;
also... in line 10, "error" is a word, but your function needs to return a double value. Might want to take a look and see how you can fix that. Maybe you could stop bad data from going into the function instead of trying to throw it out? That might be easier. I hope this helps- good luck!
Last edited on
Topic archived. No new replies allowed.