1

My program keeps looping and never get's to "return 0;". Is it the compiler that's bad or the code?

#include<iostream>
using namespace std;

int main() {
    string nameInput = "";
    string Input = "Yes";
    cout << "Welcome to the purple casino!" << endl << "What's your name?" << endl;
    while(Input =="Yes" || "yes"){
        getline(cin, nameInput);
        cout << nameInput << ", what a nice name!" << endl << "Do you want to change it?" << endl;
        getline(cin, Input);
        if(Input =="Yes" || "yes"){
            cout << "To what?" << endl;
        }
    }
    cout << "Let's begin!";
    return 0;
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 9
    Rule of thumb: it's you, not the compiler! –  Sep 21 '17 at 09:43
  • Aside from the yucky indentation, this is well-asked. – Bathsheba Sep 21 '17 at 09:47
  • 2
    at least in 99.99% of the cases, see https://stackoverflow.com/questions/42088015/lambda-capture-and-parameter-with-same-name-who-shadows-the-other-clang-vs-g for a counterexample. – Picaud Vincent Sep 21 '17 at 09:49
  • `if(Input =="Yes" || "yes")` -- You'll discover that C++ doesn't know shorthand English-speak. We may say "if x equals this or that", but that is not how you "say" this in C++. – PaulMcKenzie Sep 21 '17 at 09:52
  • @PaulMcKenzie: Although some programming languages do. – Bathsheba Sep 21 '17 at 09:54
  • @Bathsheba Yes, but I usually see this mistake being made because the new coder translates their English to a C++ conditional statement. – PaulMcKenzie Sep 21 '17 at 09:56
  • In Cobol you can actually write `IF Input IS EQUAL TO 'YES' OR 'yes'` and that has worked since 1960. Who says that programming language design improves over time? – Bo Persson Sep 21 '17 at 11:30
  • @BoPersson -- Yes that's true about COBOL. However COBOL was written for the English speaker, so the shortcut language sort of fits in. – PaulMcKenzie Sep 21 '17 at 14:44

6 Answers6

5

My program keeps looping and never get's to “return 0;”. Is it the compiler that's bad or the code?

The code, as (almost) always.

Input =="Yes" || "yes" will always evaluate to true, no matter what Input's value really is, since it boils down to saying:

  • true if: Input equal to "Yes" OR "yes".
  • false if otherwise.

A string literal evaluates to true, thus the second operand of the logical or will be true, causing the whole logical expression to evaluates to true, always!

As a result your while loop's condition is always true, resulting in an infinite loop!

So change this:

while(Input =="Yes" || "yes")

to this:

while(Input =="Yes" || Input == "yes")

PS: Change the condition of the if statement similarly, since it's the same exact condition.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
5

The expression Input == "Yes" || "yes" is evaluated, due to operator precedence, as

(Input == "Yes") || "yes"

which is always true. This is because the const char[4] literal "yes" decays to a const char* type, with a non-zero pointer value.

You need Input == "Yes" || Input == "yes"

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

Your while statement's condition is wrong:

while (Input == "Yes" || "yes")

as "yes" operand always evaluates to true causing the entire condition to be true. Try this instead:

while (Input == "Yes" || Input == "yes")
Ron
  • 14,674
  • 4
  • 34
  • 47
0

There is a silly mistake in your code. Please see the below edited code -

#include<iostream>
using namespace std;

int main(){
string nameInput = "";
string Input = "Yes";
cout << "Welcome to the purple casino!" << endl << "What's your name?" << 
endl;
while(Input =="Yes" || Input == "yes"){ // Error was here
getline(cin, nameInput);
cout << nameInput << ", what a nice name!" << endl << "Do you want to             
change it?" << endl;
getline(cin, Input);
if(Input =="Yes" || "yes"){
cout << "To what?" << endl;
}
}
<< "Let's begin!";
return 0;
}

Try modifying while(Input =="Yes" || "yes"){ to while(Input =="Yes" || Input == "yes"){

I think the problem will be solved.

Naseef Chowdhury
  • 2,357
  • 3
  • 28
  • 52
0

Hi before return try :
cout << "Let's begin!";
And change in While and if your condition to:
(Input =="Yes" || Input=="yes")

RB-10111
  • 21
  • 4
0

In a more general way, if you have a loop from which your program never returns, it means that the condition you passed always evaluates to true. In your case indeed, an as others already answers, the condition

while (Input == "Yes" || "yes")

always evaluate to true because of the second part. What you really want is to check if Input is "Yes" OR Input is "yes", which in C++ should be written :

while (Input == "Yes" || Input == "yes").

Hope, this more general answer will help.

M. Yousfi
  • 578
  • 5
  • 24