Hi!
I am Abdul-Majeed.
I'm a new student to c. I tried writting a game for random multiplication calculation.
This was a similer questions for me in my midterm exams.
I need friends to inprove it for me.
You can also find faults and make corrections to my code

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>


void guesgame(void);
int main ()
{
    char name[30];

    printf("Please enter your name:\n ");

    scanf("%s",name);

    printf("Hello! %s and welcome to Abdul-Majeed's Multiplication game\n",name);
    printf("Rules for the game:\n1. You will get 5 points for every correct answer\n2. You will loss 5 points for every wrong anwer\n"
            "3.The game will end if you score up to 100\nIt will also end when you get 3 questions wrong\nWish you good luck!!!\n");

guesgame();

getch ();
return 0;
}
void guesgame(void)
{


    int result,answer,x,y;
     int i=0,j=0;


while(1)
  {
       srand(time(NULL));

         x = 1+rand()%10;

         y = 1+rand()%10;

        result = x*y;

        printf("%d * %d = ",x,y);

        scanf("%d",&answer);

        if (answer == result)
    {
            printf("\nGood you had it ryt! you have 5 points\n");
            i +=5;


    }
        if (answer != result )
        {
               printf("\noops! it's wrong. you get -5\n");
              j -=5;
        }
             if(i == 100 || j == -15)
             {
              printf("Sorry you have no more chance\n\n"
               "Thank you for playing Abdul-Majeed's game\n\n"
                 "Your Total points = %d\n",i);
                    getch ();

             }
  }




}

Recommended Answers

All 4 Replies

A few things:

  • Instead of doing this - int i=0,j=0;, int result,answer,x,y;. It's much more intuitive to use descriptive names. It also helps readability to declare variables on separate lines:

     int result = 0;
     int answer = 0;
     int mult1 = 0;
     int mult2 = 0;
     int score = 0;
     int errors = 0;
    
  • srand should only be initialized once, probably at the beginning of main.

  • try to be more consistent with your indenting. If the IDE you're using doesn't have options to fix the formatting I would suggest changing to one that does(I.E. Code::Blocks)

  • Your while loop doesn't have a clean exit. Creating a boolean variable(bool done = false) to tell the loop to exit when the game is over, is much more intuitive(while(!done)).

  • the final score doesn't factor in the errors made:

    if (score == 100 || errors == -15)
    {
        printf("Sorry you have no more chance\n\n"
            "Thank you for playing Abdul-Majeed's game\n\n"
            "Your Total points = %d\n", score + errors);
        done = true;
        getch();
    }
    

All together it would look something like this:

int main()
{
    srand(time(NULL));
    char name[30];
    printf("Please enter your name:\n ");
    scanf("%s", name);
    printf("Hello! %s and welcome to Abdul-Majeed's Multiplication game\n", name);
    printf("Rules for the game:\n1. You will get 5 points for every correct answer\n2. You will loss 5 points for every wrong anwer\n"
        "3.The game will end if you score up to 100\nIt will also end when you get 3 questions wrong\nWish you good luck!!!\n");
    guesgame();
    getch();
    return 0;
}
void guesgame(void)
{
    int result = 0;
    int answer = 0;
    int mult1 = 0;
    int mult2 = 0;
    int score = 0;
    int errors = 0;
    bool done = false;
    while (!done)
    {
        mult1 = 1 + rand() % 10;
        mult2 = 1 + rand() % 10;
        result = mult1*mult2;
        printf("%d * %d = ", mult1, mult2);
        scanf("%d", &answer);
        if (answer == result)
        {
            printf("\nGood you had it ryt! you have 5 points\n");
            score += 5;
        }
        if (answer != result)
        {
            printf("\noops! it's wrong. you get -5\n");
            errors -= 5;
        }
        if (score == 100 || errors == -15)
        {
            printf("Sorry you have no more chance\n\n"
                "Thank you for playing Abdul-Majeed's game\n\n"
                "Your Total points = %d\n", score);
            done = true;
            getch();
        }
    }
}
commented: I really enjoy seeing the "old dogs of war" taking time with support like this. +4

Wow!
I am verry glad of that.
Thank you very much for the corrections.

Please remember to mark the post solved if your question is answered Thank You

My question is answered but I wish I could get more improvement on the code
if any by other members.

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.