0

Hi guys so for fun I'm trying to translate this video to code: https://www.youtube.com/watch?v=7hhQLozezak

I want it to go

+"You stupid"

+"No I'm not" (or any small grammatical variants). If they say anything off script they get "Later dude..."

+"What's 9+10?"

If they respond with 21, the code ends. If they say anything else, the code loops back to the start.

When it loops back however, it doesn't ask again for the answer to 9+10, it just skips over to "Later dude..."

I watched this guy's video to understand how to input strings https://www.youtube.com/watch?v=AgS9JSzbvtU&list=PLZUHVRLMZd8S-F4PupuAgdf0j9C3cA9Sd&index=2

But I think it's causing the problem with this loop. Are there any alternative ways of getting a string input that'll loop properly?

#include <iostream>
using namespace std;

int main()
{
int answer;
string noImNot;
string noImNotCorrect1 = "No I'm not";
string noImNotCorrect2 = "No i'm not";
string noImNotCorrect3 = "no I'm not";
string noImNotCorrect4 = "no i'm not";
string noImNotCorrect5 = "No Im not";
string noImNotCorrect6 = "No im not";
string noImNotCorrect7 = "no Im not";
string noImNotCorrect8 = "no im not";

while (answer != 21)
{
cout << "You stupid\n";
getline(cin, noImNot);
cin.ignore();

bool noImNotBool = (noImNot == noImNotCorrect1 || noImNot == noImNotCorrect2 || noImNot == noImNotCorrect3 || noImNot == noImNotCorrect4 || noImNot == noImNotCorrect5 || noImNot == noImNotCorrect6 || noImNot == noImNotCorrect7 || noImNot == noImNotCorrect8);
//The bool only seemed to work when I put it AFTER the getline, not before...


if (noImNotBool == false)
{
    cout << "Later dude...";
    exit(0);
} else {
cout << "What's 9+10?\n";
cin >> answer;
}
}

cout << "Correct.";




return 0;

}

  • Additionally: `int answer; while (answer != 21)` is undefined behavior, because `answer` is not initialized, but is read. – Algirdas Preidžius Jan 07 '21 at 23:37
  • Okay cheers. Is that part of the problem? I've initialised it as 0 and nothing's changed from what I can see – abstoozie Jan 07 '21 at 23:44
  • Repetitive code like this is a sign it's time to learn about arrays and/or `std::vector`. – tadman Jan 07 '21 at 23:54
  • This would also be a lot easier if you just lower-cased the string, stripped punctuation, and compared it. – tadman Jan 07 '21 at 23:55
  • Hi @abstoozie, it was not nice that someone closed your question without giving you an answer or providing help to improve your question. The problem is cin.ignore(). You also distinguish between the numerical answer and a string answer. A more simplified solution can be found here: http://cpp.sh/2z5sm – UweJ Jan 08 '21 at 00:26
  • @UweJ "_it was not nice that someone closed your question without giving you an answer_" But.. It was closed as a duplicate.. So the answer can be found in the duplicate, this question has been closed with.. – Algirdas Preidžius Jan 08 '21 at 00:57
  • @abstoozie 1) "_Is that part of the problem?_" Technically speaking: Who knows? Could be, or could be not: you never know with undefined behavior. "_I've initialised it as 0 and nothing's changed_" The undefined behavior could've matched the behavior of code having it initialized to any particular value. The behavior is, literally, undefined. What certain, that it was _a_ problem. Not necessarily the one you were asking about. 2) Did you read the duplicate? Actually read it, and not skim through it? – Algirdas Preidžius Jan 08 '21 at 00:59
  • Hey guys sorry I'm really new to this I wasn't meaning to ask questions that've been asked before haha – abstoozie Jan 09 '21 at 01:11

0 Answers0