String array's? How can you???

This is a discussion on String array's? How can you??? within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I'm working on a database program and I'm using array's to store the names entered into the program but seeing ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    73

    Question String array's? How can you???

    I'm working on a database program and I'm using array's to store the names entered into the program but seeing that Strings are already char arrays, it's become quite difficult. I'm having problems using 2 dimensional arrays to do this. Can anyone help? Here's the commented code, it's not too long. Thanks!

    Code:
    int main() //includes and such are above; no other functions yet. :)
    {
      int i = 0, namesEntered = 1, *nePtr; //integers to be used...
      char name[ARRAY_MAX], nameInSlot[ARRAY_MAX][i]; //array's of chars.. "strings"
      
      nePtr = &namesEntered; //namesEntered ("ne") pointer points to the address of the variable...
        
      printf("How many names to enter: "); 
      scanf("%d", nePtr); //get the amount they entered.
      fflush(stdin);    
      printf("\n\n[%d character limit]\n", (ARRAY_MAX - 1)); 
      
      for (i = 0; i <= (namesEntered - 1); i++)
      {
        printf("Enter name %d: ", (i + 1));
        gets(name);
        nameInSlot[ARRAY_MAX][i] = name[ARRAY_MAX]; //this SHOULD put the value of the string
                                    //entered into a 2d array which consists of an string and
                                    //and index to shove it into.... note the "SHOULD"
      }
      
      printf("\nNames:\n");
      
      for (i = 0; i <= namesEntered; i++) //loop for output.
          printf("%s\n", nameInSlot[ARRAY_MAX][i]); //ends up all it does is print "(null)" odd.
                                                    //or crashes the whole program. blah.
      system("pause"); //press anykey...
      return 0; //exit
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> fflush(stdin);
    This is not a good idea

    >>gets(name);
    Nor is this

    >>char nameInSlot[ARRAY_MAX][i];
    How big is i when this variable is declared? Answer: 0. Therefore, you've created an array like so:
    nameInSlot[ARRAY_MAX][0];
    Also, using variables within an array declaration is allowed (unless you're using C99 standard compiler). The array sizes have to be known at compile time.

    >>nameInSlot[ARRAY_MAX][i] = name[ARRAY_MAX];
    This does not copy a string, to do that you need strcpy().

    Here's an adapted version to show you the way forward. It isn't by any means complete, but it should get your going in the right direction.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ARRAY_MAX 10
    #define MAX_NAME_LENGTH 50
    
    int main() //includes and such are above; no other functions yet.
    {
      char nameInSlot[ARRAY_MAX][MAX_NAME_LENGTH];
      int i;
    
      for (i = 0; i < ARRAY_MAX; i++)
      {
        printf ("Enter name %d: ", i+1);
        fflush(stdout);
        fgets(nameInSlot[i], MAX_NAME_LENGTH, stdin);
      }
    
      printf("\nNames:\n");
    
      for (i = 0; i < ARRAY_MAX; i++)
        printf("%s\n", nameInSlot[i]);
    
      system("pause");
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    Thanks a lot, I'll be sure to make note of doing it that way! I actually figured a way how to do it with structures (arraying the structure string) after a little bit of posting this thread, which took up more space than you way. Unfortunatly I couldn't get back to the web site (Was it down??) to say I figured it out... (kept getting a DNS error).

    Anyway, why is "fflush"ing the stdin after a scanf not good? I always thought you had to fflush your input after you used a scanf? (or fscanf) and why do you fflush the stdout after your printf? I read the page you linked to but it didn't really clear it up completely for me.... I didn't really see anything they flushed the input with.. the closest I saw was the "cout.flush()". (personally, I'm not a fan of "cin" and "cout"... it takes up too much room, well atleast the cout). ????????????????????????
    Last edited by Nuke; 05-12-2003 at 04:44 PM.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    Ok, so I shouldn't use regular scanf if I can and just use sscanf? Thanks a lot for clearing that up, I'll look around the site for the syntax of sscanf. (that is, if it's different. )

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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