can somebody please tell me what i did wrong?

Im still new to c++, and trying to learn if statments etc could somebody please tell me what i did wrong here?

#include <iostream>

using namespace std;

int number1;
int number2;
int total;
char enter;
int addnumbers();


int main()
{
cout << "to add two numbers together please press p!" << endl;
cin >> enter;


if(enter == p){

int addnumbers();

};







return 0;
};

int addnumber(){

cout << "please enter first number! " << endl;
cin >> number1;

cout << "please enter second number " << endl;
cin >> number2;

total = (number1 + number2);

cout << "total is " << total;

return total;


};
1
2
3
4
5
if(enter == p){

int addnumbers();

}; // if statements don't end with semicolon. 
thank you, but it still says 'p' was not declaired in this scope!
Oh yeah.

characters needs to have the ' ' quotes around them. Strings (2+ characters) needs " " around them.

In your case it should be - if(enter == 'p') // note ' '
ahh i see, complies without any errors, but now when i build and run it, the function doesnt run when i press p!!
Oh yeah...

int addnumbers(); // this is not how you call a function, remove the "int" part.

Note your function returns a number. If you want to save the number it returns you need to do so in a variable -


1
2
3
4
5
6
int saverOfAll;
if(enter == 'p'){

saverOfAll = addnumbers(); // saverOfAll will be equal to the number the function returns

}


Also, please use code tags for your code so it is easier to read. Im using code tags and you can see the difference in readability between mine and your code - http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
thank you its working now, appreciate your help!!!
could you also do

if(enter == 'p'){

addnumbers();
return total;

}

total being the variable of both numbers added together!
Again, use code tags, otherwise I will ignore your posts since you ignore mine - http://www.cplusplus.com/articles/jEywvCM9/

I'm not sure what you're trying to do. You can return variables from functions, that's it. Since that code is in main, where are you returning it to?

If you want a variable which equals the two numbers added together, then that is exactly what I gave you -

1
2
3
4
5
6
int saverOfAll;
if(enter == 'p'){

saverOfAll = addnumbers(); // saverOfAll will be equal to the number the function returns

}


http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.