Originally Posted by
CoBraX
Lol so he can see go with which one he feel like he like the best. & beside you should be happy be are willing to share some knowledge.
You realize yours was wrong right?
Code:
cin >> sum;
cout << sum;
All you're doing is printing the user input, you're not doing the sum of a and b... It completely defeats the purpose of what he was trying to do with a, b, and sum = a + b.
There are numerous ways this could be done. What OP needs to understand though is once you define a declare a variable and initialize it (int a is declare, sum = a + b is defining) it stays that value until you redefine it (i.e., when you tell that variable to be something else)
When you first wrote sum = a + b; you made sum be the value of a + b at that point. It does not change until you make it change.
Code:
#include <iostream>
#include <functional> // std::plus<Type>()(variable, variable)
int add(int first, int second)
{
return first + second;
}
int main()
{
// Declare b and sum, declare a AND initialize it to the value of 10
int a = 10;
int b, sum;
std::cout << "Give me a number: ";
std::cin >> b;
// std::plus<type> guarantees the addition of pointers as well as variables
sum = std::plus<int>()(a, b);
// Declare sum up there, initialize it right here
sum = a + b;
// Declare sum up there, pass the variabls to another function to do the math for us
sum = add(a, b);
std::cout << sum << std::endl;
// main() does not have to return a value
// return 0;
}
- - - Updated - - -
Originally Posted by
Hitokiri~
And what makes me cry is all the answers are wrong in the first place.
Pretty much what I was thinking after reading all the comments.