Thread: little help with nonassigned variable error

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    14

    little help with nonassigned variable error

    this is a basic program for playing a number game where you get 6 guesses to find a number, however im getting an error for 'gotit' not being assigned to a variable that is used in the function main and i can't seem to find the error
    Code:
    #include <stdio.h>
    #define TRUE 1
    #define FALSE 0
    #define THENUMBER 17
    #define MAXGUESSES 6
    void greet(void);
    int prompt1(void);
    void prompt2 (void);
    int getresp(void);
    int playagain(void);
    void goodbye(void);
    int main()
    {
     int play,gotit,thenumber,ans,numguesses;
     greet();
     play = TRUE;
     gotit = FALSE;
     
     while(play){
      thenumber= prompt1();
      gotit = FALSE;
      numguesses = 0;
      
      while(!gotit&& numguesses <MAXGUESSES) {
      prompt2();
      ans=getresp();
      numguesses++;
      
      if(ans==thenumber){
      printf("****correct!!!!*****\n\n");
      gotit=TRUE;
      play=playagain();
      }
      else {
       if(ans<thenumber)
       printf("too low!\n");
       else if (ans>thenumber)
       printf("too high!\n");
       gotit=FALSE;
       }
      }
      if (numguesses >=MAXGUESSES)
      printf("too many guesses\n");
     }
     goodbye();
     return 0;
    }
    void greet(void)
    {
    printf( "Welcome to the game of Guess It!\n" );
    printf( "I will choose a number between 1 and 100.\n" );
    printf( "You will try to guess that number. If you guess wrong,\n" );
    printf( "I will tell you if you guessed too high or too low.\n" );
    printf( "You have 6 tries to get the number.\n" );
    }
    int prompt1(void)
    {
    printf("\nOK, I am thinking of a number. Try to guess it.\n\n");
    return THENUMBER;
    }
    void prompt2(void)
    {
    printf("your guess?");
    }
    int getresp(void)
    {
    int resp;
    scanf("%d",&resp);
    return(resp);
    }
    int playagain(void)
    {
    int iochar;
    while((iochar=getchar())!='\n') /*clears input buffer*/
    ;
    printf("want to play again?");
    iochar =getchar();
    if(iochar=='y' || iochar=='Y')
     return 1;
     return 0;
    }
    void goodbye (void)
    {printf("goodbye, it was fun. play again sometime.\n");
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    my hair is on fire been coding since noon T.T

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Paste the actual error, not a vague approximation. "gotit' not being assigned to a variable that is used in the function main" is never going to be something a compiler will print.

    Also, I can see why you're confused (don't double post, btw), it might be because the indentation is awful. Fix that first.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    WARNING W8004 'gotit' is assigned a value that is never used in function main (on line 17)

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    and its indented on my compiler just didnt copy over right

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    That's:
    a) Obviously not an error, you can tell because it reads "WARNING".
    b) Something that's pretty self-explanatory in it of itself.

    You're never using the value you put in the variable "gotit". The compiler needs to take this seriously, because it leads to one of two (both bad) situations:
    - Later in the program, the value is not getting read correctly, making the "gotit" value unused. It could be this, I haven't checked what actually happens.
    - It doesn't ever get used, and it's a wasted instruction that you should remove.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It would be a warning, not an error. Still, it is a good idea to eliminate warnings.

    The first assignment of gotit is FALSE. The next assignment (near the top of the while (play) loop) also assigns it to FALSE. The first assignment is redundant.

    It also would probably be better to define gotit inside the while (play) loop, since it is never used outside that loop. As a rule of thumb, always define variables in the smallest scope possible - don't allow them to exist any longer than they need to.

    Your indentation, incidentally, makes that code really hard to understand.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    thank you! sorry about the indentation i'm new to coding and still am trying to figure out the best way to indent it, very helpful grumpy

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    "go tit" might not be the best name for your variable anyway. Oh wait sorry that's "got it".
    How about just calling it "win", and there's no use justting setting the variable and then doing nothing with it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    not a big problem,just a warning

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by peter__barnes View Post
    not a big problem,just a warning
    Put down your keyboard, step away from the computer.

    Unless you accept that the only viable approach is "zero warnings", you'll soon find yourself with builds containing hundreds of warnings. At which point, you'll just miss the new warning that you should really be paying attention to.

    So instead of being able to see it instantly, you go on a nice long bug hunting session to find a problem the compiler told you about in the beginning.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. little help with nonassigned variable error
    By dstrain42 in forum C Programming
    Replies: 3
    Last Post: 03-16-2012, 04:07 PM
  2. Error in return variable.
    By jericjones45 in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2009, 09:22 PM
  3. Variable being used without being defined error
    By Moony in forum C Programming
    Replies: 2
    Last Post: 06-25-2006, 10:09 AM
  4. very weird error with a variable
    By paperbox005 in forum C Programming
    Replies: 4
    Last Post: 05-23-2005, 10:33 AM
  5. variable initialization error...
    By sunoflight77 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2005, 12:01 AM