0

In the following I present a code that is not working as intended. It should be given a name, and then an age if the users chooses to give it, in order to finally print both to stdout. Following the code you will see my teacher's own run of the code. What I don't understand is why the expression get_yes_or_no("Would you tell me how old are you? ") evaluates to true when my teacher answers correctly "no". Even though I know there are some obvious mistakes in my code, as it is for the semantically wrong if (age) statement, what I'm really interested about and I don't understand is why my teacher was prompted her age after inputing "no".

#include <iostream>
#include <string>

bool get_yes_or_no(std::string question);

int main() {
    using std::cout;
    using std::cin;

    std::string result{};
    cout << "Input your name: ";
    std::getline(cin, result);

    int age{ 0 };
    if (get_yes_or_no("Would you tell me how old are you? ")) {        
        cout << "Input your age: ";
        cin >> age;
    }

    if (age)
        cout << "Your name: " << result << ", your age: " << age << std::endl;
    else
        cout << "Your name: " << result << std::endl;

    return 0;
}

bool get_yes_or_no(std::string question)
{
    std::string result{};

    std::cout << question << '\n' << "Answer yes or no: ";
    std::cin >> result;
    if (result == "yes")
        return true;
    else if (result == "no")
        return false;
    else {
        std::cout << "The only valid answers are \"yes\" or \"no\".\n";
        return get_yes_or_no(question);
    }

    return -1;
}

My teacher's run:

Input your name: Io
Would you tell me how old are you? 
Answer yes or no: nope
The only valid answers are "yes" or "no".
Would you tell me how old are you? 
Answer yes or no: no
Input your age: no
Your name: Io
Albert
  • 174
  • 8
  • By the way, excuse me but I don't know how to turn off syntax highlighting for the sampled output. – Albert Mar 26 '20 at 20:13
  • 2
    Why do you return `-1` from a function that's supposed to be boolean? You're tricking yourself there. If you mean false, return false. (`-1` cast to bool is not false.) – Mat Mar 26 '20 at 20:14
  • Third observation: syntax highlighting in the output is normal. Don't worry about it. – JaMiT Mar 26 '20 at 20:18
  • @Mat, I'll corect that. – Albert Mar 26 '20 at 20:24
  • I did a run of your code, and the code you posted does not match the output. The most obvious thing is that your code does not produce a new line between `The only valid answers are "yes" or "no".` and `Would you tell me how old are you?`. (The second difference is that your code does skip the age prompt.) Are you sure you compiled the latest version of your code before turning it in? – JaMiT Mar 26 '20 at 20:24
  • Although the `return -1;` statement can't actually be reached. (Which should probably give a compiler warning.) – aschepler Mar 26 '20 at 20:25
  • I gave the ```return -1``` a little bit as a whatever, since the execution is not reaching there, but obviously a boolean value would have been more intelligent. – Albert Mar 26 '20 at 20:27
  • 1
    @aschepler See [Why does GCC not warn for unreachable code?](https://stackoverflow.com/questions/17249934/why-does-gcc-not-warn-for-unreachable-code). – JaMiT Mar 26 '20 at 20:27
  • @JaMiT, actually, the version she compiled had the ```\n``` at the end of the ```The only valid ...``` string and also had a type alias defined on the global scope, namely ```using str = std::string```. She's compiling forcing C++11 standard. – Albert Mar 26 '20 at 20:32
  • 1
    i don't like that this code is using recursion instead of a loop. with enough invalid answers, you are eventually going to overflow the stack and crash – backcab Mar 26 '20 at 20:37
  • Your code works for me as expected. I hope you are not executing an earlier version of the program executable. – Sanil Mar 26 '20 at 20:38
  • @backcab, I think too that the recursion is another flaw of my program, but thank you for noting it anyway. – Albert Mar 26 '20 at 20:39
  • @toritoverdejo When you put it that way, it reads like you did not compile the code yourself *and try to reproduce your teacher's result*. That's step 1, before asking for help. Have you done that? – JaMiT Mar 26 '20 at 20:40
  • @JaMiT, yes I did, and since it didn't reproduce I thought it would have been due to a machine-dependent bug. – Albert Mar 26 '20 at 20:45

2 Answers2

2

As others have mentioned, the posted code behaves correctly. It sounds like this is a case of submitting the wrong version of the executable since an executable made with the above code works.

The other possibility may be the teacher ran another student's executable and accidentally gave the feedback to you instead of them.

It may be worth checking with your teacher to see what executable they got and verify they ran the one you submitted.

backcab
  • 638
  • 1
  • 6
  • 21
0

Well, after all this research, and by the way thank you all for your comments, I found out the mistake. I asked my teacher to send me back the code she was compiling and it lacked the return keyword in return get_yes_or_no(question);. She was not compilling the last version I sent her. That produced the unexpected behaviour we are talking about. Sorry for all this lost time.

Albert
  • 174
  • 8