Trouble with simple equation

[code]
double a;
double b;
double c;
a = 300;
b = 290;
c = a - b;
double d;
double e;
d = c / a;
e = d * 100
[code]

Yes i'm newish to c++ and don't link me to any tutorials on c++. I will start from the beginning to end of the tutorial on this website, but I'd like to finish this program I'm working on first.

When I use this code, e always ends up as 100, because its multiplied by 100. The program is giving e an value of 1, but its supposed to be what ever c / a equals.
Post more of your code
Works fine for me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
double a;
double b;
double c;
a = 300;
b = 290;
c = a - b;
cout<<c<<endl;
double d;
double e;
d = c / a;
e = d * 100;
cout<<"e = "<<e<<endl;
return 0;
}

e=3.33333
[code]
double gp;
gp = sls - cog;
double gm;
gm = gp / sls;
double gm2;
gm2 = gm * 100;
[code]

I use
cin >> sls
and
cin >> cog
to get the value of sls an cog, but I'm just using 300 and 290 as the values for now.
Last edited on
This also works fine for me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
    double sls;
    double cog;
    cin>>sls>>cog;
    double gp;
    gp = sls - cog;
    double gm;
    gm = gp / sls;
    double gm2;
    gm2 = gm * 100;
    cout<<"gm2 = "<<gm2<<endl;
return 0;
}

gm2=3.33333
Last edited on
Alright I figured out whats going wrong with my code, but I don't know how to fix it. Alright with the code you gave me with gp / sales. You have 10 / 300 an you should get 0.33 repeating. Though with my code instead of getting 0.33 the program just gets one as a default. I think this is because 300 won't go into 10. I think it could have something to do with having it as a 'double' variable. I'm not too keen on what the others are. I just know 'double and int'.

#include <iostream>
using namespace std;

int main()
{
double sls;
double cog;
cin>>sls>>cog;
double gp;
gp = sls - cog;
double gm;
gm = gp / sls; // This is where the code messes up /*
double gm2;
gm2 = gm * 100;
cout<<"gm2 = "<<gm2<<endl;
return 0;
}
Topic archived. No new replies allowed.