Unable to get rid of errors even after using the correct headers

I just started to learn how to program in C, I can not get rid of this error. Here is 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");
    }



}

Errors I get:

test.c: 25: 22: error: implicit declaration of function 'strupr' is not allowed in C99 [-Werror, -Wimplicit-function-declaration]

   strcpy(dans, strupr(ans));

                 ^

test.c: 25: 22: error: incompatible pointer conversion integer passing 'int' to a parameter of type 'const char *' [-Werror, -Wint-conversion]

   strcpy(dans, strupr(ans));

                 ^~~~~~~~~~~

/usr/include/string.h:129:70: note: the argument is passed to the '__src' parameter here extern char * strcpy (char * __ restriction __dest, const char * __ restriction __src)

                                                                ^

test.c: 33: 18: error: array comparison is always evaluated as false [-Werror, -Wtautological-compare]

   if ((ans == higher) && (total1 > total))

           ^

test.c: 37: 23: : false [-Werror, -Wtautological-compare]

   else if ((ans == lower) && (total1 < total))

              ^

test.c: 41: 23: : false [-Werror, -Wtautological-compare]

   else if ((ans == equal) && (total1 = total))

, .

,

  • , strupr stdlib, ?

  • , int?

  • % s scanf? ( )

.

+4
3

. strupr C . , , , isupper(), islower() <ctype.h> toupper(), .

. - :

/** convert string to uppercase.
 *  returns string with all chars converted to uppercase.
 */
char *str2upper (char *str)
{
    if (!str) return NULL;

    char *p = str;

    for ( ; *p; p++)
        if ('a' <= *p && *p <= 'z')
            *p += 'A' - 'a';

    return str;
}

- , . , , , .

, , fgets. , '\n' , asn. , , , dans higher lower equal - , ?

" "

'H','I','G','H','E','R'

dans, ( '\n', ):

'H','I','G','H','E','R','\n'

fgets '\n', dand . ?

, , ans, fgets, '\n' '\n' nul-terminating character, . ( , , , EOF, . , , ,

    if (!fgets (ans, 25, stdin)) {  /* check for user cancle with EOF */
        fprintf (stderr, "user canceled input.\n");
        return 1;
    }
    size_t len = strlen (ans);          /* get string length */
    if (len && ans[len - 1] == '\n')    /* valisate last char is '\n' */
        ans[--len] = 0;                 /* overwrite with nul-character */
    else    /* warn if chars can remain unread (input too long) */
        fprintf (stderr, "unread characters may remain in stdin.\n");

    strcpy(dans, str2upper(ans));

dans '\n', .

- . ==. ( , ). strcmp string.h . , strcmp 0. , :

    /*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 (strcmp (dans, higher) == 0 && (total1 > total))
    {
        printf("You're right, it is higher!\n");
    }
    else if (strcmp (dans, lower) == 0 && (total1 < total))
    {
        printf("You're right, it is lower!\n");
    }
    else if (strcmp (dans, equal) == 0 && (total1 == total))
    {
        printf("You're right. they are equal!\n");
    }
    else
    {
        printf("Your prediction was wrong.\n");
    }

, , , , .

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

/** convert string to uppercase.
 *  returns string with all chars converted to uppercase.
 */
char *str2upper (char *str)
{
    if (!str) return NULL;

    char *p = str;

    for ( ; *p; p++)
        if ('a' <= *p && *p <= 'z')
            *p += 'A' - 'a';

    return str;
}

int main(void)
{
    int  dice1, dice2, total, total1= 0;
    char ans[25] = {0}, dans[25] = {0};
    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);
    fputs ("Type higher, lower or equal: ", stdout);

    if (!fgets (ans, 25, stdin)) {  /* check for user cancle with EOF */
        fprintf (stderr, "user canceled input.\n");
        return 1;
    }
    size_t len = strlen (ans);          /* get string length */
    if (len && ans[len - 1] == '\n')    /* valisate last char is '\n' */
        ans[--len] = 0;                 /* overwrite with nul-character */
    else    /* warn if chars can remain unread (input too long) */
        fprintf (stderr, "unread characters may remain in stdin.\n");

    strcpy(dans, str2upper(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 (strcmp (dans, higher) == 0 && (total1 > total))
    {
        printf("You're right, it is higher!\n");
    }
    else if (strcmp (dans, lower) == 0 && (total1 < total))
    {
        printf("You're right, it is lower!\n");
    }
    else if (strcmp (dans, equal) == 0 && (total1 == total))
    {
        printf("You're right. they are equal!\n");
    }
    else
    {
        printf("Your prediction was wrong.\n");
    }
}

/

$ ./bin/rolldice
Will the next number be higher, lower or equal to 6 ?
Type higher, lower or equal: lower
The upper string is LOWER.
You're right, it is lower!

, .

+2

[ ]

% s scanf?

, &s

  // scanf("&s", ans); //had to remove this lin

%s.

+3

[ ]

error: implicit declaration of function 'strupr' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

, strupr . , " ". , , int. C.

, , , strupr() , int,

error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Werror,-Wint-conversion]

, int, 2- strcpy(), .

, C, , strupr(). , C .

, , , :

char * strupr(char * s)
{
  assert(s);

  char * pc = s;
  while (*pc)
  {
    *pc = toupper(*pc); /* toupper() requires <ctype.h> */
    ++pc;
  }

  return s;
}
+2

Source: https://habr.com/ru/post/1693959/


All Articles