I'm practicing so I wrote this simple program that suppose to add and divide two numbers. It does that but the result comes out with a 0 at the front and don't know why. Any idea? Thanks

#include<iostream>

using namespace std;

int main()

{

int a ,b;

int result;

a = 0;

b = 0;

//a = a + 90;

//result = 0;

result = a + b;

cout << "Enter a number for a and a number for b:" << endl;

cin >> a >> b;

cout <<"the answer is:" << result << (a + b)/2 <<endl;

return 0;

}

#include<iostream>

using namespace std;

int main()

{

int a ,b;

int result;

a = 0;

b = 0;

//a = a + 90;

//result = 0;

result = a + b;

cout << "Enter a number for a and a number for b:" << endl;

cin >> a >> b;

cout <<"the answer is:" << result << (a + b)/2 <<endl;

return 0;

}

result = a + b;

cout << "Enter a number for a and a number for b:" << endl;

cin >> a >> b;

You are summing a and b first and then taking a & b as input.

When a and b are being added, a = 0 & b = 0, so result is giving 0.

**Solution:**

#include<iostream>

using namespace std;

int main()

{

int a ,b;

int result;

a = 0;

b = 0;

//a = a + 90;

//result = 0;

cout << "Enter a number for a and a number for b:" << endl;

cin >> a >> b;

result = a + b;

cout <<"the answer is:" << result << (a + b)/2.0 <<endl;

return 0;

}

cout << "Enter a number for a and a number for b:" << endl;

cin >> a >> b;

You are summing a and b first and then taking a & b as input.

When a and b are being added, a = 0 & b = 0, so result is giving 0.

#include<iostream>

using namespace std;

int main()

{

int a ,b;

int result;

a = 0;

b = 0;

//a = a + 90;

//result = 0;

cout << "Enter a number for a and a number for b:" << endl;

cin >> a >> b;

result = a + b;

cout <<"the answer is:" << result << (a + b)/2.0 <<endl;

return 0;

}

Last edited on

thanks wiseRehan,

I ran it and now it's giving me a different result which doesn't look right. For example, if enter two numbers, 5,5 it give me 105.

I ran it and now it's giving me a different result which doesn't look right. For example, if enter two numbers, 5,5 it give me 105.

The thing being printed is the result (10) and average(5) without any space or newline.

Replace your cout statememnt with:

cout <<"the answer is:" << result << endl;

cout << "Average is: " << (a + b)/2.0 <<endl; //or you can write cout << "Average is: " <<result/2.0 <<endl;

Replace your cout statememnt with:

cout <<"the answer is:" << result << endl;

cout << "Average is: " << (a + b)/2.0 <<endl; //or you can write cout << "Average is: " <<result/2.0 <<endl;

Topic archived. No new replies allowed.