Hello, I am currently practising functions here and am a bit confused onto the output of my program. I am returning string functions here, and as you can see I am just outputting it by using "cout". Anyways, this particular program is not really an assignment, but just practice.
This is what happens:
1. I enter a word for the first part: "Hello, are you you?".
- This gets placed in the variable enter which is "good".
Then we get to the big line on line 17.
We first stumble upon the first function "hello()" which in the function, says "Please enter another word here" and then returns that word to the function "hello()". Let's say I enter earth. Then goes to "hello1("Narn"). Because there is already a word inside here, this gets passed on to the parameter and literally returns the parameter, so it should just display "Narn" then we get to the third part, which is "hello1(enter)" which should just display "good" because again, we entered that for the variable "enter" already so it should just return that to the function "hello1(enter)" and lastly, the "hello()" function which again, requires you to enter another word and will return that word. Let's say I enter Mars.
Anyways.. sorry for that explanation but this is my problem. After I enter all of that, I get the output of: "mars and Narn and good and earth". Why is this? Isn't it supposed to be "earth and Narn and good and mars"? Isn't the code supposed to read from left to right? That's my confusion really. Thank you.
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
|
#include <iostream>
#include <string>
using namespace std;
// Function Prototype
string hello(); // Has to return something.
string hello1(string); // Has to return something.
int main()
{
string enter;
string hold;
string hold1;
cout << "Hello, how are you? ";
cin >> enter;
cout << hello() << " and " << hello1("Narn") << " and " << hello1(enter) << " and " << hello() << endl;
return 0;
}
string hello()
{
string test;
cout << "Please enter another word here: ";
cin >> test;
return test;
cout << "This does not display\n";
}
string hello1(string san)
{
string awesome = san;
return san;
}
|