I am writing a calculator in C, but it will take input like 1+2*9/5. I am not an expert in C, so I thought I can ask the user how many numbers he has (for example let's say 4), then asking 3 times "enter the number, enter operator" and 1-time "enter number". Then putting all inputs to an empty array respectively then doing the operations by looping through the array. But of course, I've got problems.
#include <stdio.h>
int main(void) {
int how_many;
printf("How many numbers do you have to calculate?");
scanf("%d", &how_many);
int number_entry = how_many; // for example 1+2*9/5 has 4 numbers
int operator_entry = how_many - 1; // and 3 operators
int number_in;
char operator_in;
int all_inputs[(number_entry + operator_entry)];
for (int j = 0; j < operator_entry; j++) {
printf("Enter the number : ");
scanf("%d", &number_in);
all_inputs[j] = number_in;
printf("\n");
printf("Enter the operation as + for add, - for substract, * for "
"multiply and / for division");
scanf(" %c", &operator_in);
all_inputs[j + 1] = operator_in;
}
printf("Enter the number : ");
scanf("%d", &number_in);
all_inputs[operator_entry + 1] = number_in;
for (int k = 0; k < (2 * how_many) - 1; k++)
printf("%s\n", all_inputs[k]);
return 0;
}
As you can see from the code, looping through the code to append inputs into the array is not working because I am doing
j=0
append a number to all_inputs[0]
append operator to all_inputs[0+1]
j=1
Now, the operator will be replaced with a number.
And another problem is, it shows "enter a number" just once and then loops the "enter the operator".
If you know any other way to do this, please let me know. Again, I am not a pro in C.