Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't get Mike Danes code to work. Here it is, already somewhat fixed by me:

C
#include <stdio.h>
#include <stdlib.h>

/* https://www.mikedane.com/programming-languages/c/building-a-guessing-game/ */

int secretNum = 7;
int guess;
int guessCount = 0;
int guessLimit = 3;
int outOfGuesses = 0;
    while (guess != secretNum && outOfGuesses == 0){
        if (guessCount < guessLimit){
            printf("Enter a guess: ");
            scanf("d", &guess);
            guessCount++;
     } else {
          outOfGuesses = 1;
     }
    }

    if (outOfGuesses != 0){
     printf("You Lose!");
    } else {
     printf("You Win!");
    }


Here is the first error code (the other 2 are similar):
Line 12: error: expected identifier or '(' before 'while'


What I have tried:

I have tried to fix line 13 so that there is a ")" after:
printf("Enter a guess: "
Posted
Updated 29-Dec-20 22:46pm
v3

Look at your code: the while is not inside a function at all - in C, all executable code (other than initialization) has to be inside a function body - in your case probably main

Start here: C - Program Structure[^]
 
Share this answer
 
Comments
Mieczyslaw1683 29-Dec-20 17:21pm    
Okey. Yes, it was a mistake to miss the main function. So now the code looks like this:
#include <stdio.h>
#include <stdlib.h>

/* https://www.mikedane.com/programming-languages/c/building-a-guessing-game/ */

int main() {

int secretNum = 7;
int guess;
int guessCount = 0;
int guessLimit = 3;
int outOfGuesses = 0;
    while (guess != secretNum && outOfGuesses == 0){
        if (guessCount < guessLimit){
            printf("Enter a guess: ");
            scanf("d", &guess);
            guessCount++;
     } else {
          outOfGuesses = 1;
     }
    }

    if (outOfGuesses != 0){
     printf("You Lose!");
    } else {
     printf("You Win!");
    }

}


It is not working as intended. Can someone please help to fix it?
Greg Utas 29-Dec-20 17:33pm    
"It is not working as intended" isn't informative. How does its behavior differ from what you expect?
OriginalGriff 29-Dec-20 17:38pm    
"I expected to be able to hand it in straight away and I can't" I suspect. :sigh:
OriginalGriff 29-Dec-20 17:37pm    
Oh come on!
Even a week two student should be able to see what the problem is with simple code like that, given that they wrote it ...

Here's a hint: what value is in the guess? Why? What do you think you should do about that?

Just copying code from a random website and hoping you can hand it in as your own work is not a good idea: you learn nothing except that your teacher is aware of sites like this and the source, and will probably give you a fail for plagiarism if you aren't kicked out for cheating ...
That code provided by Dane is incomplete (it lacks, as OriginalGriff pointed out, the proper 'framing' in the main function) and incorrect (the scanf format specifier is wrong).
Try
C
#include <stdio.h>

int main()
{
  int secretNum = 7;
  int guess;
  int guessCount = 0;
  int guessLimit = 3;
  int outOfGuesses = 0;

  while(guess != secretNum && outOfGuesses == 0)
  {
    if (guessCount < guessLimit)
    {
      printf("Enter a guess: ");
      scanf("%d", &guess);
      guessCount++;
    }
    else
    {
      outOfGuesses = 1;
    }
  }

  if(outOfGuesses != 0)
  {
     printf("You Lose!");
  }
  else
  {
     printf("You Win!");
  }

  return 0;
}
 
Share this answer
 
Comments
Mieczyslaw1683 30-Dec-20 20:13pm    
Thank you, this code works as intended.
CPallini 31-Dec-20 1:55am    
You are welcome.
That is a really poor piece of code, over complicated and contains a few errors. Here is a simpler working example:
C++
#include <stdio.h>

int main()
{
    int secretNum = 7;
    int guess = 0;
    int guesscount;

    for (guesscount = 0; guesscount < 3; ++guesscount)
    {
        printf("Enter a guess: ");
        scanf("%d", &guess);
        if (guess == secretNum)
        {
            break;
        }
    }
    if (guess == secretNum)
        printf("You Win!");
    else
        printf("You Lose!");

    return 0;
}

The real take home message for you is, do not blindly copy code from the internet, especially when you do not understand it. Time spent actually learning the language is far more productive in the long run.
 
Share this answer
 
v2
Comments
Mieczyslaw1683 30-Dec-20 20:13pm    
How do you learn C programming language if not by YouTube lessons?
Richard MacCutchan 31-Dec-20 3:20am    
Well, as I said, this sample was very poor code, and certainly not a good example of a teaching resource. And most YouTube videos are even worse. If you really want to learn C then get a copy of The C Programming Language - Wikipedia[^] by Kernighan & Ritchie. It is one of the best books written and will teach you the basics in a clear concise fashion.
Mieczyslaw1683 1-Jan-21 14:41pm    
Okey. Thanks for the answer. I like good books.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900