Thread: Need help, code stops

  1. #1
    Registered User
    Join Date
    Dec 2023
    Posts
    13

    why backspace in scanf function ?

    Hi need your help again ...

    Code:
    #include <stdio.h>
    #include <string.h>
     
    typedef struct
    {
        char phone[30];
        char name[30];
    }
    Phonebook;
    Phonebook people[2];
    
    int showfile(void);
    
    int main()
    {
    
        //open file & user input
        FILE *file = fopen("phonebook.txt", "w");
        
        for(int i = 0; i < 2; i++)
        {
            printf("Name [%d] ?: ", i+1);
            scanf(" %[^\n]", people[i].name);
            
            printf("Phone [%d] ?: ", i+1);
            scanf("%s", people[i].phone);
    
            fprintf(file, "Name: %s\t Phone: %s\n", people[i].name, people[i].phone);
        }
        fclose(file);
    
        //ask user to read file
        char decision;
        printf("\nShow File ? (y/n): ");
        scanf("%c", &decision);
    
        if(decision == 'y')
        showfile();
            else if(decision == 'n')
            printf("Quit\n");
    
    return 0;
    }
    
    //// FUNCTIONS ////
    ///////////////////
    
    int showfile(void)
    {
        FILE *fh_output = fopen("phonebook.txt", "r");
    
        if(fh_output != NULL)
        {
            char c;
            while((c = fgetc(fh_output)) != EOF)
            putchar(c);
        }
        fclose(fh_output);
        return 0;
    }
    The problem was that the code always stopped after
    //ask user to read file
    printf("\nShow File ? (y/n): ");
    scanf("%c", &decision);

    It didnt let me type in anything if I didnt have a backspace in front of "%c" in the scanf function but if I remove the code above I dont need a backspace, why is that the case ? Whats the deal with the backspace in front of it ? Someone explained it to me once but I still dont get it.

    Thanks in advance.
    Last edited by crusher152; 02-04-2024 at 05:47 AM. Reason: chanegd question

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First of all, it's not backspace
    " " is space (cursor moves right(*))
    "\b" is backspace (cursor moves left(*))

    Try this
    Code:
        if(decision == 'y')
            showfile();
        else if(decision == 'n')
            printf("Quit\n");
        else
            printf("Unknown char with decimal value %d\n", decision);
    Without the space, chances are you're ending up with the newline (aka \n aka decimal 10) in decision.

    The space before the %c ensures that all white space (spaces, newlines) is skipped before putting the next visible character (which I guess in your case is y or n) into decision.

    (*) assuming text is being displayed left-to-right to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2023
    Posts
    13
    Ups not sure why I wrote "backspace", thanks alot for the explanation and your time.

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    140
    You are scanning the phone field with %s. Remember that %s stops on white space, so if someone types a complex phone string this will break. You probably should use the same pattern you used for the name, which reads until end of line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with my code, my program stops
    By aqoonyahan in forum C Programming
    Replies: 2
    Last Post: 02-23-2016, 01:06 AM
  2. Replies: 8
    Last Post: 03-29-2010, 04:38 AM
  3. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  4. Stops before the end of the code.
    By Elivmar in forum C++ Programming
    Replies: 14
    Last Post: 10-08-2005, 01:56 PM
  5. This never stops for some reason
    By Shadow12345 in forum C++ Programming
    Replies: 14
    Last Post: 09-02-2002, 10:41 AM

Tags for this Thread