Thread: Code for two entries, skips second entry request

  1. #1
    zach
    Guest

    Code for two entries, skips second entry request

    Re code below:
    Question 1. After inputting data.name (say John Smith) and terminating with Enter, the data.actionCode cannot be entered. Sm is entereded instead. How can I change that?
    Question 2. In the line "fwrite .." I don't understand the period in "...out),1,fptr..." . How much data has to go in, is already determined by the sizeof(... I have looked at differeten tutorials, still I don't understand.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
        struct about
        {
            char name[35];
            char actionCode[2];
        };
        
    int main()
    {
        struct about data;
        
        printf("Concerns    : ");
        scanf("%s",data.name);    
            
        printf("Action Code : ");
        scanf("%s",data.actionCode);
        
        FILE *fptr;
        fptr=fopen("K:\\descrip", "a+");
        
        fwrite(&data,sizeof(struct about),1,fptr);
        fclose(fptr);    
        return 0;    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    %s uses space as a separator, so to read "John Smith", you should ideally use fgets(), like
    Code:
    fgets( data.name, sizeof(data.name), stdin );
    But you may want to remove the \n at the end of the line (see the FAQ for details).


    Your fwrite() is fine.
    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
    zach
    Guest
    Thank you. I need to do more homework.

  4. #4
    zach
    Guest

    Getting rid of trailings

    Quote Originally Posted by zach View Post
    Thank you. I need to do more homework.
    After coding:

    [code]
    data.name = data.name.TrimEnd('\r', '\n');
    data.actionCode = data.ActionCode.TrimEnd('\r', '\n');
    [\code]

    I get:

    [Error] request for member 'TrimEnd' in something not a structure or union.

    Question:

    Does TrimEnd not like structures?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    More like C# != C
    String.TrimEnd Method (System) | Microsoft Docs

    Right idea, but wrong language.

    I usually do
    Code:
    char *p;
    if ( (p=strchr(buff,'\n')) != NULL ) *p = '\0';
    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.

  6. #6
    zach
    Guest
    Quote Originally Posted by Salem View Post
    More like C# != C
    String.TrimEnd Method (System) | Microsoft Docs

    Right idea, but wrong language.

    I usually do
    Code:
    char *p;
    if ( (p=strchr(buff,'\n')) != NULL ) *p = '\0';
    Is p the last character of the entered line of char?

  7. #7
    zach
    Guest
    Quote Originally Posted by zach View Post
    Is p the last character of the entered line of char?
    Thank you for this solution. Excellent / nice and simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. XLib Event Loop - My code entry point??
    By jaws2935 in forum Linux Programming
    Replies: 7
    Last Post: 10-17-2012, 09:23 AM
  2. Replies: 4
    Last Post: 05-16-2010, 02:05 PM
  3. scanf skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  4. Code Check Requested! (Alphabetize entries?)
    By theonetruehero in forum C Programming
    Replies: 6
    Last Post: 04-15-2008, 04:43 PM
  5. program skips over code
    By willc0de4food in forum C Programming
    Replies: 9
    Last Post: 11-16-2006, 06:38 PM

Tags for this Thread