I cant figure why my program is not running it compiles with no errors but doesnt run, am bearly starting to learn c , am more proefficient in java but its wierd changing, i use vi to write my code. heres a snipet

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
 main()
{
 int numberofplays = 0;
 float average = 0;
 int numberguesses = 0;

 printf("Playing guess my number game\n");
 printf("-------------------------------\n");
 bool play = true;
 int xtry = 0;
 int totaltries = 0;
 char answer;
 while(play==true)
 {
   xtry = playgame();
   numberofplays++;
   totaltries = totaltries + xtry;
   printf("Play again? (y for yes n for no): ");
   answer = getchar();
   if(answer =='n')
   {
     play = false;
   }

 }
 average = totaltries/numberofplays;
 printf("average is %f tries per game\n",average);
 return 0;
}
int playgame()
{

        int guessnumber = 50;
        int xnumber = 25;
        int count=0;
        char x;
        bool stillguessing = true;

        while(stillguessing == true);
        {
                printf("is your number %d ?", guessnumber);
                x = getchar();

                if(x=='=')
                {
                        count++;
                        printf("Great!\n");
                        stillguessing=false;
                        return count;
                }
                else if(x == '<')
                {
                        guessnumber = guessnumber - xnumber;
                        xnumber = xnumber/2;
                        if(xnumber<1)
                                xnumber = 1;
                        count++;
                }
                else if (x == '>')
                {
                        guessnumber = guessnumber +xnumber;
                        xnumber = xnumber/2;
                        if(xnumber<1)
                                xnumber = 1;
                        count++;
                }

        }

}

My program is basically a guessng game where user thinks a number from 1-100 in his head and my prgram guesses the number , the user enters <,>,= when the guessed number is either less , more or equal

while(stillguessing == true);

There is nothing in that while loop, so it loops forever.
I expect you meant

while(stillguessing == true)

without the semi-colon

  1. You need to provide a function prototype for playgame()

  2. Line 43: remove the semicolon at the end of the line.

  3. line 70: you need to provide an else without any conditions to account for cases when the user enters something else.

  4. I think the biggest problem is line 30: an int divided by another int is still an int, not a float. integers have no decimal places, to 1/2 is 0 not 0.5. In order to correct that typecast either the numerator or denominator to float.

On line 43, while(stillguessing == true); you have actually results in an infinite loop if stillguessing is true, because of the semicolon. Remove the semicolon to fix it.

Ok thanks, 1st comment is right dint even see that extra semicolon, now its running. The for the average do you typecast like in java then? , and for some reason is note even getting to the code with the average, and its also repeating line 22 and skipping that part and goes back to playing, this is some output i get

is your number 50 ?<
is your number 25 ?is your number 25 ?>
is your number 37 ?is your number 37 ?=
Great!
Play again? (y for yes n for no): is your number 50 ?

PS: is there an ide thats helpful with c , i use netbeans for java and other codeing but not sure if i can use it with c.

getchar() only retrieves one keystroke from the keyboard buffer. When you type something you have to also hit the <Return> key, right? The Return key is put into the keyboard buffer along with the other keys you type. After calling getchar() you need to clear the keyboard buffer of all remaining keys. Unfortunately there is no standard way of doing that.

After calling getchar() you need to clear the keyboard buffer of all remaining keys. Unfortunately there is no standard way of doing that.

Well, there's a standard method, but it's situational in that there must be at least one character pending in the stream if you don't want to block for input:

/*
    @description:
        Extracts and discards characters from the stream pointed
        to by in until a newline is found, end-of-file is reached,
        or a stream error is detected.
*/
void clear_line(FILE *in)
{
    if (!feof(in)) {
        int ch;

        do {
            ch = getc(in);
        } while (ch != '\n' && ch != EOF);

        if (feof(in) && !ferror(in)) {
            /*
                Preserve an error state, but remove 
                an end-of-file state if we caused it.
            */
            clearerr(in);
        }
    }
}

The problem is that you have to be sure there are extraneous characters in the stream before calling clear_line() because the following will just pause and wait for input:

int main(void)
{
    clear_line(stdin);
    return 0;
}

So you're replacing one subtle problem with another subtle problem. ;)

So you're replacing one subtle problem with another subtle problem. ;)

That's why I said there is no standard way to clear the keyboard buffer. Nonstandard way is to use _kbhit() from conio.h

while( _kbhit())
   getch();

Or on *nix look into termcap library.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.