0

We've been given a assignment at school and the while loop at the end of the code doesn't seem to give the value I expected.

#include <stdio.h>
#include <string.h>

int main() {
    char str[50],storage[50][50]={0},temp[50]={0};
    printf("Input Formula without space:");
    gets(str);
    int x=0, y=0,z=0,w,i,top=-1,max=strlen(str);
    
    
    for(i=0;i<max;i++){
        printf("Eyyyyyy");
        top+=1;
        switch(str[top]){
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '-':
        case '+':
        case '*':
        case '/':
        case '9':storage[x][y]=str[top];x++;break;
        case ')': 
            if (x-3<0)
                break; 
            else{
                while(storage[x-3][y]!=0){
                    temp[y]=storage[x-3][y];
                    y++;
                    printf("Hey");}; 
                storage[x-3][y]='(';
                while (temp[y]!=0){
                    storage[x-3][y+1]=temp[y];
                    y++;
                };
                for (z=2;z>=0;z--){
                    while (storage[x-z][y]!=0){
                        w=strlen(storage[x-3]);
                        storage[x-3][w]=storage[x-z][y];
                    }
                }
            }
            x-=3;
            break;
            
        }
    }
    for(i=0;i<20;i++){
        printf("%c",storage[0][i]);
    }
    for(i=0;i<20:i++){
        printf("%c",storage[0][i]);
    }
    while(storage[x][y]!=0){
        printf("%c",storage[0][y]);
        y++;
    }
    
}

I'd like to ask for some help on how do I make the while loop work. It doesn't give me any errors when compiling its just the value on while loop doesn't show up. only the other printfs before that.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • 3
    Now seems like a great time to learn how to use a *debugger* to step through your code statement by statement while monitoring variables and their values. Note that it will be easier if you have a consistent indentation, and don't put multiple statements on a single line. – Some programmer dude Dec 10 '20 at 23:56
  • 1
    You may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel Dec 11 '20 at 00:03
  • 1
    regarding the statement: `for(i=0;i<20:i++){` This does not compile! The parameters MUST be separated via a semicolon `;`, not a colon `:` – user3629249 Dec 12 '20 at 00:54
  • 1
    the function: `strlen()` return a `size_t` not an `int` – user3629249 Dec 12 '20 at 00:55
  • 1
    regarding: `gets(str);` The function: `gets()` has been depreciated for decades and completely removed from the C language back in 2009. Strongly suggest using `fgets()` (read the [MAN page](https://linux.die.net/man/3/fgets) as the parameter list (and some other details) are different) – user3629249 Dec 12 '20 at 00:58
  • regarding: `for(i=0;i<20;i++){ printf("%c",storage[0][i]); } for(i=0;i<20:i++){ printf("%c",storage[0][i]); }` Why print the same data twice? – user3629249 Dec 12 '20 at 01:18
  • 1
    regarding statements like: `for(i=0;i<20;i++){` What is the importance/meaning of the 'magic' number `20`? Especially since the actual (possible) length of the input is 48. – user3629249 Dec 12 '20 at 01:25

1 Answers1

0

regarding the while() statement at the end of the posted code:

while(storage[x][y]!=0){`

what are the initial values of x and y?

Since those values are already 'maxed out' the selection of storage[x][y] is past the end of the array `storage'

Suggest properly initializing those index values before entering the while() loop

user3629249
  • 16,402
  • 1
  • 16
  • 17