Jaakkath
Jaakkath

Reputation: 65

C not printing whole string

My problem is that it's only printing out 1 letter, when it's supposed to print the whole string. I would really appreciate your help! Thank you once again for helping me.

#include < stdio.h >

int main(void) {

    int val;

    val=1;

    while (val!=0) {

        printf("\n");

        printf("Choose\n");

        printf("1) Add new letter\n");

        printf("2) Empty string\n");

        printf("3) Print string\n");

        printf("0) Stop\n");

        printf("Choice: ");

        scanf("%d",&val);

        printf("\n");

        char sign[200];

        char sign2[200];

        int size;

        size = strlen(sign2);

        switch (val) {

            case 1:

                printf("Give some letter: ");

                scanf("%s",&sign2);

                strcpy(sign, "");

                strcat(sign,sign2);

                sign2[size]=sign2;

                sign2[size+1]='\0';

                break;

            case 2:

                if (val==2) {

                    printf("Emptied string.\n");

                    sign2[0] = '\0';

                }
                break;

            case 3:

                if (size == 0) {

                    printf("Is empty\n");


                } else {

                    printf("String: %s\n",sign);

                }

                break;

            case 0:

                printf("Stopping.\n");

                break;

            default:

                printf("Unknown.\n");

                break;

        }

    }

        return 0;
}

My problem is that it's only printing out 1 letter, when it's supposed to print the whole string. I would really appreciate your help! Thank you once again for helping me.

Upvotes: 0

Views: 527

Answers (1)

user2736738
user2736738

Reputation: 30906

Lots of problem - but yes we can discuss one by one

  • strlen returns the length of the nul terminated char array. The content of the array in your case is indeterminate at the time you are applying strlen on it. This is undefined behavior. You may wanted to initialize the char array char sign2[200]={0} so that you can use strlen on it. (By the way - if you want to get the size of the char array use sizeof(sign2) )

  • scanf("%s",&sign2); will be scanf("%s",sign2); because char array converted ("decays") into pointer to the first element - which is a pointer to the sign2[0]. That is what you are supposed to pass to scanf. Your compiler complained about it.

  • sign2[size]=sign2; is not doing what you are thinking it is doing - this is trying to assign a char* to a char variable which is wrong.

  • Also every time you are overwriting what is there in sign2. Ideally you want to get a char input and that will be scanf(" %c",&somecharvar);.

  • After you get the input in somecharvar then adding it would be easy. You can simply get the length of the string and then add the character and also the nul terminating char. In case you are thinking - what will be the case the very first time then you will have do one thing - initialized sign2 with \0 initially. That way even if you call strlen over it the very first time - it will give you the result of 0.

    char somevar;
    if( scanf(" %c",&somevar)!= 1){
        fprintf(stderr,"Error in input\n");
        exit(1);
    }
    size_t sz = strlen(sign2);
    if(sz+1 < sizeof(sign2)){
        sign2[sz]=somevar;
        sign2[sz+1]=0;
    } else {
        fprintf(stderr,"Noe enough space\n");
        exit(1);
    }
    
  • As you have already seen, strlen returns the length of the string which of type size_t. size_t is guaranteed to hold the size of any object in C.

  • For scanf always check it's return value, in case it fails you will know it and terminate the program or handle it accordingly( most plausible being - eating out the wrong inputs from stdin that would still be there because scanf couldn't parse it).

  • This is a good practice you can say - instead of using hard-coded value (magic numbers) like 200 or similar use a macro or constant to incorporate them.

Upvotes: 3

Related Questions