scanf multi dimension character array help

This is a discussion on scanf multi dimension character array help within the C Programming forums, part of the General Programming Boards category; I am trying to complete an exercise and am unable to proceed to the next string after pressing enter. I ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    38

    scanf multi dimension character array help

    I am trying to complete an exercise and am unable to proceed to the next string after pressing enter. I have no warning in the compiler so am unsure of what's causing the issue.



    Code:
    #include <stdio.h>
    
    int main(void)
    
    
    {
    int x=0;
    char  cName[5][21];
    
    
    
    	printf("Enter the name of 5 people\n\n");
    	
    	printf("Enter name %d:\t", x+1);
    	for(x=0;x<6;x++)
    	scanf("%s", cName[x]);
    	
    	
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You only prompt once, so you'll only see it ask once. After that, you aren't prompting, it's just expecting you to type. Also, you are hitting enter after you type the number, so it is probably skipping one of your attemptes. You generally need to take care of that \n that you've entered when you use multiple calls to scanf.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    38
    Thanks

    I've changed the code a tad,
    Code:
    for(x=0;x<5;x++){
    	printf("Enter name %d:", x+1);
    	scanf("%s", cName[x]);
    	}
    If I press space then enter a last name the last name is counted towards a different scanf input is there anyway to enter first and last name without using an underscore or creating a new variable?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    %[^\n] should do the trick. You will want to probably read up on the format specifiers in your man page or book. Or use something like fgets.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-23-2010, 03:19 PM
  2. Help printing a multi array
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-05-2008, 02:35 PM
  3. input row of integers via scanf into an array...
    By noodles355 in forum C Programming
    Replies: 1
    Last Post: 11-20-2006, 10:12 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Help with 2-D character array
    By choykawairicky in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 01:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21