HiveBrain v1.2.0
Get Started
← Back to all entries
patterncModerate

Simple random number guessing game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomnumbersimplegameguessing

Problem

I made this simple game code. Can you please give me some advice on how I can improve it?

#include
#include
#include
#include 
#include 

void main ()
{
    printf("\t\tWelcome to our gessing game\n\n\n");
    printf("the computer choose a random number between 1-99, try to gess the random number...");

    srand(time(NULL));

    int gess,i=0,found=0,  r = rand()%100;

    while (ir)
        {
            printf("\n Too big\n");
            i++;
        }
        else if(gess<r)
        {
            printf("\n Too small\n");
            i++;
        }
    }
    if (found==1&&gess==1)
        printf("\n Very good the number is %d this is your %dst gess",gess,i);
    else if(found==1&&gess==2)
        printf("\n very good the number is %d this is your %dnd gess",gess,i);
    else if(found==1&&gess==3)
        printf("\n very good the number is %d this is your %drd gess",gess,i);
    else if(found==1&&gess!=1&&gess!=2&&gess!=3)
        printf("\n very good the number is %d this is your %dth gess",gess,i);
    else
        printf("\n Never mind try again");
    getch();
}

Solution

One variable per line please.

int gess,i=0,found=0,  r = rand()%100;


This makes the code hard to read. There is also one corner case (with pointers) were this will not work as expected for beginners. As a result this is usually banned in most companies coding standards.

You are using found as a boolean variable.

Treat it as such. use TRUE/FALSE.

I would change the while() into a for(;;) loop. Then you can remove all the increments.

while (i<10 && found==0)

--

for(int i = 0; i < 10 && !found; ++i)


White space is your friend:

if (found==1&&gess==1)  // This is hard to read and will get worse with more conditions.

if (found && (guess == 1)) // much more readable
//  ^^^^^  boolean use it as such.


Your "st", "nd", "rd", "th" is not correct.

10-19    => th
x1       => st
x2       => nd
x3       => rd
x[4-9]   => th


Also you print basically the same string every time on success. So we not have one print statement and just separate the logic for getting the ending.
I would change the logic:

if (!found)
{
    printf("\n Never mind try again");
}
else
{
    char const* ext = "th";
    if ((gess = 20))  // 10->19 already set
    {
        int end = gess % 10;
        if      (end == 1) { ext = "st";}
        else if (end == 2) { ext = "nd";}
        else if (end == 3) { ext = "rd";} 
    }

    printf("\n Very good the number is %d this is your %d%s gess", gess, i, ext);
}

Code Snippets

int gess,i=0,found=0,  r = rand()%100;
while (i<10 && found==0)

--

for(int i = 0; i < 10 && !found; ++i)
if (found==1&&gess==1)  // This is hard to read and will get worse with more conditions.

if (found && (guess == 1)) // much more readable
//  ^^^^^  boolean use it as such.
10-19    => th
x1       => st
x2       => nd
x3       => rd
x[4-9]   => th
if (!found)
{
    printf("\n Never mind try again");
}
else
{
    char const* ext = "th";
    if ((gess < 10) || (gess >= 20))  // 10->19 already set
    {
        int end = gess % 10;
        if      (end == 1) { ext = "st";}
        else if (end == 2) { ext = "nd";}
        else if (end == 3) { ext = "rd";} 
    }

    printf("\n Very good the number is %d this is your %d%s gess", gess, i, ext);
}

Context

StackExchange Code Review Q#23657, answer score: 10

Revisions (0)

No revisions yet.