首页 > 解决方案 > comparing an inputted string to a printed function

问题描述

Below is a code I wrote for a dice game called cho han. To input your guess I've used number to represent the words 'odd' and 'even'. Since then I have tried to write it again, but to actually write odd or even in the scanf section, but can't get it to work. Any help would be appreciated :)

//cho-han

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

int main(void)
{
srand(time(NULL));

int x = (rand() % 6) + 1;
int y = (rand() % 6) + 1;
int result = 0;
int guess = 0;

printf("The values of two dice rolls will be added up. The aim is to guess whether that total number will be odd or even.\n");
printf("To guess odd, enter '1'. To guess even, enter '2'.\n\n");
printf("Please enter your guess for the combined total of the two dice rolls: ");

scanf_s("%d", &guess);
if (guess == 2)
{
    printf("\nyour guess is even.\n");
}
if (guess == 1)
{
    printf("\nyour guess is odd.\n");
}
if (guess > 2 || guess < 1)
{
    printf("\nInvalid guess.\nYou lose!\n");
    return (1);
}

printf("\ndice roll 1 = %d\n", x);
printf("dice roll 2 = %d\n", y);

result = x + y;
printf("\ncombined total of both rolls is %d", result);

if (result == 1 || result == 3 || result == 5 || result == 7 || result == 9 || result == 11)
{
    printf("\ncombined total of both rolls is odd.\n");
}
else
{
    printf("\ncombined total of both rolls is even.\n");

}

if (guess == 1 && result == 1 || guess == 1 && result == 3 || guess == 1 && result == 5 || guess == 1 && result == 7 || guess == 1 && result == 9 || guess == 1 && result == 11)
{
    printf("\nYou win!\n");
}
else if (guess == 2 && result == 2 || guess == 2 && result == 4 || guess == 2 && result == 6 || guess == 2 && result == 8 || guess == 2 && result == 10 || guess == 2 && result == 12)
{ 
    printf("\nYou win!\n");
}
else
{
    printf("\nYou lose!\n");
}
return 0;

}

标签: cvisual-studio

解决方案


  1. 你应该scanf_s改为scanf
  2. 该行if (result == 1 || result == 3 ...可能是if (result % 2 == 1) {
  3. 你可以strcmp用来解决你的问题

以下code可以工作:

   //cho-han

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

int main(void)
{
    srand(time(NULL));

    int x = (rand() % 6) + 1;
    int y = (rand() % 6) + 1;
    int result = 0;
    int guess = 0;
    char buf[10];

    printf("The values of two dice rolls will be added up. The aim is to guess whether that total number will be odd or even.\n");
    printf("To guess odd, enter 'odd'. To guess even, enter 'even'.\n\n");
    printf("Please enter your guess for the combined total of the two dice rolls: ");

    fgets(buf, sizeof buf, stdin);
    if (strcmp(buf, "even\n") == 0) {
        guess = 2;
        printf("\nyour guess is even.\n");
    } else if (strcmp(buf, "odd\n") == 0) {
        guess = 1;
        printf("\nyour guess is odd.\n");
    } else {
        printf("\nInvalid guess.\nYou lose!\n");
        return 1;
    }

    printf("\ndice roll 1 = %d\n", x);
    printf("dice roll 2 = %d\n", y);
    printf("\ncombined total of both rolls is %d", x + y);

    result = (x + y) % 2;
    if (result == 1)
        printf("\ncombined total of both rolls is odd.\n");
    else
        printf("\ncombined total of both rolls is even.\n");

    if (guess == result)
        printf("\nYou win!\n");
    else
        printf("\nYou lose!\n");

    return 0;
}

推荐阅读