0

I'm having an issue in my for loop where val is being printed out as garbage data. I'm wondering if that's because when I do num[(j+1)] it actually increments the variable J, and if so how do I increase the next element without affecting the for loop variable?

while ((scanf("%c",&userIN) == 1) && (userIN != '\n')) {
    if(userIN >= '0' && userIN <= '9') {
        num[i] = (userIN-'0');   
    }
    else if ((userIN == '+')||(userIN == '-')||(userIN == '/')||(userIN == '*')) {
        ops[i+1] = userIN;
    }
  i++;
}


for (j = 1; j <= i; j+2) {
    if (ops[j] == '+') {
        k = j;
        k++;
        num[k] = ((num[j]--) + (num[j]++));
        printf("\n%d",(num[j]--));
        val = num[k];
    }
    else if (ops[i] == '-') {
        k = j;
        k++;
        num[k] = ((num[j]--) - (num[j]++));
        val = num[k];
    }
    else if (ops[j] == '/') {
        k = j;
        k++;
        num[k] = ((num[j]--) / (num[j]++));
        val = num[k];
    }
    else if (ops[j] == '*') {
        k = j;
        k++;
        num[k] = ((num[j]--) * (num[j]++));
        val = num[k];
    }
}
Puck
  • 2,080
  • 4
  • 19
  • 30
  • "when I do num[(j+1)] its actually increment J the variable" Why not post code to increment the variable? Be careful not to cause [undefined behavior](http://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior)! – MikeCAT Mar 08 '16 at 06:53
  • When the input stream runs out, `scanf` returns `EOF`, which is a negative value. In the context of a conditional, this is taken to be true. Better use `while (scanf("%c", &userIN) == 1 && ...)`. Better yet, use `getchar` instead of `scanf("%c", ...)`. – M Oehm Mar 08 '16 at 06:54
  • 2
    Your problems have nothing to do with some magical, invisible increment of the `j` variable. They are all caused by not understanding the algorithm, nor your own code. And from the point when the programmer no longer understand their own code, nothing good can come out of the program. With the last edit, neither the question nor the code makes any sense any longer. – Lundin Mar 08 '16 at 07:19

1 Answers1

1

The loop is quite wrong in general. Actually both loops.

In the second loop you probably expect that the second element read was operator and around there were operands. But the increment part increments just by one so it will then process operator at the second position and take operands at indices 1 and 3. This is wrong because most probably they are uninitialized. To increment j by 2 would somehow solve (or rather kind of work-around) the problem.

In the first loop there is no guarantee that the input will come in order operand-operator-operand-operator-operand-... Even worse, what does i++ in the else block mean? This would completely screw up the things as it will make additional space in both arrays and this will completely confuse the second loop. If you remove this else block completely, the code may work if you provide expected input.

But rather consider some way how to validate input, even with the fixes above there are still quite a few limitations, like for example you can provide only single digit as an operand etc.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43