0

For example, if I want to call switch statement in for loop, I got an error.How can I correct this issue?If I want to move my switch statement outer part in determined condition which is to be using more than calling switch statement. Thanks in advance.

    #include<stdio.h>
    #define     _CRT_SECURE_NO_WARNINGS

    #pragma warning(disable : 4996)


    #define     SIZE        50

    int i = 0;

    int main() {

    char usertxt[SIZE], myoperator[SIZE];
    printf("addition='+',subtraction='-',multiplication='*',division='/'\n");
    usertxt[0] = 0;
    int x, myarray[SIZE];
    printf("How many numbers should be entered? ");
    scanf_s("%d", &x);


    for (i = 0; i < x; i++) {
        scanf_s(" %c", &myoperator[i], 1);
            switch (myoperator[i]) {
            case '+':printf("Addition operation\n");
                printf("  Enter your number: ");
                scanf_s("%d", &myarray[i]);
                usertxt[i] = printf("%d%c", myarray[i], myoperator[i]);
                break;
            case '-':printf("Subtraction operation\n");
                printf("Enter your numbers: ");
                scanf_s("%d", &myarray[i]);
                usertxt[i] = printf("%d%c", myarray[i], myoperator[i]);
                break;
            case '*':printf("Multiplication operation\n");
                printf("Enter your numbers: ");
                scanf_s("%d", &myarray[i]);
                usertxt[i] = printf("%d%c", myarray[i], myoperator[i]);
                break;
            case '/':printf("Division operation\n");
                printf("Enter your numbers: ");
                scanf_s("%d", &myarray[i]);
                usertxt[i] = printf("%d%c", myarray[i], myoperator[i]);
                break;
            default :if (myoperator[i] == '\0') {
                break;
            };
        }

    }
}
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
Johnny
  • 19
  • 6

1 Answers1

0

As @Gerhardh said, welcome to SO. But I really think that the logic behind your code isn't good enough. I take some time to understand your goal and I make a new clearer version.

While testing and understanding your code, I've encountered the following problems:

  • Implicit declaration of function 'scanf_s' is invalid in C99
  • Declarations of global variables as counters and their usage inside the loop were the main reason why your code doesn't work as expected.
  • The printf() function is used for printing the output. It returns the number of characters that are printed. If there is some error then it returns a negative value. You used it like it returns a char or even a string.
  • Switch statement is useless if you have to do always the same operations.
  • Char reading doesn't work correctly.

I'm gonna attach here the updated code. Hope that it could be helpful and that actually it has the same "goal" that your code has.

#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS

#pragma warning(disable : 4996)
#define SIZE 500
#define nOperator 4

int main() {
    
    char usertxt[SIZE], myoperator[nOperator];
    printf("addition='+',subtraction='-',multiplication='*',division='/'\n");
    usertxt[0] = 0;
    int nOperations;
    int x, myarray[SIZE];
    
    printf("How many numbers should be entered? ");
    scanf("%d", &x);
    printf("How many operations would you like to try? ");
    scanf("%d", &nOperations);
    
    for (int i = 0; i < nOperations; i++) {
        fseek(stdin, 0, SEEK_END);
        printf("\n\nEnter operation #%d: ", i + 1);
        myoperator[i] = getchar();
        for (int j = 0; j < x; j++) {
            printf("Enter number #%d: ", j + 1);
            scanf("%d", &myarray[j]);
        }
        
        int k = 0;
        for (int m = 0; m < x; m++) {
            if (m == x - 1) {
                usertxt[k] = myarray[m];
                printf("%d", myarray[m]);
            } else {
                usertxt[k] = myarray[m];
                usertxt[k + 1] = myoperator[i];
                printf("%d%c", myarray[m], myoperator[i]);
            }
            k++;
        }
        printf("\n\n");
    }
}

As you can see, I tried to stay as much as next to your code idea and implementation. For instance, I've not deleted the initial pragma and define. Probably you are going to use them later. I've used fseek(...) as suggested here: How to clear input buffer in C? in order to read correctly char and integers together.

First thing first, it reads the numbers that you want to use and the number of operations that you want to do. After that, it takes the numbers associated with a certain operation, and at the end, it prints the expression as you are doing in your own code. If you want to add the result, you just need a little edit in the code to sum all the numbers in the array or counting them while reading from input.

Variable Counters Description:

  • i is for 'myoperator' array
  • k is for 'usertxt' array
  • m is for 'myarray' array

Next steps:

  • Add the result variable to display the result of the operation
  • Use dynamic memory

Note that: fseek(...) works on some systems; if not, then it is not surprising as nothing guarantees that it will work when standard input is an interactive device (or a non-seekable device like a pipe or a socket or a FIFO, to name but a few other ways in which it can fail).

If you need that it has to be portable, then check the link that I've placed before. Hope that it was helpful.

Cheers, Denny

D. Caruso
  • 163
  • 1
  • 3
  • 11