Thread: Scanning strings into both 2D & 3D arrays from one line of txt ALL AT ONCE

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Scanning strings into both 2D & 3D arrays from one line of txt ALL AT ONCE

    alright so the goal is to open a file scan in the lines and have the user edit various aspects of each section starting with lines similar to:

    Ball David Tucson 52000
    Doe Oscar Tucson Phoenix 12225.89
    Miller Janet SanDiego 120000

    in which a lastname, firstname, and city character array will be declared and utilized, along with a int digit array.

    however as seen above the city section of each line can contain up to 3 cities, therefor an idea of a 3D array (EXAMPLE: cities[MAX][3][15]) came to mind, in which the MAX lines of txt will be scanned into 3 different sections with 15 characters a section, rather than a 2D array for the last and first names, etc.

    trouble: running a two 'for' loops at the same time to scan one line of txt into both 2D arrays and 3D arrays...and if not all 3 cities are there, then skipping the remaining city entrys and scanning into the int array for the totals.


    ANY IDEAS???
    tanks in advance

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... sit down with pen and paper and figure it out step by step... become the process... then when you understand the problem, you can start working on a solution... be the program! Then once you understand the problem and have a workable solution, figure out which functions, variables etc. you need and start writing code...

    If you get stuck --really stuck, not lazy stuck-- post your code here (in code tags please) and we'll see what we can do.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest using a structure to represent a single line of data.
    I see no need for a 3D array, I would not even use an 2D array (This is counting an array of C-string as a single D array; it is technically a 2D array.)
    I would use two single D arrays.

    One to hold the cites for one person inside the structure.
    An second to hold the structures for all the people.

    Tim S.
    Last edited by stahta01; 04-28-2011 at 07:58 PM.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    alright so each line of txt was changed whereby:

    Ball David 52000 Tucson
    Doe Oscar 12225.89 Tucson Phoenix
    Talley Stephen 181250.50 Seattle Tacoma

    insert
    Code:
     FILE *fpx;
            fpx = fopen("salesData", "r");
    
            if (fpx==NULL) {
                    printf("File not Opened!\n");
                    return(0);
                    }
    
            for (i=0; i<MAX; ++i) {
                    fscanf(fpx, "%s %s %lf %s %s %s", lastname[i], firstname[i], &digits[i], city1[i], city2[i], city3[i]);
                    }
    
            fclose(fpx);
    i kinda understand your concept of a 1D array, yet how would that work for the character arrays, givin a MAX number of lines, each holding 15 characters.

    with the code above...if one line only has 1 city, the other cities start scanning the next line!

    THUS: How do you get the 'for' loop to terminate after that line has no more cities or reaches the max amount of 3 cities?????

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with scanning in strings
    By cdonlan in forum C Programming
    Replies: 3
    Last Post: 11-08-2004, 05:23 PM
  2. Searching and Scanning Arrays
    By apoc632 in forum C Programming
    Replies: 7
    Last Post: 11-28-2003, 03:28 PM
  3. Scanning Strings till no white space is found?
    By HomerJ in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2002, 02:02 PM
  4. Scanning characters & strings
    By rain_e in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 01:43 AM
  5. Scanning a line in a document
    By bc120 in forum C Programming
    Replies: 5
    Last Post: 01-02-2002, 02:06 PM