0

When I run the program no matter what I enter for shape, the final else statement is executed.

#include <stdio.h>

int main(void)
{
    char shape = 'a';
    printf("What shape do you want?\nEnter 's' for a Square, 'b' for a Box, 't' for a Triangle\n");

    while (shape != 's' && shape != 'b' && shape != 't')
    {
        scanf_s(" %c", &shape);

        if (shape == 's')
        {
            printf("You entered %c", shape);
        }

        else if (shape == 'b')
        {
            printf("You entered %c", shape);
        }

        else if (shape == 't')
        {
            printf("you entered %c", shape);
        }

        else
        {
            printf("Please enter 's' 'b' or 't'");
        }
    }
    return 0;
}
user2284570
  • 2,891
  • 3
  • 26
  • 74
  • try removing the leading space on the format string. scanf_s is not a good choice for reading a single character. getchar is probably a better choice. – Hogan Apr 12 '14 at 01:49
  • I don't know `C`, but `scanf_s(" %c", &shape);` is not capturing the input. BTW, your `while` will always be true. – yizzlez Apr 12 '14 at 01:49
  • @user3525735 : [Please avoid anything which is related to chating inside posts and comments](http://meta.stackexchange.com/a/3021/242800) (this include thanks and please...). – user2284570 Apr 12 '14 at 02:35

3 Answers3

0

You appear to have a space character in parameter 1.

scanf_s(" %c", &shape);
Steve K
  • 643
  • 6
  • 8
0

scanf_s requires the number of chars you want to read (hence the _s). Try scanf(" %c", &shape, 1).

ooga
  • 15,423
  • 2
  • 20
  • 21
0

Replace scanf_s(" %c", &shape); with either scanf_s(" %c", &shape, (size_t)1); or scanf(" %c", &shape);. You did not provide enough arguments.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118