can someone help this time

This is a discussion on can someone help this time within the C Programming forums, part of the General Programming Boards category; I'm suppose to use a multidimensional array to print out a list of names from the scanf() can someone show ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    134

    can someone help this time

    I'm suppose to use a multidimensional array to print out a list of names from the scanf() can someone show me what to do because I've tried a bunch of times and I get weird output heres some code:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define i 30
    
    int main()
    {
    	int x;
    	char iName[i] = {'\0'};
           int iChoice = 0;
    
    
    	while(iChoice != 3)
    	{
    		system("cls");
    		printf("\n1\tEnter a name");
    		printf("\n2\tPrint Report");
    		printf("\n3\tQuit");
    		printf("\n\n\tEnter a Choice: ");
    		scanf("%d", &iChoice);
    
    		if(iChoice == 1)
    		{
    			system("cls");
    			printf("\n\tEnter a name: ");
    			scanf("%s", iName);
    	
    		}
    		else if(iChoice ==2)
    		{
    			break;
    
    		}
    		else if(iChoice == 3)
    			break;
    	}
    	system("cls");
    
    	for(x=0;x<=2;x++)
    		printf("%s\n",iName[x]);
    
    }
    I'm not using a multidimensional array in this example but I couldn't even get it to work with a one dimensional array

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I only reply to a topic with such a horrible title because I'm in a good mood today...

    Anyways, is this a joke?
    The whole problem (at least, the biggest problem) is the fact that you're NOT using a multidimensional array. If you don't use one, you can't expect it to behave as one, can you?

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Do you have a book?

    Seriously, you can't expect to learn C by asking infinite questions.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by zacs7 View Post
    Seriously, you can't expect to learn C by asking infinite questions.
    What was saying he wanted to learn C? He just wants to pass school. That way, he can bother actual coders that become his colleagues later.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    134
    I guess that was a rude title but I asked on here before and didn't get a good answer its not for class the problem is in a book called c programming for the absolute beginner sorry for asking such a lame question I just can't get it to print out even using a one dimensional array If you guys don't want to tell me thats fine I understand maybe I've been asking to many questions on here hee hee

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Ask as many questions as you like but try to use your brain when you ask them. For example:

    can someone show me what to do because I've tried a bunch of times and I get weird output heres some code:
    So what, you want me to test the code for you? Someone might if they are bored enough, but if YOU are looking for an answer, the best way to do it is make it EASIER for the person you are asking.

    That means focus on the issue. Explain the weird output. You can even cut and paste an example.

    How To Ask Questions The Smart Way

    Here's an observation:
    Code:
    printf("%s\n",iName[x]);
    %s refers to a string, but iName[x] is not a string, it's a single character (%c) in a string.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    134
    sorry guys I will read the thing on how to ask smarter questions I get this error when I run the code do you know what it means

    C:\Users\artist\Documents\Pelles C Projects\NamesArray\NamesArray.c(26): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'char *' but found 'int'.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define i 30
    
    int main()
    {
    	int x;
    	char iName[i] = {'\0'};
           int iChoice = 0;
    
    
    	while(iChoice != 3)
    	{
    		system("cls");
    		printf("\n1\tEnter a name");
    		printf("\n2\tPrint Report");
    		printf("\n3\tQuit");
    		printf("\n\n\tEnter a Choice: ");
    		scanf("%d", &iChoice);
    
    		if(iChoice == 1)
    		{
    			system("cls");
    			printf("\n\tEnter a name: ");
    			scanf("%c", iName[i]);
    	
    		}
    		else if(iChoice ==2)
    		{
    			break;
    
    		}
    		else if(iChoice == 3)
    			break;
    	}
    	system("cls");
    
    	for(x=0;x<=2;x++)
    		printf("%c\n",iName[i]);
    
    }

    also I change the code around a little so I posted the code again I used the character specifier like you said does c even have a string specifier i think thats what their called

    I hope you guys can be patient with me because I tried a whole slew of things and I can't get the print out to do what I want really all I want to do is to enter in 5 names first and last and print them out I just can't get the results I've tried for days

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You should be passing the address, not the char itself. Use the & operator like you do with the first scanf.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Bigger problem:
    Code:
    scanf("%c", iName[i]);
    i = 30. iName is a string (a 1D array), allocated 30 elements. The first one is 0. The LAST one is 29. So iName could be:

    "abcdefghijklmnopqrstuvwxyz123"
    Because the last element of a string (iName[29]) should be '\0', the "null terminator".

    Anyway, your iName[i] -- iName[30] -- is stepping past the bounds of your array! There is no iName[30].

    If you just want to read one name in, you were right the first time:
    Code:
    scant("%s",iName);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    134
    hey i did that and it worked but now my code is looping where I don't want it to in other words its

    saying

    1 Enter a name:
    2 print report
    3 quit

    it keeps looping to that so thats something that I will have to work on myself and get right before I can post again maybe I just need to enter a break or something maybe my code isn't setup logically but I will figure it out thanks I'm getting closer

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    134
    hey thanks man I'm working on restructuring the whole program because I think it might not be logically correct but I will keep what you posted in mind when I get back to that part

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    134
    well I rewrote the program a little

    I don't get any errors in this code but it won't run it will say iNamesArray stopped working checking for solution is that a runtime error here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int i  = 2;
    	int x = 0;
    	char iName[ ] =  {'\0'};
           int iChoice = 0;
    
    	while (x != i )
    	{
    		system("cls");
    		printf("\n1\tEnter a name");
    		printf("\n2\tPrint Report");
    		printf("\n3\tQuit");
    		printf("\n\n\tEnter a Choice: ");
    		scanf("%d", &iChoice);
    	
    		if (iChoice == 1)
    		{
    			x++;
    			system("cls");
    			printf("\n\tEnter a name: ");
    			scanf("%c", &iName[i]);
    			break;
    		
    		}
    	
    
    		if (iChoice == 2)
    		{
    			system("cls");
    			printf("%c\n",iName[0]);
    
    	       }
    		if (iChoice == 3)
    			system("cls");
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-02-2006, 11:46 PM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

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