Running c language and not working.

Asked 2 weeks ago, Updated 2 weeks ago, 0 views

ERROR: When running

#####1st password######

 4 x 3 is?

 #####################################

Enter password (exit :-1)>>

If you enter an integer, you won't get the next question Press any text and type enter to print out the last question at once and end it.

  ####### The first password #####

     ? 1 4 x

#####################################

Please enter your password (Termination : - 1) > > a.

> > ttyaeng, wrong.



####### the second password #####

     10? 12 x

#####################################

Please enter your password (Termination : - 1) > >.
> > ttyaeng, wrong.



The password the third ####### #####

     ? 7, 13 x

#####################################

Please enter your password (Termination : - 1) > >.
> > ttyaeng, wrong.



The fourth code ####### #####

     25? 3 x

#####################################

Please enter your password (Termination : - 1) > >.
> > ttyaeng, wrong.



#######5th password######

     32 x 8 is?

#####################################

Enter password (exit :-1)>>
>>Sting! Wrong.


 You got 0 out of 5 passwords

C:\Users\qoral\source\repos\Exercise Myproject\x64\Debug\Exercise Myproject.exe (process 9364) has ended (code: 0).
Press any key to close this window...

It's a code.

#include <stdio.h>
#include <time.h>
int getRandomNumber(int level);
void showQuest(int level,int num1, int num2);
void success();
void fail();

    int main(void)
    {


    //There are 5 doors, and each door has an increasingly difficult formula quiz (random)
    //If you get it right, you pass, if you get it wrong, you fail
    srand (time(NULL));
    int count = 0;
    for (int i = 1; i <=5; i++)
    {
        //x*y=?
        int num1 = getRandomNumber(i);
        int num2 = getRandomNumber(i);
            //printf("%d*%d?",num1,num2);
        showQuest(i, num1, num2);

        int answer;
        scanf_s(" %d ", &answer);
        if (answer == -1)
        {
            printf ("Exit the program"\n");
            exit(0); // exit the program
        }
        else if (answer == num1 * num2)
        {

            //Success
            success();
            count++;
        }
        else
        {
            //Failed
            fail();
        }
    }
    printf("\n\n You got %d out of 5 passwords\n", count);
    return 0;
}

int getRandomNumber(int level)
{
     return rand() %(level * 7) + 1;
}
    void showQuest(int level, int num1, int num2)
    {
    printf("\n\n#####%d" password######\n",level);
    printf("\n\t %d x %d"?\n\n", num1, num2);
    printf("#####################################\n");
    printf("\nEnter your password (exit :-1)>>");
}

void success()
{
    printf("\n>> good!) Correct.\n");
}    
void fail()
{
    printf("\n>> Wrong! Incorrect.\n");
}

c

2022-09-20 11:04

1 Answers

scanf_s(" %d ", &answer);

The code above is the problem.

As of %d, there is one space on the left and right, but you should not use the space carelessly. The space before %d in the current code is not a big deal. However, spaces after %d must be deleted.

The preceding space means that the number has a space character before it or does not exist, and the space after it means that the number must be followed by a space.

Delete all spaces on the left and right sides of %d as shown below.

scanf_s("%d", &answer);


2022-09-20 11:04

If you have any answers or tips


© 2022 pinfo. All rights reserved.