Thread: Space between lines (common)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Space between lines (common)

    Hi, I've searched the internet and seen the answer to this, but none of these answers have worked for me. So I guess it is something wrong with my code.

    The user are supposed to enter like host, name, date and so on. But when the host and name are entered it jumps to enter the time directly.

    Code:
    #include <stdio.h>
    
    
    int main() {
    	
    	char host[20];
    	printf("Who is this invitation from: "); 
    	scanf("%s", host);
    	
    	char name[20];
    	printf("Who is this invitation to: "); 
    	scanf("%s", name);
    	
    	char date[20]; 
    	printf("Date: "); 
        scanf("%[^\t\n]s", &date);
    	
    	short time, duration;
    	printf("\nTime: "); 
    	scanf("%hd", &time); 
    	
    	printf("\nDuration (h): ");
    	scanf("%hd", &duration); 
    	
    	char destination[20]; 
    	printf("Where: "); 
    	scanf("%s", destination); 
    	
    	printf("\n\nHello %s", name); 
    	printf("\n\nYou are invited to a party the %s at %hd in %s.", date, time, destination); 
    	
    	short lastsUntil = time + duration;
    
    
    	printf("\nThe party lasts until %hd", lastsUntil);
    	
    	printf("\n\nSee you there!"); 
    	printf("%s", host); 
    	
    	return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    When you enter the names, are there spaces in that data? %s reads a whole word and breaks on a space, so you may have to rewrite some stuff to handle spaces correctly, like host first name and last name, and guest first name and last name, then date.

    When you enter the date, I think your format is more complicated than you want it to be.
    scanf("%[^\t\n]s", &date);
    I suspect you simply want to read until some white space occurs. But %s already does this. Your format will read input until a tab or newline and then store it in date. Then scanf() will expect an s to be input.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    No, what I want is to read the variable date as a string with spaces example of input:

    Date: 12th of April

    And I need this whole string to be printed out in the message
    "You are invited to a party the %s at ..."
    "You are invited to a party the 12th of April at ..."

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
        char date[20]; 
        printf("Date: "); 
        scanf("%[^\t\n]s", &date);
    coding error,
    Code:
    gcc -Wall -o "Space_between_lines" "Space_between_lines.c" (in directory: /home/userx/bin)
    Space_between_lines.c: In function 'main':
    Space_between_lines.c:16:11: warning: format '%[^
    ' expects argument of type 'char *', but argument 2 has type 'char (*)[20]' [-Wformat=]
         scanf("%[^\t\n]s", &date);
               ^
    Compilation finished successfully.
    what is that format trying to get?

    EDIT


    Code:
       char date[20]; 
        printf("Date: "); 
        scanf("%s", date);
    Code:
    userx@slackwhere:~/bin
    $ ./Space_between_lines
    Who is this invitation from: me
    Who is this invitation to: you
    Date: 12/23/44
    
    Time: 23:34
    
    Duration (h): Where: // not working 
    
    Hello you
    
    You are invited to a party the 12/23/44 at 23 in :34.
    The party lasts until -233
    
    See you there!meuserx@slackwhere:~/bin
    now you need to fix the duration as it skips it. You might want to add
    the format you want that date entered in your printf

    and put an endline on your last printf
    printf("%s\n", host);

    this just would require a little fancy coding, for me anyways.
    the need to know how it is being added to is needed.

    month/day/year
    day month year
    etc..
    Code:
    And I need this whole string to be printed out in the message 
    "You are invited to a party the %s at ..."
    "You are invited to a party the 12th of April at ..
    so you can get your input then parse it. then print it out how you want.

    this is just missing something
    Code:
    printf("\nDuration (h): ");
    scanf("%hd", &duration);
    a simple oops mistake.
    Last edited by userxbw; 10-29-2017 at 02:23 PM.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    SOLUTION

    Apparently white space in front of the %-sign seems to make a difference?
    Code:
    #include <stdio.h>
    
    
    int main() {
        
        char host[20];
        printf("Who is this invitation from: "); 
        scanf("%s", host);
        
        char name[20];
        printf("Who is this invitation to: "); 
        scanf("%s", name);
        
        char date[20]; 
        printf("Date: ");
        scanf(" %[^\n]s", date); // white space in front of the %-sign seems to make a difference?
        
        short time, duration; 
        printf("Time: "); 
        scanf("%hd", &time); 
        
        printf("Duration (h): "); 
        scanf("%hd", &duration); 
        
        char destination[20]; 
        printf("Where: "); 
        scanf("%s", destination); 
        
        printf("\n\nHello %s", name); 
        printf("\n\nYou are invited to a party the %s at %hd in %s.", date, time, destination);
        
        short lastsUntil = time + duration;
        printf("\nThe party lasts until %hd", lastsUntil);
         
        printf("\n\nSee you there!\n"); 
        printf("%s", host); 
        return 0;
    }
    Input and output example:

    Who is this invitation from: Elsa
    Who is this invitation to: Lucas
    Date: 12th of April
    Start time: 19
    Duration (h): 4
    Where: Marinmuseet
    Hello Lucas!
    You are invited to a party the 12th of april at 19 in Marinmuseet.
    The party lasts until 23.
    See you there!
    Elsa
    Last edited by DecoratorFawn82; 10-29-2017 at 02:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im printing 3 lines of the same on all my lines, please help
    By mountaindrew11 in forum C Programming
    Replies: 8
    Last Post: 04-11-2016, 10:57 AM
  2. Replies: 4
    Last Post: 04-15-2014, 07:57 PM
  3. Translate a string matrix[lines][4] into one[lines][2]
    By muacsom in forum C Programming
    Replies: 3
    Last Post: 06-16-2012, 04:45 PM
  4. NUMA Virtual Adress Space / Physical Adress Space
    By NUMA Orly in forum Windows Programming
    Replies: 0
    Last Post: 03-14-2011, 03:19 AM

Tags for this Thread