two dimensional string array question

This is a discussion on two dimensional string array question within the C Programming forums, part of the General Programming Boards category; Does anyone here have any tips on how to fill a two dimensional string array with a scanf() function. I ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67

    two dimensional string array question

    Does anyone here have any tips on how to fill a two dimensional string array with a scanf() function. I know this is not the best way to do it, but it's what I'm supposed to use. I'm writing a program that asks the user for 7 strings of input. I need to use the preprocessing directive:

    Code:
    #define   N_Strings     7
    so I figured a two dimensional array would work well. Something like:

    Code:
    w[N_Strings][100]
    Could you help me figure out how to read in the 7 strings so they are put in the right spot.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,151
    Code:
    for(i = 0;i < N_Strings;++i)
      scanf("%s", w[i]);
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    Am I correct in saying that w[0] is a pointer to the first row,w[1] is a pointer to the second row,...... w[i] is a pointer to the i-th row or is this wrong? (just trying to understand the concept)

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    Also,how would you test for a newline in there?
    would this work:
    Code:
    for(i = 0; i < N_Strings; ++cnt);
          if( w != '\n')
          scanf("%s", w[i]);

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    i meant ++i on the end of the for statement.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,681
    Quote Originally Posted by Hoser83
    i meant ++i on the end of the for statement.
    That's what the edit button is for.
    I used to be an adventurer like you... then I took an arrow to the knee.

  7. #7
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Quote Originally Posted by Hoser83
    Also,how would you test for a newline in there?
    would this work:
    Code:
    for(i = 0; i < N_Strings; ++cnt);
          if( w != '\n')
          scanf("%s", w[i]);

    Consider researching the strchr() function to find a certain character in a character array.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,671
    Quote Originally Posted by Hoser83
    Also,how would you test for a newline in there?
    would this work:
    Code:
    for(i = 0; i < N_Strings; ++cnt);
          if( w != '\n')
          scanf("%s", w[i]);
    you got to say w[i] in the if statmenet a well. otherwise u are just checking the w[0]th string b'cose the w is the base address and the i is the off set. changes made code will soemthing look as follow

    Code:
    if(w[i] != '\n')
       ....
       ....
    as said strchr() is the best to do this kinds of things

    ssharish2005

  9. #9
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    thanks man, I appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 05:44 PM

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