sorry if am asking too many questions but the following works perfectly:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int main()
{
int a,b,result;
cout<<"\n Enter a number: ";
cin>>a;
cout<<"\n Enter the second number: ";
cin>>b;
result=a+b;
cout<<"\n Result: "<<result;
cin.get();
return 0;
}
why does it matter if i place the result=a+b right after int a,b,result; after i declare the variables.
what happens is the program compiles successfully but when the 2 numbers add i get the wrong calculations....just curious
cout<<"\n Enter a number: ";
cin>>a; // this gets a number from the user, puts that number in 'a'
cout<<"\n Enter the second number: ";
cin>>b; // this gets another number from the user, puts that number in 'b'
result=a+b; // this adds the two together
so then:
1 2 3 4 5 6 7
result=a+b; // ?? what are you adding together here?
// a and b were not set to anything
cout<<"\n Enter a number: ";
cin>>a;
cout<<"\n Enter the second number: ";
cin>>b;
How can you add two numbers before the user tells you what numbers they are?
Also with the way computer memory works your program needs to tell the computer to make room in the memory for your variables, then you are allowed to store values in the memory cells that you program made room for. Then you can ask the user for values to enter. From there you can perform addition or whatever you want to do with your variables.
#include <iostream>
int main()
{
int a, b, result, *p;
a = b = result = 0;
p = &result;
a = 5;
b = 6;
result = a + b;
std::cout << *p << '\n';
a = 100;
b = 150;
result = a + b;
std::cout << *p;
std::cin.get();
return 0;
}
I am merely pointing this out,; do as Disch has explained.