0

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".

ss for problem

If you know any other way to do this, please let me know. Again, I am not a pro in C.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
apotamkin
  • 5
  • 1
  • 5
  • **Some side tips:** `main()` must return `int` & `printf(all_inputs[k])` – here you need to provide `%s` format specifier before inserting that parameter. – Rohan Bari Jan 21 '21 at 18:39
  • `scanf("%c", &operator_in);` ==> `scanf(" %c", &operator_in);` with added space, hundreds of questions here about that, for example [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Jan 21 '21 at 18:40
  • What should I use instead of main? Fixed the %s, thank you., @RohanBari – apotamkin Jan 21 '21 at 18:43
  • 1
    thank you @WeatherVane – apotamkin Jan 21 '21 at 18:44
  • You should probably read the whole line (`char line[4096]; if (fgets(line, sizeof(line), stdin) == NULL) …report error; do not go past go; do not collect $200…` and then parse the line. That's verging on getting into compiler theory, though. The `scanf("%c", …)` reads the newline left after the previous number; use a space too: `scanf(" %c", …)`. – Jonathan Leffler Jan 21 '21 at 18:45
  • @apotamkin use `int main(void)` instead, and `return 0` at the end of the program. – Rohan Bari Jan 21 '21 at 18:46
  • 1
    thank you so much @RohanBari – apotamkin Jan 21 '21 at 18:47
  • @JonathanLeffler I fixed the repeating problem by adding space before %c, thank you. I am having a problem while appending inputs to array lol – apotamkin Jan 21 '21 at 18:49
  • 1
    @RohanBari thanks for the clean code, dev c plus does not allow inital declaretion inside for loop btw.thanks again :) – apotamkin Jan 21 '21 at 18:58

0 Answers0