Thread: string question

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    19

    Smile string question

    I would like to input a whole name (including spaces), Here's what I have so far:

    #include <stdio.h>

    char name[50];

    int main(void)
    {
    scanf("%s",name);
    printf("%s",name);
    return 0;
    }

    If I were to enter John Doe, only John would come out of the print statement. How do I "Doe"?

  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
    fgets( name, 50, stdin );

    or

    scanf( "%[^\n]", name );
    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
    Sep 2001
    Posts
    19
    I tried using those but when I run the program, it never gives me the chance to input the information. It just flashes by...Did I need to do something else?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    19
    er, Nevermind. Got it going. Thanks for your help!

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    19
    Ok...this is the code that's not working well for me so far. I assume it's a problem with my function calls since I see that the scanf is working correctly. Where am I going wrong? Thanks in advance.

    #include <stdio.h>
    #include <stdlib.h>

    char name[50];
    int selection;

    int main(void)
    {
    scanf("%d",selection);

    switch(selection)
    {
    case 0:
    write_name();
    break;

    default:
    break;
    }

    return 0;
    }

    int write_name(void)
    {
    clrscr();
    printf("What is your name?");
    scanf("%[^\n],name);
    printf("\n%s",name);
    return 0;
    }

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Salem, what is this mean?
    Code:
    scanf( "%[^\n]", name );
    I know scanf, but what is %[^\n]? I can see the newline (\n), but I don't understand the rest. Thanks.
    1978 Silver Anniversary Corvette

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    9
    I think scanf() cannot read spaces, you would have to have two seperate scanf() functions to read the first then last name.. Could be wrong though, Im reading this from an out-of-date book hehe

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    try
    Code:
    gets(name);
    works like a charm for me

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    19

    Unhappy

    I tried gets(name), but upon execution, it doesn't let me input information. I'm not sure what I'm doing wrong here.

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    100

    This could work!

    Try something like

    cin.getline(name,lenght_of_the_name_field,"\n");

    Oh, by the way...
    I think, that you have to include
    #include <iostream.h>
    also!

    Have a nice weekend...
    Sauron a few seconds before his defeat:
    "What? A 'division by 0' error?!?"

  11. #11
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    cin.getline is c++ not c.

    scanf( "%[^\n]", name );

    will read until a newline is encountered. For that reason you'll have to clear the buffer before using it -

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    char name[50]; 
    int selection; 
    int write_name(void) 
    { 
    	int c;
    //	clrscr(); doesn't work on my compiler
    	printf("What is your name?"); 
    	while ((c = getc(stdin)))
            if (c == '\n')
             break;
    
    	scanf("%49[^\n]",name); 
    	printf("\n%s",name); 
    	return 0; 
    }
    int main(void) 
    { 
    	scanf("%d",&selection); 
    
    	switch(selection) 
    	{ 
    	case 0: 
    		write_name(); 
    	break; 
    
     	} 
    
    	return 0; 
    }

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    19

    Cool

    Yay! It works! Thanks!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM