3

I have a problem where I need to write a menu driven program using function pointers in C. The user picks a choice 1-3, and addition is executed if response = 1, subtract if response = 2, multiplication if response =3.

The first run through is perfect, but once I loop and try to do a second calculation, it searches for the choice AND the first number before it displays what choice you have made.

The math in each function works, passing the variables work, just the printf and scanf statements are out of order after the first run through.

(There are subtraction and multiplication functions, they are exactly the same, except instead of the "+" operator there is the "-" and "*" operator, respectively.)

I searched an issue similar to this and tried fflush and setvbuf commands, these did not work.

void addition(int num1, int num2);
void subtraction(int num1, int num2);
void multiplication(int num1, int num2);

int main(void) {
    void(*m[3])(int, int) = { addition, subtraction, multiplication };

    size_t choice;
    int num1, num2;
    printf_s("Would like to add, subtract, or multiply?\nType 1 for 
addition, 2 for subtraction, 3 for multiplication.\n");
    scanf_s("%d", &choice);
    printf_s("what two numbers would you like to work with?\n");
    scanf_s("%d", &num1);
    scanf_s("%d", &num2);
        if (choice >= 1 && choice <= 3) {
        (*m[choice - 1])(num1, num2);
        while (choice >= 1 && choice <= 3) {

            printf_s("Would like to add, subtract, or multiply?\nType 1 
for addition, 2 for subtraction, 3 for multiplication.\n");
            choice = 0;
            scanf_s("%d\n", &choice);
            printf_s("what two numbers would you like to work with?\n");
            scanf_s("%d", &num1);
            scanf_s("%d", &num2);
            (*m[choice - 1])(num1, num2);
        }
        printf("execution complete");
    }
    return 0;
}

void addition(int num1, int num2) {
    int i = 0;
    i = num1 + num2;
    printf("%d + %d = %d\n", num1, num2, i);
    }

If I input the following: 1 2 3, 2 3 1, i expect the output to be

"1"

"What two numbers would you like to work with?"

"2"

"3" 
"2+3=5" 
"would you like to add, subtract, or multiply?" 
"Type 1 (...) for multiplication" 
"2" 
"what two numbers (...)" 
"3" 
"1" 
"3-1 =2"

However I get this:

"1" 
"What two numbers would you like to work with?"
"2" 
"3" 
"2+3=5" 
"would you like to add, subtract, or multiply?" 
"Type 1 (...) for multiplication"
"2" 
"3" 
"what two numbers (...)" 
"1" 
"3-1 =2"

As you can see, the math is right, but the second time around it required 2 numbers before it asks for the 2 numbers. I don't see how this is possible as there is only one variable being scanned before the following printf statement is executed. Even though the "3" i multiplied was saved in num1, it scanned before the printf, even though the statement is after the printf so this shouldn't be possible. I'm so confused!

6
  • Consider using some linebreaks/newlines in your question (code and prose) to improve readability; especially in expected output.
    – Yunnosch
    Jan 22, 2019 at 17:04
  • 2
    size_t choice; scanf_s("%d", &choice); does not match. Try int choice;. Jan 22, 2019 at 17:09
  • 2
    Remove extra "\n" in one of the scanfs.
    – pmg
    Jan 22, 2019 at 17:14
  • 1
    ... this one scanf_s("%d\n", &choice); Jan 22, 2019 at 17:15
  • 1
    You never check the return value from scanf_s(), so you never know what it reads. Note also that the so-called "safe" functions such as scanf_s() aren't any safer than the so-called "unsafe" functions (they aren't "deprecated" - they're required by the C standard...) and all you're really doing by using them is making your code non-portable and platform-specific. Jan 22, 2019 at 17:15

4 Answers 4

2

why that so long and complicated code ?

just replace

printf_s("Would like to add, subtract, or multiply?\nType 1 for addition, 2 for subtraction, 3 for multiplication.\n");
scanf_s("%d", &choice);
printf_s("what two numbers would you like to work with?\n");
scanf_s("%d", &num1);
scanf_s("%d", &num2);
    if (choice >= 1 && choice <= 3) {
    (*m[choice - 1])(num1, num2);
    while (choice >= 1 && choice <= 3) {

        printf_s("Would like to add, subtract, or multiply?\nType 1 for addition, 2 for subtraction, 3 for multiplication.\n");
        choice = 0;
        scanf_s("%d\n", &choice);
        printf_s("what two numbers would you like to work with?\n");
        scanf_s("%d", &num1);
        scanf_s("%d", &num2);
        (*m[choice - 1])(num1, num2);
    }
    printf("execution complete");
}

by

for (;;) {
  printf("Would like to add, subtract, or multiply?\nType 1 for addition, 2 for subtraction, 3 for multiplication.\n");
  if (scanf("%d", &choice) != 1) {
    puts("abort");
    break;
  }

  if ((choice < 1) || (choice > 3))
    break;

  printf("what two numbers would you like to work with?\n");
  if (scanf("%d %d", &num1, &num2) != 2) {
    puts("abort");
    break;
  }
  (*m[choice - 1])(num1, num2);
}
printf("execution complete");
1
  • @AndrewHenle this is why I just edited my answer ;-)
    – bruno
    Jan 22, 2019 at 17:17
2

I would do it like this :

int main(void) {
    void(*m[3])(int, int) = { addition, subtraction, multiplication };
    size_t choice = 1;
    int num1, num2;
    while (choice >= 1 && choice <= 3) {
        printf_s("Would like to add, subtract, or multiply?\nType 1 for addition, 2 for subtraction, 3 for multiplication.\n");
        choice = 0;
        scanf_s("%d", &choice);
        printf_s("what two numbers would you like to work with?\n");
        scanf_s("%d", &num1);
        scanf_s("%d", &num2);
        (*m[choice - 1])(num1, num2);
    }
    return 0;
}

Your problem was - you had the scanf_s("%d\n", &choice); - you should do it without \n - scanf_s("%d", &choice); and the loop you can do much easier. Remove the if and set choice to 1.

1

Remove '\n'.

scanf_s("%d\n"... will not return until non-white-space is entered after the number.

// scanf_s("%d\n", &choice);
scanf_s("%d", &choice);

Other problems may exist too.

4
  • Thank you, I looked over that line so many times and the extra \n didn't even click. Have a great day. Jan 22, 2019 at 17:22
  • how is it possible to choose that answer who solve almost nothing ? "Other problems may exist too" lol sure
    – bruno
    Jan 22, 2019 at 17:22
  • @bruno Code has numerous short comings. I see about at least 5. If OP wants a general review code review is a good spot for that. Jan 22, 2019 at 17:26
  • 1
    @bruno yes. With chux's answer I realized my mistake and was able to solve it, you simply just wrote your own code and I would have never actually known the issue. Jan 22, 2019 at 17:26
0

Use scanf_s("%d\n",&var); doing so you 'force' the scanf to read the new line (enter) character inserted when you 'commit' your data entry. Otherweise that character will reamin in stream and affect the next scanf and, being that character an enter, will make your code go further.

1
  • Yes I did, fixed. .
    – DDS
    Jan 23, 2019 at 8:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.