Having trouble accessing strings within an Array:/ Please help.

This is a discussion on Having trouble accessing strings within an Array:/ Please help. within the C Programming forums, part of the General Programming Boards category; I am working on a program that has an input file like this: 3 Adam Bob John Kathy Jennifer Whitney ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Having trouble accessing strings within an Array:/ Please help.

    I am working on a program that has an input file like this:

    3
    Adam Bob John
    Kathy Jennifer Whitney

    3 is the number of each gender.
    All the men names are separated by a space. All the women names are also separated by a space.

    How do I read in all the names into an array and then access ONE name from each line?


    I tried something like:
    Code:
    for (i=0; i < numPairs; i++){
       
    fscanf(ifp, "%s", &nameOfPerson)   
    }
    Where nameOfPerson is declared as an char array of size 20 (since each name is 20 characters in length max).


    So how would I access the name bob skipping the rest of the names? or how would I access the name whitney skipping the rest of the names?


    Thanks in advnace!

    (Please give me a helpful answer b/c I am working a big comparison project in Computer science where I need to use recursion and I do not want to spend too much time on figuring out generalized hints)
    Last edited by matthayzon89; 09-20-2010 at 11:16 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    fgets + strtok is probably a good idea.
    Be sure to read SourceForge.net: Scanf woes - cpwiki
    Note that the fscanf is dangerous and it's wrong. Don't use & on arrays; arrays implicitly decay to pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 11:28 AM
  2. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 12:48 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 03:53 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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