This code compiles fine. It accepts my values. Instead of returning 1+1+1 = 3 it returns a random number. I keep going over it but I am too new to spot my mistake. Could anyone out there help me out??
#include <iostream>
usingnamespace std;
void breakfast(int x)
{
cout <<"How many calories did you at breakfast and before lunch?";
cin >> x ;
}
void lunch( int x )
{
cout <<"How many calories did you eat at lunch and before dinner?";
cin >> x;
}
void dinner (int x)
{
cout <<"How many calories did you eat at dinner and anything else you will eat before tomorrow?";
cin >> x ;
}
int main( )
{
usingnamespace std;
cout << "Hello I will tell you how may calories you have eaten today" << endl;
int x, y, z;
breakfast(x);
lunch(y);
dinner(z);
cout<< x+y+z ;
return 0;
}
You pass the parameters by value, so a copy is being made each time.
Your input is stored in those copies, which go out of scope as soon as the functions return.
Pass by reference (int& x) if you want to modify the original values.
Of course you can. I don't even really see the need for a single functions here though. You shouldn't ever really make functions that just do I/O, that's just a waste.
I don't even really see the need for a single functions here though. You shouldn't ever really make functions that just do I/O, that's just a waste.
I personally think it looks more clean compared to other beginner code, which, by the way, looks like they went nuts with the spacebar, or just ignore the spacebar completely.
As for you code, I would recommend passing the arguments by reference, as Athar, and Killerwounds suggested.
Disch, making three functions to output one sentence and take in a number is unnecessary. As a matter of fact if the OP hadn't used seperate functions and just put the commands into main() they wouldn't have had their error with references in the first place.
I can't argue with that, and I'm not trying to diss the OP in any way, sorry if I came across wrong :/ I'm not a very good C++ programmer, especially compared to some of the Guru's on this forum so I'm sorry if I came off as a jerk, I was just disagreeing with their program structure :-|
Disch, making three functions to output one sentence and take in a number is unnecessary
It compartmentalises code and makes the logic more clear. Granted it's not ideal. He could have chosen his names and semantics a little better (returning a variable instead of passing by reference), but for a beginner it's a very good effort.
It's like kfmfe04 said, 1 function = 1 task. This style should be encouraged.
As a matter of fact if the OP hadn't used seperate functions and just put the commands into main() they wouldn't have had their error with references in the first place.
All part of the learning experience. Using language features often leads to bugs. That doesn't mean you should avoid using language features... it just means you should fix the bugs.
That said... I get your point. The functions he wrote are very small and trivial, and it wouldn't really hurt much to put them into main. I don't necessarily disagree with that point. It's just that this is a small step towards good style that you don't see very often, and it seemed like you were discouraging growth in that particular area (even though I know that's not what you were trying to do).
Fair enough, and it's true that for a beginner it's good to see that they didn't use ten variables, and 3 lines of whitespace inbetween every statement. Kudos to the OP ;)