0

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

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
LeCoding
  • 1
  • 1
  • 3
    Please [edit] your question to indent the code to make it possible to read and follow along. – Some programmer dude May 22 '23 at 18:08
  • As for your problem, this seems to be the perfect time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. For example by using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through the code line by line while monitoring variables and their values. – Some programmer dude May 22 '23 at 18:09
  • As a very probably hint: The array `check`, how big string can it hold? Remember that strings in C are really called ***null-terminated** strings*. A string of three characters needs space for *four* to be able to hold the terminator. – Some programmer dude May 22 '23 at 18:11
  • 1
    A C string is always zero terminated, so `check` is too short to hold "yes". Destroying other data is a common result. – BoP May 22 '23 at 18:11
  • To be more certain, restrict the length input with `char check[3]; scanf("%2s", check);` One lesson to take, is not to use such tiny input buffers. Even if you have `char check[4];` if someone enters "yess" by mistake it's much harder to clean up and re-enter than if you use a generous buffer. – Weather Vane May 22 '23 at 18:14
  • 1
    ... anyway, the prompt does not ask the user to enter "yes". It asks for "y/n". Another edgey case is that you ask for 1 or 2 to confirm or refuse, and then make the unsafe test for 2, (anything not 2 taken as OK). You should try to be as exclusive as possible, checking specifically for the OK reply. – Weather Vane May 22 '23 at 18:18
  • also be careful with `strcat`, it does _not_ add the two buffer sizes together, it simply appends the source string to the destination and adds a NUL. This means that your first name + space + last night must all fit in `sizeof(name1)-1`, which is 29. – yano May 22 '23 at 18:34

2 Answers2

0
 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();
}
Harith
  • 4,663
  • 1
  • 5
  • 20
0

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335