Little help required...

This is a discussion on Little help required... within the C Programming forums, part of the General Programming Boards category; Hey guys, I got a couple of small little bugs here, for one when my program restarts, it doesn't work ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    16

    Little help required...

    Hey guys, I got a couple of small little bugs here, for one when my program restarts, it doesn't work properly, and I can't figure out why!
    I know the layout is a little messy, I need to try and sort that out, but I was wondering what name I could use for the integer 'z' as 'z' is confusing me!
    Any suggestions?

    Thanks alot!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        int number,
        input,
        a=0,
        z=50,
        fin=0,
        restart=0;
    
        printf("Think of a number between -100 and 100...\n\nI will attempt to guess it. To answer the questions you must enter:\n\n1 for Higher\n2 for Lower\n3 for the Correct answer\n");
        
          while(restart==0) /* restarts program automatically after it's found your number */
            {
            printf("\nThought of that number yet? - Ready when you are!\n\n");     
            printf("Is your number Higher or Lower than 0? ");
            scanf("%d", &input);
    
               if(input==1)
               {
                       number=50;
                       }
                             if(input==2)
                             {
                                     number=-50;
                                     }
                                     if(input==3)
                                     {
                                             printf("Your number is 0");
                                             }
                {
                while(fin==a)
                /* This while loop is used to run the code to ask the next question
                while the game is not over i.e. "fin" is not set to 1 */
                {
                    printf("Is your number Higher, Lower or Correct at %d? ", number);
                    scanf("%d", &input);
                    
                    if(z%2==1)
                    
                    {
                        z=(z/2)+1;
                        /* This checks to see if there is a remainder when "z" is
                        divided by two. 1 is added to the result, if a remainder
                        is found. If "z" cannot be divided by two without a remainder
                        it is rounded up */    
                    }                
                    else
                    {
                        z=z/2;
                        }
                              if(input==1)
                              {
                                      number=number+z;
                              /* If you respond that your number is higher (1) then z is added to "number"*/
                    
                              }
                              if(input==2)
                              {
                                      number=number-z;
                              /* If you respond that your number is lower (2) then z is subtracted 
                              from "number" */
                    
                              }
                              if(input==3)
                              {
                                      printf("\nYour number is %d\n\n", number);
                                      fin=1;
                              /* If you respond 'correct answer'(3) then the integer "number" is displayed
                              and "fin" is set to 1 which makes it drop out of the while loop */
                    }
               }
         }
    }
    system("PAUSE");	
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    take all these inside the restart loop.
    a=0,
    z=50,
    fin=0,
    restart=0;

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    Quote Originally Posted by qqqqxxxx
    take all these inside the restart loop.
    a=0,
    z=50,
    fin=0,
    restart=0;

    Ahhh... Thanks alot, it now restarts properly!
    Do they still have to be declared with their values? Ahh, guess I can see for myself!

    Problem 1 solved, but what do you think I should call 'z' - I can't think of a suitable name which sums up it's job...

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    guess?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    I've changed 'fin' to 'game' and 'a' to 'zero'

    but 'z' to 'guess' - 'z' I suppose guess is slightly related, but I dunno.. any other ideas please?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    attempt?

    You should name your variables, not me.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Lvalue required error
    By eklavya8 in forum C Programming
    Replies: 5
    Last Post: 01-03-2009, 03:47 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21