Home > Mobile >  My code printing blank space instead of the input I have given
My code printing blank space instead of the input I have given

Time:12-31

I have the code below and it is supposed to print the names I have given but instead it prints blank spaces.

I might've made a mistake with data type's because I was printing integers but not names. I couldn't figure it out.

Here's the full code but I think the main problem is at printStack function.

Edit: I've changed everything to english sorry for the confusion.

#include <stdio.h>
#include <stdlib.h>
#define SIZE 25

char stack[SIZE];
int top=-1;

int isFull(){
    if (top>=SIZE-1){
        return 1;
    }
        return 0;    
}

struct n{
    n * next;
    char data;
};

typedef n node;

void printStack(char person){
    printf("Entered persons: \n");
    for(int i=0;i<=top;i  )
        printf("%c\n",stack[person]);
    printf("\n");
}

void push(node * root, char person){
    if(root == NULL || isFull()){
        root = (node *)malloc(sizeof(node));
        root -> data = person;
        root -> next = NULL;
        stack[  top]=person;
    }
    node * iter = root;
    while(iter->next !=NULL || isFull()){
        iter = iter -> next;
        stack[  top]=person;
    }
}


int main()
{
    int pick;
    char person;
    node * s=NULL;
    while (1){
       
       printf("1 - Add person\n");
       printf("2 - Show persons\n");
       printf("3 - Exit\n");
       
       scanf("%d",&pick);
       switch (pick){
            case 1: 
                printf ("Person name:  ");
                scanf("%s",&person);
                push(s, person);
                break;
            case 2:
                printStack(person);
                break;
            case 3:
                exit(0);
                break;
       }
                    
   }
    return 0;
}

CodePudding user response:

printStack() iterates over stack but in the printf() call you print stack[yolcu] so the same thing top 1 times. I think you mean to print stack[i]. There is a logic error between 1 and later elements (first element top is 1 after 2nd element top is 3). I suggest you use the normal c range [0; top[ where top is now size instead of a (possible invalid) index so initialize is to 0 instead of -1. Iterate i<top. Full if top >= BOYUT. You want to use scanf(" %c", ..) to ignore the newline from previous prompt:

#include <stdio.h>
#include <stdlib.h>
#define BOYUT 25

char stack[BOYUT];
int top=0;

int isFull(){
    return top >= BOYUT;
}

typedef struct node {
    struct node *next;
    char data;
} node;

void printStack(){
    printf("Girilen yolcular: \n");
    for(int i=0;i<top;i  )
        printf("%i: %c\n",i, stack[i]);
    printf("\n");
}

void push(node * root, char yolcu){
    if(root == NULL || isFull()){
        root = malloc(sizeof(node));
        root -> data = yolcu;
        root -> next = NULL;
        stack[top  ]=yolcu;
    }
    node * iter = root;
    while(iter->next !=NULL || isFull()){
        iter = iter -> next;
        stack[top  ]=yolcu;
    }
}

int main() {
    int secim;
    char yolcu;
    node *s=NULL;
    while (1){
        printf("1 - Yolcu Ekleme\n");
        printf("2 - Yolculari Goster\n");
        printf("3 - Cikis\n");
        scanf("%d",&secim);
        switch (secim){
            case 1:
                printf ("Yolcu Adi: ");
                scanf(" %c",&yolcu);
                push(s, yolcu);
                printf("\n");
                break;
            case 2:
                printStack();
                break;
            case 3:
                return 0;
                break;
        }

    }
}

and example session:

1 - Yolcu Ekleme
2 - Yolculari Goster
3 - Cikis
1
Yolcu Adi: a

1 - Yolcu Ekleme
2 - Yolculari Goster
3 - Cikis
1
Yolcu Adi: b

1 - Yolcu Ekleme
2 - Yolculari Goster
3 - Cikis
2
Girilen yolcular: 
0: a
1: b

1 - Yolcu Ekleme
2 - Yolculari Goster
3 - Cikis
  •  Tags:  
  • c
  • Related