I've just started learning how to program in C, I'm not able get rid of the error. Here's my program:
/* This program rolls two dice and presents the total. It then asks the user
to guess if the next total will be higher, lower, or equal. It then rolls
two more dice and tells the user how they did. */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
int dice1, dice2, total, total1= 0;
char ans[25], dans[25];
char higher[] = "HIGHER", lower[] = "LOWER", equal[] = "EQUAL";
//the following 3 lines, throws the dice and adds them.
dice1 = (rand() % 5) + 1;
dice2 = (rand() % 5) + 1;
total = dice1 + dice2;
//the next few ask the question.
printf("Will the next number be higher, lower or equal to %d ?\n", total);
puts("Type higher, lower or equal.");
// scanf("&s", ans); //had to remove this line, because apparently we can't use &s to get the string input
fgets(ans, 25, stdin);
strcpy(dans, strupr(ans));
//the next few throw the dice two more times
dice1 = (rand() % 5) + 1;
dice2 = (rand() % 5) + 1;
total1 = dice1 + dice2;
/*All of these check if the user input matches the actual output and
then tells the user if he/she was right.*/
printf("The upper string is %s.\n", ans);
if ((ans == higher) && (total1 > total))
{
printf("You're right, it is higher!\n");
}
else if ((ans == lower) && (total1 < total))
{
printf("You're right, it is lower!\n");
}
else if ((ans == equal) && (total1 = total))
{
printf("You're right. they are equal!\n");
}
else
{
printf("Your prediction was wrong.\n");
}
}
The errors which I'm getting:
test.c:25:22: error: implicit declaration of function 'strupr' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
strcpy(dans, strupr(ans)); ^
test.c:25:22: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Werror,-Wint-conversion]
strcpy(dans, strupr(ans)); ^~~~~~~~~~~
/usr/include/string.h:129:70: note: passing argument to parameter '__src' here extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
test.c:33:18: error: array comparison always evaluates to false [-Werror,-Wtautological-compare]
if ((ans == higher) && (total1 > total)) ^
test.c:37:23: error: array comparison always evaluates to false [-Werror,-Wtautological-compare]
else if ((ans == lower) && (total1 < total)) ^
test.c:41:23: error: array comparison always evaluates to false [-Werror,-Wtautological-compare]
else if ((ans == equal) && (total1 = total))
Please help me with the errors.
Also,
strupr is supposed to be there in
stdlib
, why am I still getting an error?When I convert a string to an uppercase string how does it change to int?
Why am I not able to use
%s
inscanf
? (I have used this before)
Thank you.