0

I am having an impossible time trying to make this work. It is just a sample run of the code without the full options.

What is needed from the program is for the user to enter there choice, 1-3 or a-c. I am using a string in case the user enters more than just a single character. The switch case then should compare just the first char in the array to the cases. And the do while loop is to make sure it keeps going until they enter the right characters.

#include <stdio.h>
#define SIZE 81

void main(){
    char thing[SIZE] = {3};
    int rvalue;
    do
    {
        scanf_s("%s", thing);
        switch (thing[0])
        {
        case 'a':
        case 1:
            printf("first\n");
            rvalue = 1;
            break;
        case 'b':
        case 2:
            printf("second\n");
            rvalue = 2;
            break;
        case 'c':
        case 3:
            printf("third\n");
            rvalue = 3;
            break;
        default:
            printf("Wrong\n");
            rvalue = 4;
            break;
        }
    } while (rvalue == 4);
}
3
  • 1
    scanf_s() doesn't work like that. You must specicy the string size. Always check the return value, I/O can fail! Commented Feb 19, 2015 at 9:26
  • 2
    Just change scanf_s("%s", thing); to scanf_s("%s", thing, SIZE);. Commented Feb 19, 2015 at 9:27
  • 2
    Always tell your compiler to show all warnings possible and take them for serious. Commented Feb 19, 2015 at 9:28

2 Answers 2

2

Change

scanf_s("%s", thing);

To

scanf_s("%s", thing,(unsigned int)sizeof(thing)); //Read the comments to know why the cast is required

This done because scanf and scanf_s are different function. scanf_s has an additional argument present to prevent buffer overflows.
Also change these

case 1:
case 2:
case 3:

To

case '1':
case '2':
case '3':

Because the character 1('1') and the rest of them are different from the integer 1. The characters(those enclosed in single quotes) have their values represented in the ASCII table.

Sign up to request clarification or add additional context in comments.

5 Comments

The result of sizeof is of type size_t, but MS scanf_s() expects unsigned int (C11 expects rsize_t).
@cremno , Dosen't size_t have type unsigned int?
No, it can be any unsigned integer type. For example, your code is broken on Windows x64 where sizeof(unsigned int) is 4, but sizeof(size_t) is 8.
@cremno , I see. So, would casting the result of sizeof to unsigned int work?
Of course (I know it's ugly. I also don't know why the person behind scanf_s() decided to use unsigned int.)
1

As far as it seems, you want to print first when the first character in the thing string is a or 1, and so on.

The problem is that case 1: is not same as case '1':. 1 is an int, '1' is a char, and as you are comparing the first character of the string, you need to change your casestatements a bit.

Code:

#include <stdio.h>
#define SIZE 81

void main(){
    char thing[SIZE] = {3};
    int rvalue;
    do
    {
        scanf_s("%s", thing,SIZE);
        switch (thing[0])
        {
        case 'a':
        case '1':
            printf("first\n");
            rvalue = 1;
            break;
        case 'b':
        case '2':
            printf("second\n");
            rvalue = 2;
            break;
        case 'c':
        case '3':
            printf("third\n");
            rvalue = 3;
            break;
        default:
            printf("Wrong\n");
            rvalue = 4;
            break;
        }
    } while (rvalue == 4);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.