z+y+z = -2314234124 and to this I say "What!?"

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??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace 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( )

{
    using namespace 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.
If I had a kudos I'd give it to you thnks.
Also I have a question about this.
Could you make this the same way with breakfest, lunch, and dinner in the same funciton?
Ex.

#include <iostream>

using namespace std;

void food(int& x, int& y, int& z)
{
cout <<"How many calories did you at breakfast and before lunch?";
cin >> x ;

cout <<"How many calories did you eat at lunch and before dinner?";
cin >> y;

cout <<"How many calories did you eat at dinner and anything else you will eat before tomorrow?";
cin >> z ;

}

int main( )

{
using namespace std;
cout << "Hello I will tell you how may calories you have eaten today" << endl;
int x, y, z

food(x, y, z);
cout<< x + y + z ;
return 0;

}
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.
You shouldn't ever really make functions that just do I/O, that's just a waste.


I couldn't disagree with you more. ;P
Last edited on
closed account (zb0S216C)
ascii wrote:
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.

Wazzak
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.
agreed

your style is unusually clear and modular for a beginner (good variable/function names - each function does only one task)

it'll scale very nicely as your projects get larger and larger

gj
@ascii: It's a learning process. Had he not used functions, and not ran into this issue, he would not have learned anything. Start small, like OP is.
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 ;)
Disch wrote:
It's like kfmfe04 said, 1 function = 1 task. This style should be encouraged.


This is called high cohesion. It's a great thing to shoot for.
http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29
Topic archived. No new replies allowed.