challenge#2 help

+2 krishna dhoot · March 6, 2015
the following program is working but the output is not perfect as in after 1st output you get another extra fixed output.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int main()

 int i , a, b,c, sum, nextsum;
    char comp ;
    a=(rand()%6) + 1;
    b=(rand()%6) + 1;
    c=(rand()%6) + 1;
    sum = a+ b+ c;
    printf("%d \n", sum);
for(i=0;i<50 ;i++)
    {    a=(rand()%6) + 1;
         b=(rand()%6) + 1;
         c=(rand()%6) + 1;
         nextsum = a+ b+ c;
         printf("will the next sum be higher(h) or lower(l)? \n");
         if ((nextsum>sum && comp=='h') )
             printf("you rock! and the nextsum is %d \n",nextsum);
         else if( (nextsum<sum && comp=='l'))
              printf("you rock! and the next sum is %d \n",nextsum);
             printf("you suck! and the next sum is %d \n", nextsum);

return 0;

Post a Reply


Oldest  Newest  Rating
0 Jeff T. · March 7, 2015
 In your scanf("%c", &comp) line, you need to do one of two things;
     either put a space in before %c like this (" %c", &comp) or
     keep scanf as is and on the line below it add getchar():

This is an issue with scanf which I won't try to explain, I would probably do more damage than good, but this will get your program working correctly.
Bucky touches on this in one of his tutorials but I don't remember which one. If you really want to know whats going on, Google scanf not working correctly, you'll get a bunch of explanations why.
I had the same frustrating experience myself. Good luck. 
0 krishna dhoot · March 7, 2015
thank you!
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator