-2

I created a program that calculates Area of geometric shapes. At the beginning it asks user to enter a letter to select the shape for which he wants to calculate the area. Letters don't work I don't know why so I use numbers instead. Can anyone help me with that?

int letter;
printf("1 for triangle\n2 for square\n3 for rectangle: ");
scanf("%d", &letter);
if (letter == 1)
...

I want it to be like t for triangle, s for square and so on.

Dorin Baba
  • 1,578
  • 1
  • 11
  • 23

1 Answers1

0

You can do something like this, store your input string in a buffer and then check user choice with strcmp, which compares first argument with second one.

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


int main (void) {
    char s[3];
    
    printf("Choose geometric shape: <t> for triangle | <s> for square | <r> for rectangle\n");
    scanf(" %s",s);
    
    if (strcmp(s, "t")==0) {
         printf("TRIANGLE\n");
        /* do stuff */
    }
    else if (strcmp(s, "r")==0) {
        printf("RECTANGLE\n");
        /* do stuff */
    }
    else if (strcmp(s, "s")==0) {
        printf("SQUARE\n");
        /* do stuff */
    }
    else {
        printf("Unknown input\n");
    }
    
    return 0;
}
Matteo Pinna
  • 409
  • 4
  • 9