1 #include <iostream>
2
3 using namespace std;
4
5 int breakfast(int x)
6 {
7 using namespace std ;
8 cout <<"How many calories did you at breakfast and before lunch?";
9 cin >> x ;
10 return 0;
11 }
12
13 int lunch( int y )
14 {
15 using namespace std;
16
17 cout <<"How many calories did you eat at lunch and before dinner?";
18 cin >> y;
19 return 0;
20
21 }
22
23 int dinner (int z)
24 {
25 using namespace std;
26 cout <<"How many calories did you eat at lucnh and before dinner?";
27 cin >> z ;
28 return 0;
29 }
30 int AddcaL(int x, int y , int z )
31 {
32 using namespace std;
33
34 cout<<" here is todays total";
35 return x+y+z ;
36
37 }
38 int main()
39
40 {
41 using namespace std;
42 cout << "Hello I will tell you how may calories you have eaten today" << 43 endl;
44 int x;
45 int y;
46 int z;
47 breakfast(x);
48 lunch(y);
49 dinner(z);
50 AddcaL();
51
52 return 0;
53 }
When I compile this I get an error in the AddcaL function that there are too few arguements. I am trying to create this program to show off to my wife.
What I want it to do is run ask the calories for breakfast,lunch, and dinner period. Then take the numbers and put them in the AddcaL() function and have it add them all together.
Other than the error what do you think of think of this program in general. I am just trying to learn and would appreciate any constructive criticism. Please tell me if I am way off track I am not taking courses or anything and am doing this on my own. I appreciate your time.
AddcaL( ) takes 3 integer arguments. However, when you call it, you pass no arguments.
You can overload AddcaL( ) or assign the argument default values. For example:
1 2 3 4 5 6 7 8 9
// Overloaded:
int AddcaL( void )
{
}
// Default argument values:
int AddcaL( int x = 0, int y = 0, int z = 0 )
{
}
Uhh. you should read the tutorial on this site. An pl0x use code tags so you dont need to put those numbers before every line. Don't put usingnamespace std; on every function just the global code. If you want a function to not to return any value make it void.
#include <iostream>
usingnamespace std; // only put using namespace std; here
void breakfast(int& x)
{
cout <<"How many calories did you at breakfast and before lunch?";
cin >> x ;
}
void lunch( int& y ) // if you want a function that just returns 0 and u wont use the return value just make it void
{
cout <<"How many calories did you eat at lunch and before dinner?";
cin >> y;
}
void dinner (int& z) // Passing the variable as reference changes the variable in main function.
{
cout <<"How many calories did you eat at lucnh and before dinner?";
cin >> z ;
}
void AddcaL(int x, int y , int z ) // here you are just passing copies of the variable.
{
cout<< "here is todays total " << x+y+z << endl;
}
int main()
{
usingnamespace std;
cout << "Hello I will tell you how may calories you have eaten today" << endl;
int x;
int y;
int z;
breakfast(x);
lunch(y); // Parameters and arguments don't need to have the same name. :P
dinner(z);
AddcaL(x, y, z); // here you use the changed variables. In your old code you didnt define them.
return 0;
}
You have the right idea, but there are some issues with your code. I would recommend that you read the tutorials, since you seem to be a bit confused about these functions.
The x, y, and z that you talk about it the function definitions are different from the ones you talk about in your main function. Whenever you define a variable in a function, it is a "local variable", meaning that it only has value in that function. When you leave the function, the variable can be whatever it wants to be.
I would do the program like this. Instead of making the functions return 0 every time, I would make them return x, y, or z, depending on the function. Also, you don't need to include x, y, and z as arguments in the functions, although you need to initialize them (like int a;) in the functions.
Then in your main section, you would say x = breakfast(), y = lunch(), z = dinner(), then do AddcaL(x, y, z).
Thanks Draksis I will try that . I literally sat down a week ago and said I am going to learn this. SO I am only about a week into c++. It seems a daunting task.