the first time my program runs it gets the character from the keyboard
the second time it gets a 10 and I have not pressed any keys on the keyboard

#include <stdio.h>
#include <stdlib.h>
 void showone();
 void showtwo();
 void showthree();

 void showfour();
 void showfive();
 void showsix();
 int randx();
 char getans();
 void blines();



int main(int argc, char **argv)

{ 
     int r;
    char ans;

   ans = getans();
 printf("%c\n",ans);
   while(ans== 'y')
    {
      r = randx(6);
      printf("%d\n",r);
      printf("%c\n",ans);
      blines(25);
      if (r==1) showone();
      if (r==2) showtwo();
      if (r==3) showthree();
      if (r==4) showfour();
      if (r==5) showfive();
      if (r==6) showsix();
      blines(21);
      ans = getans();
    }
     system("PAUSE");
}
 
 void showone()
  {
    printf("\n * \n");
  }

 void showtwo()
  {
    printf(" * \n\n");
    printf(" * \n");
  }

 void showthree()
  {
    printf(" *  \n");

    printf("  * \n");
    printf("   *\n");
  }

 void showfour()
  {
    printf(" * * \n\n");
    printf(" * * \n");
  }

 void showfive()
  {
    printf(" * * \n");
    printf("  *  \n");
    printf(" * * \n");
  }

 void showsix()
  {
    int i;

    for(i=1 ; i>=3 ; i++) printf(" * * \n");
  }


 void blines(int n)
  {
    int i;

    for(i=1 ; i<=n ; i++) printf("\n");
  }


char getans()
 {
   int ans;

   printf("Throw y/n ?");
   ans = -1;
  
   while (ans == -1)
    {
     

      ans=getchar();
    
     
    }
  
   return ans;

}

int randx(int n)
 {
   return rand()%n + 1;
 }

Recommended Answers

All 5 Replies

10 is typically the numeric value of '\n'. Enter is a character too, so when you type a character and then press Enter, that puts two characters on the stream. See if you can find the problem with that explanation of your output.

Try to clear the contents of the stream at strategic points. Read more on "flushing the standard input stream in c". :)

You can code the getans() function in a far better way.

And asking you out of curiosity "Being a newbie how did you know it was taking 10 as the input? Because as far as I have seen your program you are never printing the decimal(ascii)value of char variable ans at any point.":?:

When you input for "Throw y/n ?"; It means you pressed two keys, first is 'y' or any other and second is carriage return. After that getchar() function pops up two characters one after one and this is the reason your while loop is not working as you expect.

well, I was debuging the code with printf statements, I left them out of the code I sent to this form. I did not want to confuse the issue.

After I sent the code to the form I went back to the web and found
fflush(stdin) that solved my problem.
Thank you for responding.

Try to clear the contents of the stream at strategic points. Read more on "flushing the standard input stream in c". :)

You can code the getans() function in a far better way.

And asking you out of curiosity "Being a newbie how did you know it was taking 10 as the input? Because as far as I have seen your program you are never printing the decimal(ascii)value of char variable ans at any point.":?:

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.