My string variable lost its memory after a block of code

46 Views Asked by At

I'm trying to make a simple memory for username and phone number so the user could refer back to it at the end of the big chunk of code, but the string stored in name1 seems to be lost when printing it out again:

// Online C compiler to run C program online
#include <stdio.h>
#include <string.h>
//Intialisation 
int main() {
    // Write C code here
char name1[30], name2[30], phonenumber[30], final
    int userdetailchecker = 2;
    int finalconfirmation = 0;
    printf("\n\n******GMI KOPERASI ORDER AND SELF-PICK-UP SERVICE******\n");
    printf("|You have to register your details to access this service|\n");
    while(userdetailchecker != 1){
    printf("Please enter your first name: ");
    scanf("%s", name1);
     printf("Please enter your last Name: ");
     scanf("%s", name2);
    strcat(name1," ");
strcat(name1, name2);
   

    printf("Is your name %s?\n1.Yes\n2.No\n", name1);
    scanf("%d", &userdetailchecker);
    }
    printf("Great! Your orders would be associated with the username %s", name1);
    printf("\nPlease enter your phone number: ");
    scanf("%s", phonenumber);
     while(strlen(phonenumber) < 9 || strlen(phonenumber) > 13){
     printf("Invalid phone number(Must be 9 to 13 digits only)");
     printf("\nPlease enter your phone number: ");
     scanf("%s", phonenumber);
     }
     printf("You have successfully registered your phone number! (%s)", phonenumber);
     printf("\n\nHello user %s!\n", name1);
     printf("Continue to end section? y/n?\n");
     char check[3];
     scanf("%s", check);
     if(strcmp(check, "yes") == 0)
    {
     printf("\nPlease confirm your user details");
    printf("\nUsername = %s", name1);
    printf("\nPhone number = %s", phonenumber);
    printf("\n1.Yes\n2.No\n");
    while(1){
    scanf("%d", &finalconfirmation);
    if(finalconfirmation == 2)
    {
    printf("Please log out of this session.");
    break;
    }
    }
    }
    else{
        return 0;
    }
    return 0;
}

I've been at it for a while now; to no avail. Would appreciate help and tips

2

There are 2 best solutions below

0
Harith On
 char check[3];
 scanf("%s", check);
 if(strcmp(check, "yes") == 0)

check is not big enough to store yes. Recall that a string in C is simply an array of characters terminated by a nul-byte. We need a bigger array and stricter error checking:

char check[4];
if (scanf("%3s", check) != 1) {
    fputs("Invalid input.\n", stderr);
    complain();
}
0
Vlad from Moscow On

There are several problems with your code.

For example in calls of scanf like that

scanf("%s", name1);

the user can enter more characters than the array name1 can store.

You should write at least like

scanf("%29s", name1);

Another problem is that the array name1 is not large enough to store also a space and the string stored in the array name2 that are appended to the array in these statements

strcat(name1," ");
strcat(name1, name2);

If to keep the size of the array name2 as is then the array name1 should be declared at least like

char name1[60], name2[30], phonenumber[30], final;
     ^^^^^^^^^

And you should write for example

scanf("%29s", name1);
printf("Please enter your last Name: ");
scanf("%29s", name2);
strcat(name1," ");
strcat(name1, name2);

Pay attention to that the variable final is not used. The compiler in such a case usually issues a warning.

The array check is not large enough to store the string "yes"

 char check[3];
 scanf("%s", check);
 if(strcmp(check, "yes") == 0)

You need to declare it like

 char check[4];

and write

 scanf("%3s", check);
 if(strcmp(check, "yes") == 0)

Also this prompt

printf("\n1.Yes\n2.No\n");

confuses users. They can think that they again must to enter a string while actually the program expects that users will enter an integer.

while(1){
scanf("%d", &finalconfirmation);
if(finalconfirmation == 2)

And you should format the text of the program. Otherwise it is unreadable.