Thread: How do i get array work for string?

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    8

    How do i get array work for string?

    here's the example of the code

    Code:
    #include <stdio.h>
    
    void initialize (void);
    void menu (void);
    
    int main (void)
    {
    	initialize ();
    
    	return 0;
    }
    
    void initialize ()
    {
    	char choice[10];
    	int i;
    
    	for (i=0;i<5;i++)
    	{
    		printf("\nEnter the name of guitar : ");
    		scanf("%s",&choice[i]);
    	}
    
    	for (i=0;i<5;i++)
    	{
    	    printf("%s\n",choice[i]);
    	}
    }
    i want to make the question, Enter the name of guitar, be repeated 5 times, for user to input 5 different names, then getting output with 5 different names. any error i did above there? as the thing didnt work out.
    furthermore, i dont really understand how does array works

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    >>furthermore, i dont really understand how does array works
    http://www.cprogramming.com/tutorial/lesson8.html
    Last edited by GanglyLamb; 07-31-2006 at 09:37 AM.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Almost! One of the most important things to consider is user input. User Input: Strings and Numbers [C]

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    8
    i still dont get it, after reading those links
    i tried using fgets, and it wont work as well. furthermore, i havent thought to use fget, so it wouldnt be nice to be using on it when i'm doin my work.

    the error comes from here

    Code:
           for (i=0;i<5;i++)
    	{
    	    printf("%s\n",choice[i]);
    	}
    once i replace the choice[i] to choice, there wont be any error, but things dont work out fine.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Are you trying to accept and regurgitate 5 guitar names? If so, try using a 2-dimensional array (e.g. choice[5][10]).
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    8
    thanks, it works now.
    thanks alot, i've been doin this all nite, and now its working.

    but the output goes weird when it is set into another function

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void initialize (void);
    void menu (void);
    
    int main (void)
    {
    	initialize ();
    
    	return 0;
    }
    
    void initialize ()
    {
    	char choice[5][10];
    	int i;
    
    	for (i=0;i<5;i++)
    	{
    		printf("\nEnter the name of drinks : ");
    		scanf("%s",&choice[i]);
    	}
    
    	menu ();
    }
    
    void menu ()
    {
    	char choice[5][10];
    	int i;
    
    	for (i=0;i<5;i++)
    	{
    		printf("%s\n",choice[i]);
    	}
    }
    it is displaying weird signs

  7. #7
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    That's because in initialize() you allocate memory for one array of strings, fill it with drink names, and then in menu() you allocate memory for another array and print those strings...

    Try putting the array declaration in global scope.

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>but the output goes weird when it is set into another function

    That's because it doesn't get sent to the other function... the choice array in
    initialise is not the same as the choice array in menu. Read up about functions

    Also,

    Code:
    scanf("%s",&choice[i]); /*& not necessary*/
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, that's because you're using 2 different arrays... you have to pass the one from initialize() into menu() to do what you're trying to do:

    In initialize() do: menu(choice)

    Declare menu() as: void menu(char choice[5][10])

    Get rid of the choice declaration inside menu().

    EDIT: *shakes fist at Richie for beating him*
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by skydancer
    it is displaying weird signs
    Shure it does. Just because you use the same name of the variable in initialize() and menu() don't make them the same variable.
    You could either declare choice as a global variable ( not so nice ) or declare it in main() and pass it to the other functions.
    Kurt
    EDIT: this was really slow

  11. #11
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by skydancer
    thanks, it works now.
    thanks alot, i've been doin this all nite, and now its working.

    but the output goes weird when it is set into another function

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void initialize (void);
    void menu (void);
    
    int main (void)
    {
    	initialize ();
    
    	return 0;
    }
    
    void initialize ()
    {
    	char choice[5][10];
    	int i;
    
    	for (i=0;i<5;i++)
    	{
    		printf("\nEnter the name of drinks : ");
    		scanf("%s",&choice[i]);
    	}
    
    	menu ();
    }
    
    void menu ()
    {
    	char choice[5][10];
    	int i;
    
    	for (i=0;i<5;i++)
    	{
    		printf("%s\n",choice[i]);
    	}
    }
    it is displaying weird signs

    You're not sending it to another function. In menu, you're just declaring a multi-dimensional array that has absolutely nothing to do with your origina choice array.

    You need to actually pass the array (well a pointer to it) into the menu function.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM