Can you tell me what's wrong with this program? PLEASE!

+2 Sandra T · September 9, 2015
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>


 int main()
{
  int a,b,ok,no;
  char r;
  ok=no=0; a=0; b=0;
  printf("WELCOME! YOUR FIRST SUM OF 3 NR. IS ");
  a=((rand()%6)+1)+((rand()%6+1)+1)+((rand()%6)+1);
  printf(" %d\n", a);
  printf("HOW WILL BE THE NEXT?\nPRESS:\nh-FOR HIGHER\nl-FOR LOWER\ns-FOR THE SAME\ne-FOR EXIT GAME\n");

 do {
    scanf(" %c\n", &r);
    b=((rand()%6)+1)+((rand()%6)+1)+((rand()%6)+1);
    if (r=='s')  {if (a==b) {printf("GREAT!\n"); ok++; break;}else{printf("STUPID\n!"); no++; }
                 }else{ if (r=='h') {if (b>a) {printf("GREAT!\n"); ok++; break;}else{printf("STUPID!\n"); no++; }
                                   }else{if (r=='l') {if (b<a) {printf("GREAT!\n"); ok++; break;}else{printf("STUPID!\n"); no++; }
                                         }else{printf("READ THE RULES!\n");}}}
  printf("HOW WILL BE THE NEXT?\n");

  a=b;
  }while(r=='s' || r=='l' || r=='h');
  printf("END OF GAME!\nGOOD POINTS: %d\nBAD POINTS: %d ", ok,no);
 return 0;
}

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Jon Z. · September 10, 2015
specify what the problem you are encountering is.

get rid of the \n in scanf


scanf(" %c", &r);
0 Jon Z. · September 10, 2015
also insted of
if()
{
}
else
{
if()
{
}
else
{
}
//...
}


use 

if()
{
}
else if()
{
}
else if()
{
}
else
{
}
+1 Jon Z. · September 10, 2015
ok. i fixed your program. i'm assuming you want to continue playing until u enter e. you need to get rid of the break statements then, or else your program exits the loop.


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>


int main()
{
    int a,b,ok,no;
    char r;
    ok=no=0;
    a=0;
    b=0;
    printf("WELCOME! YOUR FIRST SUM OF 3 NR. IS ");
    a=((rand()%6)+1)+((rand()%6+1)+1)+((rand()%6)+1);
    printf(" %d\n", a);
    printf("HOW WILL BE THE NEXT?\nPRESS:\nh-FOR HIGHER\nl-FOR LOWER\ns-FOR THE SAME\ne-FOR EXIT GAME\n");

    do
    {
        scanf(" %c", &r);
        b=((rand()%6)+1)+((rand()%6)+1)+((rand()%6)+1);
        if (r=='s')
        {
            if (a==b)
            {
                printf("GREAT!\n");
                ok++;
            }
            else
            {
                printf("STUPID\n!");
                no++;
            }
        }
        else if (r=='h')
        {
            if (b>a)
            {
                printf("GREAT!\n");
                ok++;
            }
            else
            {
                printf("STUPID!\n");
                no++;
            }
        }
        else if (r=='l')
        {
            if (b<a)
            {
                printf("GREAT!\n");
                ok++;
            }
            else
            {
                printf("STUPID!\n");
                no++;
            }
        }
        else
        {
            printf("READ THE RULES!\n");
        }


        printf("HOW WILL BE THE NEXT?\n");

        a=b;
    }
    while(r != 'e');
    printf("END OF GAME!\nGOOD POINTS: %d\nBAD POINTS: %d ", ok,no);
    return 0;
}
0 Sandra T · September 10, 2015
do {
    scanf(" %c", &r);
    b=((rand()%6)+1)+((rand()%6)+1)+((rand()%6)+1);
    if (r=='s')  {if (a==b) {printf("GREAT!\n"); ok++; break;}else{printf("STUPID\n!"); no++; }
                 }else if (r=='h') {if (b>a) {printf("GREAT!\n"); ok++; break;}else{printf("STUPID!\n"); no++; }
                                   }else if (r=='l') {if (b<a) {printf("GREAT!\n"); ok++; break;}else{printf("STUPID!\n"); no++;}
                                         }else{printf("READ THE RULES!\n");}
   if (r=='s' || r=='h' ||r=='l') {printf("HOW WILL BE THE NEXT?\n");}

  a=b;
  }while(r=='s' || r=='l' || r=='h');
  printf("END OF GAME!\nGOOD POINTS: %d\nBAD POINTS: %d ", ok,no);
  return 0;


THANK YOU! I'VE MADE CORRECTIONS, BUT NOW I HAVE ANOTHER PROBLEM...WHEN I GUESS HOW WILL BE THE SUM, THE PROGRAM STOPS...IT DOESN'T LET ME CONTINUE (I CAN CONTINUE ONLY WHEN I'M WRONG).  CAN YOU TELL ME WHY?
0 Sandra T · September 10, 2015
It works! Thank you very much!:D
0 Sandra T · September 10, 2015
It works! Thank you very much!:D
0 Jon Z. · September 11, 2015
the problem is that you were using break; , which exits the loop. instead you could use continue; ,which goes back to the beginning of the loop. 
0 Sandra T · September 11, 2015
You're right! I didn't even think about that...It works...
I sent  the last message with the program because I was too concentrated and I didn't realized that you sent me the good version of the program. But now I know what mistake i've made when I used the word 'break'. 
THANK YOU AGAIN!!!
0 Jon Z. · September 11, 2015
;)

feel free to pm me if you have any question.
0 Sandra T · September 12, 2015
Ok, I will... :-)
  • 1
  • 2

C

107,183 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator