Help with arrays again

This is a discussion on Help with arrays again within the C Programming forums, part of the General Programming Boards category; Okay, I'm new at this and please if you don't want to help don't post. I need help with this ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    19

    Help with arrays again

    Okay, I'm new at this and please if you don't want to help don't post. I need help with this array that I need to use to print a name. Here is what I have already. Any suggestions. I might be way off here but like I said I'm new. Thanks in advance.
    Code:
    #include <stdio.h>
    main()
    {   int n;
        char nounone[8] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
        nounone[0] = "Victor";
        nounone[1] = "Anna";
        nounone[2] = "Andrew";
        nounone[3] = "Marty";
        nounone[4] = "Lisa";
        nounone[5] = "Tony";
        nounone[6] = "Victor";
        nounone[7] = "Victor";
        printf("Pick a number between 1-8.\n");
        scanf("%d\n", n);
        printf("%d" nounone[n]);
        getchar();
        getchar();
        }
    What I need this for is for a sentence generator. It will have four arrays with eight words to use from the array. Tokens confuse me alot. I've read the section on arrays and strings in my text book but it does not elaborate using any method that I need for this to work.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've got two different things you're doing.

    1) You've got a simple character array. This is fine if you just want one string. If you want an array of strings, it isn't.

    2) You're trying to assign strings to each individual slot in your simple array. This won't work.

    You need an array of pointers. Then you can make them point at strings. Or, you can use a multi-dimensional array.

    Code:
    char *names[5] =
    {
        "Bob",
        "Sally",
        "John",
        "Juan",
        "Mary"
    };
    int x;
    
    for( x = 0; x < 5; x++ )
        puts( names[x] );
    Or...

    Code:
    char names[5][10] =
    {
        "Bob",
        "Sally",
        "John",
        "Juan",
        "Mary"
    };
    
    int x;
    
    for( x = 0; x < 5; x++ )
        puts( names[x] );
    Something like that would suffice. Now, with the latter method, you'll need string functions to copy the contents in and out of the array if you want to change it, or else you'll have to do it a cell at a time.

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

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    first off, you can't create your array like that. When you're declaring your array for the first time you're making it be an array of 8 single char variables, set to the values a, b, and so on. You cannot later go in and set each value to a string. You could set a two dimensional array up that would accomplish the job, and do it like so:

    Code:
    char nounone[][]={"Victor","Anna",/*...and so on*/
    [edit]
    bah! beaten by quzah!:-P
    [/edit]

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    19
    I really appreciate your help. Please be patient. I think I'm getting somewhere. I played around with what you posted, but I need to print one name. Let's just say I wanted to print "Anna". And it also needs to be a number chosen from input from the keyboard. I tried to set it up like this:

    Code:
    #include <stdio.h> 
    
    int main() 
    {
    char *names[8] =
    {
        "Victor",
        "Anna",
        "Betty",
        "Marty",
        "Lisa",
        "Andrew",
        "Tony",
        "Sherry",
    
    };
    int x;
    printf("Please select a number from 0-7.\n");
    scanf("%d", x);
    puts(names[x] );
    
    getchar();
    }
    It compiles fine and I can enter a number but when I press ENTER I get ERROR. What did I do wrong this time?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    scanf("%d", &x);
    But you really ought to search for why to avoid scanf and what to use instead.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    scanf() takes a pointer to x, so just change that line to:

    scanf("%d", &x);

    also, make sure that you return 0 at the end of the main() function since you (correctly) declare it as returning a value.

    Code:
    #include <stdio.h> 
    
    int main() 
    {
      char *names[8] =
      {
          "Victor",
          "Anna",
          "Betty",
          "Marty",
          "Lisa",
          "Andrew",
          "Tony",
          "Sherry",
      };
      int x;
    
      printf("Please select a number from 0-7.\n");
      scanf("%d", &x);
      puts(names[x]);
    
      getchar();
    
      return 0;
    }
    DavT
    -----------------------------------------------

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    19
    Thanks I got it now. I will look for reasons why scanf() is bad to use but for now that is what I'm used to.

    Would it be smart to set up each word that needs to be generated as a seperate function? Just a thought.

    You guys are really smart. Thanks again.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    22

    Watch out for the return values!

    Someone pointed out that you have to be careful of the return types.

    I wrote a program where i declared my main to be .. just
    <
    main ()
    and then i was returning 0 at the end of the program.
    It compiled and worked fine, but i got a little problem later. I bet it was due to the incompatibility.

    Thanks for pointing it out!
    You guys rock.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 03:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

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