Ok we did a program in class thats pretty self explanatory. I wrote it two ways, our main focus is this line:
Code:
cout<<"From function: "<<make_chng(value)
<<"\nFrom main: "<<value<<endl;
Putting this all in one line, the output is:
Enter integer: 10
From function: 100
From main: 10
This output is wrong because value was changed, as you can clearly see on the previous line of the cout, where it was 100.
when i change this line to
Code:
cout<<"From function: "<<make_chng(value);
cout<<"\nOriginal: "<<value<<endl;
it outputs:
Enter integer: 10
From function: 100
From main: 100
That output is correct according to the program. Now my question is this: Why does having it in one cout make it give correct value for "value" on the first part then the wrong number ont he second part?? According to logical program flow, should it not output two 100's even tho its in one cout because its been changed??? I know this is because of how i write the cout, but i don't know why. Heres the full program with the cout line that is working wrong being used:
Code:
#include <iostream.h>
int make_chng(int &changed);
int main()
{
int value;
cout<<"Enter a integer: ";
cin>>value;
cout<<"From function: "<<make_chng(value)
<<"\nFrom main: "<<value<<endl;
return 0;
}
int make_chng(int &changed)
{
changed *= 10;
return changed;
}