limiting printf output in scanf multi dimension character array help

This is a discussion on limiting printf output in scanf multi dimension character array help within the C Programming forums, part of the General Programming Boards category; I have been attempting to get this exercise right but don't know how to write the code to get desired ...

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

    limiting printf output in scanf multi dimension character array help

    I have been attempting to get this exercise right but don't know how to write the code to get desired results

    Code:
     #include <stdio.h>
    
    int main(void)
    
    
    {
    int x=0;
    char  cName[5][21]= {0};
    int  iPick;
    
    
    	printf("Enter the name of 5 people\n\n");
    	
    	
    	for(x=0;x<5;x++){
    	printf("Enter name %d:", x+1);
    	scanf("%s", cName[x]);
    	
    	
    	
    	printf("\n\nWould you like to add another name or view current names stored?\n");
    	printf("1)\tAdd another name\n");
    	printf("2)\tView names stored\n");
    	scanf("%d", &iPick);
    	switch(iPick){
    	case 1:
    	break;
    	case 2:
    	for(x=0;x<5;x++){
    	printf("\nName %d is %s\n",x+1, cName[x]);
    				
    					}
    }
    	}
    }
    
    
    /*Create a program that allows a user to enter up to five names
    of friends. Use a two-dimensional array to store the friendsí
    names. After each name is entered, the user should have the
    option to enter another name or print out a report that shows
    each name entered thus far*/
    My problem is when I select case 2 of the switch, printf displays 5 names instead of the actual amount entered up to the point of me selecting case 2. If I entered 2 names then select case 2 I will see 5 name results with 3 of them being blank. I'd like for the blank ones to not be displayed but don't know how to code it. I tried using an if statement and ! operator to display only the names that aren't null but got compiler errors.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    for(x=0;x<5;x++)
    {
        printf("Enter name %d:", x+1);
        scanf("%s", cName[x]);
    	
        printf("\n\nWould you like to add another name or view current names stored?\n");
        printf("1)\tAdd another name\n");
        printf("2)\tView names stored\n");
        scanf("%d", &iPick);
        switch(iPick)
        {
        case 1:
            break;
        case 2:
            for(x=0;x<5;x++)
            {
                printf("\nName %d is %s\n",x+1, cName[x]);
            }
        }
    }
    You've got a problem with your reuse of the variable x in that second for loop. Once you've selected to view the names in the list, you will go into that second for loop and once that happens you'd be out of luck for entering in anymore names after that point because x would be set to 5 and the outer for loop would not execute after that. You need another variable to control that inner loop which goes from 0 to x so it only displays how many names you've entered thus far.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    38
    Code:
     for(y=0;y<=x;y++){
    	printf("\nName %d is %s\n",y+1, cName[y]);
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 04:58 PM
  2. Array printf / output problem
    By zyntax in forum C Programming
    Replies: 4
    Last Post: 03-09-2010, 01:38 PM
  3. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  4. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 10:25 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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