-1

I've just started programming. I've followed a tutorial video and entered the code like he did. It works for him but not for me and I can't find the mistake. It could be a typo but I've checked multiple times. The required packages to run iostream and so forth are installed. It says the error message in the title:

#include <iostream>
using namespace std;

int main()
{

    int secretNum = 7;
    int guess;

    while (secretNum != guess) {
        cout << "Enter guess please: ";
        cin >> guess;
    }

    cout << "Win";

    return 0;

}
Knampf
  • 117
  • 1
  • 1
    When you do `while (secretNum != guess)`, have you given `guess` a value? – NathanOliver Sep 03 '19 at 17:45
  • variable 'guess' is never initialized and using it in a conditional statement is causing an error. Initializing variable guess should reoslve your issue. – Hemantharaju .H Sep 03 '19 at 17:49
  • That’s not a good tutorial video, but as far as I’m aware, the good beginner’s C++ tutorial video has not been invented yet. – molbdnilo Sep 03 '19 at 17:51
  • You should not watch the videos of whoever uses that code as a supposedly correct example. It is quite clear to anyone used to the language that it is wrong. I hope it was just a copy mistake on your part... – walnut Sep 03 '19 at 17:54
  • 1
    This was obviously not closed for being not reproducible (unless you mean your compiler's error message isn't identical). So where is the typo? – François Andrieux Sep 03 '19 at 17:55
  • Well, if Knampf would confirm that not typing e.g. "=0" is the typo they already suspected, then it would be a typo. But actually I am with you, bringing my reopen-vote along. ;-) – Yunnosch Sep 03 '19 at 18:00
  • Compiling with g++ and running didn't result in any error. Compare with the description in [default initialization](https://en.cppreference.com/w/cpp/language/default_initialization). In principle the undefined `guess` can perfectly be compared with the `secretNum` and result in them being different. – aligur Sep 03 '19 at 18:02
  • 2
    @aligur But `while (secretNum != guess) {` certainly uses an unitialized variable. It was even mentioned twice in the comments before the question was closed. – François Andrieux Sep 03 '19 at 18:03
  • @FrançoisAndrieux Which shouldn't be a problem. Read the link that I added in my comment. – aligur Sep 03 '19 at 18:04
  • @uneven_mark Here's the link to the segment in the video: https://www.youtube.com/watch?v=vLnPwxZdW4Y&t=8333s I maybe should've put it in the original post but I thought it doesn't matter. – Knampf Sep 03 '19 at 18:07
  • 1
    @aligur I don't see what you mean. From the link you gave it clearly says that `guess` will be an indeterminate value. `int` is not a class type and not an array and it has automatic storage duration so it defaults to "initialized to indeterminate values". And it's Undefined Behavior to attempt to get an indeterminate value. Maybe you could point me to the passage that I missed. – François Andrieux Sep 03 '19 at 18:08
  • @FrançoisAndrieux You are reading exactly what I said. It is *undefined behavior*, which 'Compilers are not required to diagnose'. Therefore, the compilation and execution need not result in an error. – aligur Sep 03 '19 at 18:28
  • @aligur Ah, then *"can perfectly be compared"* is very misleading. It seems to me that the phrase does not apply to Undefined Behaviour. I guess you meant that it "compiles without error". But closing every question about code that has UB for not being not reproducible would be excessively counter productive. – François Andrieux Sep 03 '19 at 18:32
  • @Knampf The video isnt terrible, but he makes some mistakes that shouldn't happen. The one you are asking about and forgetting `#include` when using `std::string`, for example, and at one point he mistakenly write `==` multiple times instead of `=`. These mistakes should have been corrected in editing of the video. He also skips over many details that not knowing will cause the learners trouble when writing their own code, for example how uninitialized data behaves. The terminology he sometimes uses seems to come from a different language. (I have only skipped through the video though) – walnut Sep 03 '19 at 18:50
  • @uneven_mark I see your points. I think, he's focusing a bit too much on the bare code instead of the meaning. He explains the basics thoroughly and in a good learning pace but it could be more here and there. To his defense: I remember the "==" thing, he edited his video and said it was a typo (at least the "==" part that I watched). – Knampf Sep 03 '19 at 19:11
  • @Knampf It seems to me that he is mostly show-casing some code, not explaining the language. It skips over the actual process of programming. As comparison, the second book on the [Definitive C++ Book Guide](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) follows up the first Hello World program with two pages explaining possible mistakes one could make and how the compiler reacts. As you noticed, the tutorial you linked doesn't even look at the compiler output for warnings. – walnut Sep 03 '19 at 19:42

2 Answers2

2

Reading the value of an uninitialized variable invokes undefined behavior, maybe your code works properly or maybe not. Try:

do {
    cout << "Enter guess please: ";
    cin >> guess;
} while (secretNum != guess);

Writing on an uninitialized variable is OK.

masoud
  • 55,379
  • 16
  • 141
  • 208
1
int guess;

The guess variable here is uninitialised. The value will be indeterminate.

while (secretNum != guess) {

On this line, you compare another the indeterminate value to another. The behaviour of reading an indeterminate value is undefined.

A trivial solution is to initialise guess with some value (other than 7).

eerorika
  • 232,697
  • 12
  • 197
  • 326