0

I'm trying to build a simple calculator for a class but for some reason the program keeps crashing.
I looked around and didn't see anything that can tell me what's wrong so I figured I'll ask here.
Now the trick is that we only learned up to if/else statements so that's the only functions we can use.

#include <stdio.h>

void main() {
  float num1, num2;
  int type;
  char oper;

  printf_s("Please enter your choise:\n1. Decimal calculator.\n2. Binary            calculator.\n");
  scanf_s("%d", &type);

  if (type == 1) {
    printf_s("Please enter the equation you want to solve and then press Enter:\n");
    scanf_s("%f %c %f",&num1,&oper,&num2);
  }
}

Anyone has any idea what's wrong here? Everytime I input 1 + 1 for example the program crashes.

Thank you!

mortalis
  • 2,060
  • 24
  • 34
user7792712
  • 23
  • 1
  • 5
  • 3
    `void main()`?? Update to a latest resource. – Sourav Ghosh Mar 30 '17 at 14:46
  • 1
    Check the return value of `scanf()`. – Sourav Ghosh Mar 30 '17 at 14:47
  • 3
    `scanf_s("%f %c %f",&num1,&oper,&num2);` --> `scanf_s("%f %c %f",&num1,&oper, 1, &num2);` – BLUEPIXY Mar 30 '17 at 14:48
  • thanks @BLUEPIXY that did it! – user7792712 Mar 30 '17 at 14:55
  • 1
    *"i looked around*" but did you [read the man page?](https://msdn.microsoft.com/en-us/library/w40768et.aspx) "Unlike `scanf` and `wscanf`, `scanf_s` and `wscanf_s` require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable." – Weather Vane Mar 30 '17 at 15:11
  • @SouravGhosh The C11 spec 5.1.2.2.1 has "It (`main`) shall be defined with a return type of `int` ... or in some other implementation-defined manner." So for OP, `void main()` is ID and not a central issue to the post. – chux - Reinstate Monica Mar 30 '17 at 15:21

1 Answers1

0

Your problem is that scanf_s requires a buffer size specifier after every %c %s and %[. See this post for a similar problem. Indeed, scanf does not have this issue, what you actually did was put the value of the address of num2 as the buffer size specifier for %c in:

scanf_s("%f %c %f",&num1,&oper,&num2); 

Note that even printf_s has additional requirements

The fix here while still using scanf_s is this:

#include <stdio.h>


int main() {
    float num1, num2;
    int type;
    char oper;
    const int oper_buff_size = 1;

    printf_s("Please enter your choise:\n1. Decimal calculator.\n2. Binary            calculator.\n");
    scanf_s("%d", &type);

    if (type == 1) {
        printf_s("Please enter the equation you want to solve and then press Enter:\n");
        // note oper_buff_size, the buffer size of the char pointer
        scanf_s("%f %c %f", &num1, &oper, oper_buff_size, &num2);
        // test to show this works
        printf_s("Entered: %f %c %f", num1, oper, num2);
    }
    return 0;
}

You may be asking why we need to specify the length of a %c format, considering it should just be the size of a char in bytes. I believe this is because you are required to put the pointer of a character for format, so you don't know if what you pointed to was a char * array or just a pointer to a char (as in this case)

I would also add an addendum, though this is not why your program failed, avoid using cross platform incompatible quirks, such as void main, as it makes it harder for people to find the real problems in your code. Do not use void main(), use int main(...) and return 0; instead. Void main is not valid in standard C or C++, it is a quirk of the Microsoft visual studio implementation of the language.

Community
  • 1
  • 1
Krupip
  • 4,404
  • 2
  • 32
  • 54