2

I'm having some problems with the scanf_s(); function or the switch function, the first time I run my code it doesn't recognize the correct char and loops back to the beginning, but after that it works just fine. It is a simple calculator.

There probably is some easy solution to this since I have just started learning programming, but I can't find it.

All the text is in Finnish, but I hope the code itself is understandable.

All feedback is welcome since I am eager to learn what I should and shouldn't do.

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

float luku1 = 0;
float luku2 = 0;
float tulos = 0;
char valinta = '\0';

int main()
{
    system("cls");
    printf("Minkä laskusuorituksen haluaisit tehdä? (+,-,*,/)\n");
    fflush(stdin);
    scanf_s("%c", &valinta);
    switch (valinta){
    case '+':
        printf("Anna yhteenlaskettavat luvut.\n>");
        scanf_s("%f %f", &luku1, &luku2);
        tulos = luku1 + luku2;
        printf("Lukujen summa on %4.2f\n", tulos);
        break;
    case '-':
        printf("Anna vähennettävät luvut.\n>");
        scanf_s("%f %f", &luku1, &luku2);
        tulos = luku1 - luku2;
        printf("Lukujen summa on %4.2f\n", tulos);
        break;
    case '*':
        printf("Anna kerrottavat luvut.\n>");
        scanf_s("%f %f", &luku1, &luku2);
        tulos = luku1 * luku2;
        printf("Lukujen tulo on %4.2f\n", tulos);
        break;
    case '/':
        printf("Anna jaettavat luvut.\n>");
        scanf_s("%f %f", &luku1, &luku2);
        if (luku2 == 0)
        {
            printf("Nollalla ei voida jakaa.\n");
            system("pause");
            main();
        }
        else
        {
            tulos = luku1 / luku2;
            printf("Lukujen jako on %4.2f\n", tulos);
        }
        break;
    default:
        printf("En tunnistanut laskutoimitusta, yritä uudelleen.\n");
        system("pause");
        main();
        break;
    }
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 1
    Learn to use the debugger. – Jonathan Wood Sep 14 '14 at 16:43
  • 1
    Use the return value you get from `scanf()` [and avoid non-standard functions such as scanf_s() ] – wildplasser Sep 14 '14 at 16:44
  • While debugging, after the scanf_s it goes directly to the default case regardless of the input. – Suff0cat1on Sep 14 '14 at 16:45
  • 2
    Use `scanf`, please and it will work. – Igor Pejic Sep 14 '14 at 16:46
  • In the new visual studio it doesn't allow the use of scanf so that's why I'm using scanf_s. – Suff0cat1on Sep 14 '14 at 16:47
  • Can you try `scanf_s(" %c", &valinta);` because sometimes the buffer is filled and fflush(stdin) won't help. – Igor Pejic Sep 14 '14 at 16:52
  • 3
    Use `#define _CRT_SECURE_NO_WARNINGS` or just don't use `scanf` at all but `getchar`/`fgets`/... and `sscanf`/`strtol`/... instead. And as far as I know, `scanf_s` needs an additional size argument for `c`, `s`, and `[` conversions. – mafso Sep 14 '14 at 16:54
  • 2
    Indeed, don't use `scanf_s`. It was previously a nonstandard and awkward interface provided only by Microsoft with their compiler, and it's now an equally-awkward interface tucked away in an optional annex to the standard, and to my knowledge still isn't supported anywhere except MSVC. – R.. GitHub STOP HELPING ICE Sep 14 '14 at 17:12

1 Answers1

0

scanf_s("%c", &valinta); need another parameter. @mafso

 scanf_s(" %c", &valinta, 1);

Adding a space before "%c" will help should code call scanf_s(" %c", &valinta); again.

"The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a *). The first of these arguments is the same as for fscanf. That argument is immediately followed in the argument list by the second argument, which has type rsize_t and gives the number of elements in the array pointed to by the first argument of the pair."

C11dr §K.3.5.3.2 4


Recommend dropping fflush(stdin); as it is non-portable and may/may not work as expected.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256