Reputation: 81
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.
Upvotes: 4
Views: 617
Reputation: 4091
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).
Upvotes: 2
Reputation: 26800
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
Upvotes: 2
Reputation: 508
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]);
}
Upvotes: 1