4

Here is the code of my simple calculator:

#include <stdio.h>

int main(void)
{
    int n1, n2;
    char op;

    do {
        printf("Enter which operation you want to do(+, -, *, /) \n"); op = getch();
    } while(op!='+' && op!='-' && op!='*' && op!='/');

    printf("\n");

    switch(op) {
        case '+':
            printf("You chose to do addition.\n\n");
            printf("Number 1: "); scanf("%i", &n1);
            printf("Number 2: "); scanf("%i", &n2); printf("\n");
            printf("%i + %i = %i\n", n1, n2, n1+n2);
            break;
        case '-':
            printf("You chose to do subtraction.\n\n");
            printf("Number 1: "); scanf("%i", &n1);
            printf("Number 2: "); scanf("%i", &n2); printf("\n");
            printf("%i - %i = %i\n", n1, n2, n1-n2);
            break;
        case '*':
            printf("You chose to do multiplication.\n\n");
            printf("Number 1: "); scanf("%i", &n1);
            printf("Number 2: "); scanf("%i", &n2); printf("\n");
            printf("%i * %i = %i\n", n1, n2, n1*n2);
            break;
        case '/':
            printf("You chose to do division.\n\n");
            float dn1, dn2;
            printf("Number 1: "); scanf("%f", &dn1);
            printf("Number 2: "); scanf("%f", &dn2); printf("\n");
            printf("%f / %f = %f\n", dn1, dn2, dn1/dn2);
            break;
    }
}

As you can see, the program takes the input from the user and makes some calculations accordingly to that. It works nice and as I expected but, I'm taking only two numbers from the user. I want to make the user be able to enter a number as many times as he/she wants.

I thought something like this and it seemed stupid to me.

for(int i=1; i<10; i++) {
    printf("Enter number %i: ", i); scanf("%i", &{i}n);
}

I tried to use the for loop initialization variable to create new variables as much as the user wants.

I have a little bit experience in Javascript and Python, and I remember something like I could use the initialization variable as a placeholder.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
wheldrake
  • 81
  • 7
  • 5
    Perhaps you should use *arrays*? – Some programmer dude Nov 05 '18 at 08:35
  • 1
    Also, *always* check what [`scanf`](https://en.cppreference.com/w/c/io/fscanf) *returns*. That will allow you to handle errors as well as being able to know when the user has pressed the end-of-file key sequence to end the input (if the user wants less than ten inputs). – Some programmer dude Nov 05 '18 at 08:35
  • 1
    Note that [`scanf()` leaves the newline in the input buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer). You'll start running into that as a problem sooner or later, I think, because you use `getchar()` to get the operator character. – Jonathan Leffler Nov 05 '18 at 09:02
  • Hi there - perhaps you meant to use function pointers? That could be a solution allowing you to pass the operator..... A possible case that I found; https://www.geeksforgeeks.org/function-pointer-in-c/ – Anders Nov 05 '18 at 14:12

3 Answers3

2

You can use a variable length array (available since C99) and do this:

First define the array:

int Arr[N];

Then read into it in every case statement:

printf("How many numbers you want to add?");
scanf("%i", &N);

for(int i=0; i<N; i++) { //index goes from 0 to N-1
    printf("Enter number %i: ", i); scanf("%i", &Arr[i]);
    printf("\n");
}

Do not forget to check the return value of scanf to make sure that you were able to read the data correctly.

Since you know how many elements of the array you have read into (based on the value of N), you can perform the arithmetic operation accordingly.

Also note that getch is a non-standard function, use the standard getchar instead

P.W
  • 26,289
  • 6
  • 39
  • 76
2

A common solution is to define a special op character for the user to exit (a common choice is q, which stands for "quit"), and wrap the whole code with an infinite loop (while(1)). We'll break out of that loop when the user enters q. This way the user has full control how many times to repeat calculations (it's not limited to 10 times or any other number...).

Here's an example of how your code might look:

// ...
while (1)
{
    // notice the slight changes:
    do {
        printf("Enter which operation you want to do(+, -, *, /). Enter 'q' to quit.\n");
        op = getch();
    } while(op!='+' && op!='-' && op!='*' && op!='/' && op != 'q');

    if(op == 'q')
    {
        printf("Exiting...\n");
        break;
    }

    // here comes the original code
}

EDIT: I just realized that I'm not sure if this answers what was asked in the question. If the meaning was to allow adding (or subtracting etc.) more than 2 numbers than it does not solve the problem (and there are many answers here that provide a good solution for this issue).

noamgot
  • 3,962
  • 4
  • 24
  • 44
  • My question was if I can allow the user to enter as many numbers as he/she wants. Your answer is an improvement but not the solution. Thank you for your contribution. – wheldrake Nov 05 '18 at 10:17
1

As @Some programmer dude mentioned, you should use arrays to do this. You can use either static or dynamic arrays. Static approach is like that:

int arr[ARRAY_LENGTH];
for (int i = 0, i < ARRAY_LENGHT; i++) {
    scanf("%i", &arr[i]);
}

But if you want anonymous number of input, solution is dynamic method.

int array_length;
int *arr;

printf("Enter the array lenght: ");
scanf("%d", &array_length);

arr = (int *)malloc(array_length * sizeof(int));

for (int i = 0; i < array_length; i++) {
    scanf("%i", &arr[i]);
}
kurtfu
  • 498
  • 2
  • 4
  • 15