Reading in data from Text file

This is a discussion on Reading in data from Text file within the C Programming forums, part of the General Programming Boards category; Originally Posted by Elysia Code: char chart1 [11][] = // Array for the Table and Plot points {"London", "Bath", "Cardiff", ...

  1. #46
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by Elysia View Post
    Code:
       char chart1 [11][] = // Array for the Table and Plot points
       {"London", "Bath", "Cardiff", "Carlisle", "Durham", "Exeter", "Leeds", "Norwich", "Truro", "York"},
       {"London", "-", "23", "12", "89", "456", "123", "46", "732", "345", "123"},
       {"Bath", "23", "-", "46", "234", "123", "46", "89", "234", "567", "90"},
       {"Cardiff", "12", "46", "-", "767", "456", "46", "234", "123", "732", "35"},
       {"Carlisle", "89", "234", "767", "-", "732", "32", "48", "67", "98", "100"},
       {"Durham", "456", "123", "456", "732", "-", "234", "46", "89", "89", "732"},
       {"Exeter", "123", "46", "46", "32", "234", "-", "123", "46", "123" "234"},
       {"Leeds", "46", "89", "234", "48", "46", "123", "-", "46", "89", "19"},
       {"Norwich", "732", "234", "123", "67", "89", "46", "46", "-", "123", "732"},
       {"Truro", "345", "567", "732", "98", "89", "123", "89", "123", "-", "78"},
       {"York", "123", "90", "35", "100", "732", "234", "19", "732", "78", "-"};
    Code:
    for (int i = 0; i < (rows); i++)
    {
    	puts(chart1[i]);
    }
    Beware that variables must be defined at the beginning of the function in C.
    i have just been messing around with the code you gave me and i keep getting this :
    http://i57.photobucket.com/albums/g2...e2k/errors.jpg

    im using DEV-C++ now as its way better then pelles C

  2. #47
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    right after some more messing around with the code i have managed to get the program to run however i get loooadddsssss of errors this is how my string is set out atm
    Code:
        char chart1 [11][11] ={ // Array for the Table and Plot points
       'London', 'Bath', 'Cardiff', 'Carlisle', 'Durham', 'Exeter', 'Leeds', 'Norwich', 'Truro', 'York',
       'London', '-', '23', '12', '89', '456', '123', '46', '732', '345', '123',
       'Bath', '23', '-', '46', '234', '123', '46', '89', '234', '567', '90',
       'Cardiff', '12', '46', '-', '767', '456', '46', '234', '123', '732', '35',
       'Carlisle', '89', '234', '767', '-', '732', '32', '48', '67', '98', '100',
       'Durham', '456', '123', '456', '732', '-', '234', '46', '89', '89', '732',
       'Exeter', '123', '46', '46', '32', '234', '-', '123', '46', '123','234',
       'Leeds', '46', '89', '234', '48', '46', '123', '-', '46', '89', '19',
       'Norwich', '732', '234', '123', '67', '89', '46', '46', '-', '123', '732',
       'Truro', '345', '567', '732', '98', '89', '123', '89', '123', '-', '78',
       'York', '123', '90', '35', '100', '732', '234', '19', '732', '78', '-'};
    below is a screenshot of alll the rubish i get up however i dont understand and know how to fix it

    [img=http://img265.imageshack.us/img265/6919/lolffsdfdsfsfv6.th.jpg]

  3. #48
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by fortune2k View Post
    right after some more messing around with the code i have managed to get the program to run however i get loooadddsssss of errors this is how my string is set out atm
    Code:
        char chart1 [11][11] ={ // Array for the Table and Plot points
       'London', 'Bath', 'Cardiff', 'Carlisle', 'Durham', 'Exeter', 'Leeds', 'Norwich', 'Truro', 'York',
       'London', '-', '23', '12', '89', '456', '123', '46', '732', '345', '123',
       'Bath', '23', '-', '46', '234', '123', '46', '89', '234', '567', '90',
       'Cardiff', '12', '46', '-', '767', '456', '46', '234', '123', '732', '35',
       'Carlisle', '89', '234', '767', '-', '732', '32', '48', '67', '98', '100',
       'Durham', '456', '123', '456', '732', '-', '234', '46', '89', '89', '732',
       'Exeter', '123', '46', '46', '32', '234', '-', '123', '46', '123','234',
       'Leeds', '46', '89', '234', '48', '46', '123', '-', '46', '89', '19',
       'Norwich', '732', '234', '123', '67', '89', '46', '46', '-', '123', '732',
       'Truro', '345', '567', '732', '98', '89', '123', '89', '123', '-', '78',
       'York', '123', '90', '35', '100', '732', '234', '19', '732', '78', '-'};
    Note that 'London' is not valid C of any kind -- only single characters can appear inside single quotes. If you want a string, try "London". Note that this means your array needs to be
    Code:
    char chart1[11][11][]

  4. #49
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by tabstop View Post
    Note that 'London' is not valid C of any kind -- only single characters can appear inside single quotes. If you want a string, try "London". Note that this means your array needs to be
    Code:
    char chart1[11][11][]
    right i have every thing into speeh marks ie. "London", however it wont run now it has a problem with
    char chart1[10][10][] ={ // Array for the Table and Plot points

    it says:
    F:\CCCC\asst_new.cpp In function `int main()':
    7 F:\CCCC\asst_new.cpp declaration of `chart1' as multidimensional array must have bounds for all dimensions except the first
    67 F:\CCCC\asst_new.cpp `chart1' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)

    for char chart1[10][10][] does [10][10][] mean 10 by 10 grid and what does the other one do?

  5. #50
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by fortune2k View Post
    right i have every thing into speeh marks ie. "London", however it wont run now it has a problem with
    char chart1[10][10][] ={ // Array for the Table and Plot points

    it says:
    F:\CCCC\asst_new.cpp In function `int main()':
    7 F:\CCCC\asst_new.cpp declaration of `chart1' as multidimensional array must have bounds for all dimensions except the first
    67 F:\CCCC\asst_new.cpp `chart1' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)

    for char chart1[10][10][] does [10][10][] mean 10 by 10 grid and what does the other one do?
    Sorry -- try chart1[10][10][10]. The first is 10 rows, the second is 10 columns, the third is 10 characters in each cell. (That appears to be enough for now -- but if the data could change you may need to change how many characters in each cell.)

  6. #51
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Here is a correct code:
    Code:
    char chart1[][11][11] = 
    	{
    		{ "London", "Bath", "Cardiff", "Carlisle", "Durham", "Exeter", "Leeds", "Norwich", "Truro", "York" },
    		{ "London", "-", "23", "12", "89", "456", "123", "46", "732", "345", "123" },
    		{ "Bath", "23", "-", "46", "234", "123", "46", "89", "234", "567", "90" },
    		{ "Cardiff", "12", "46", "-", "767", "456", "46", "234", "123", "732", "35" },
    		{ "Carlisle", "89", "234", "767", "-", "732", "32", "48", "67", "98", "100" },
    		{ "Durham", "456", "123", "456", "732", "-", "234", "46", "89", "89", "732" },
    		{ "Exeter", "123", "46", "46", "32", "234", "-", "123", "46", "123" "234" },
    		{ "Leeds", "46", "89", "234", "48", "46", "123", "-", "46", "89", "19" },
    		{ "Norwich", "732", "234", "123", "67", "89", "46", "46", "-", "123", "732" },
    		{ "Truro", "345", "567", "732", "98", "89", "123", "89", "123", "-", "78" },
    		{ "York", "123", "90", "35", "100", "732", "234", "19", "732", "78", "-" }
    	};
    You were confusing us by packing them into one more subscript, but didn't specify any brackets to specify an actual subscript.
    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.

  7. #52
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by Elysia View Post
    Here is a correct code:
    Code:
    char chart1[][11][11] = 
    	{
    		{ "-", "London", "Bath", "Cardiff", "Carlisle", "Durham", "Exeter", "Leeds", "Norwich", "Truro", "York" },
    		{ "London", "-", "23", "12", "89", "456", "123", "46", "732", "345", "123" },
    		{ "Bath", "23", "-", "46", "234", "123", "46", "89", "234", "567", "90" },
    		{ "Cardiff", "12", "46", "-", "767", "456", "46", "234", "123", "732", "35" },
    		{ "Carlisle", "89", "234", "767", "-", "732", "32", "48", "67", "98", "100" },
    		{ "Durham", "456", "123", "456", "732", "-", "234", "46", "89", "89", "732" },
    		{ "Exeter", "123", "46", "46", "32", "234", "-", "123", "46", "123" "234" },
    		{ "Leeds", "46", "89", "234", "48", "46", "123", "-", "46", "89", "19" },
    		{ "Norwich", "732", "234", "123", "67", "89", "46", "46", "-", "123", "732" },
    		{ "Truro", "345", "567", "732", "98", "89", "123", "89", "123", "-", "78" },
    		{ "York", "123", "90", "35", "100", "732", "234", "19", "732", "78", "-" }
    	};
    You were confusing us by packing them into one more subscript, but didn't specify any brackets to specify an actual subscript.



    yep it works thanks a bunch however i cant get it to print out on the screen and look pretty it messes up

    im currently using the following to print out on screen
    Code:
          for(row=0; row<11;row++) // outputs the array in  2 loops 
                   {                  
                                      printf("\n\n");   
                          for(col=0;col<=11;col++)
                          {
                             printf("%-6s",chart1[row][col]);
                          }
                         
                   }
    i have b een messing around with the %5s but i keep getting similar outputs 2 this
    http://i57.photobucket.com/albums/g2...tune2k/ddd.jpg
    i want it all aligned nicely and looking fab anyone know a better method to achive this ?

  8. #53
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Thought I would chime in on this drawn out thread.

    Change to

    Code:
    printf("%-8s",chart1[row][col]);
    You must compensate for you longest string. They key it so justify it one way or the other, right or left.

  9. #54
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by slingerland3g View Post
    Thought I would chime in on this drawn out thread.

    Change to

    Code:
    printf("%-8s",chart1[row][col]);
    You must compensate for you longest string. They key it so justify it one way or the other, right or left.
    thats seems to do the trick thanks alot i did that but forgot to change the window size but yep it looks fab cheers

  10. #55
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    right my program is comming along nicley and well id like to thank you all for your help i still havent been able to import from a text file but ill learn that soon.
    im after an exit command soo when the exit option is selected from the menu it will say somthing like goodbye and then close the program. I am also after a thing where the program has finished running a part of the menu and it will have like Press enter to continue and when enter is pressed it will clear the screen and then call upon the menu method. i know how to call upon the menu part the rest i cant seem to find out how to do it .

  11. #56
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by fortune2k View Post
    im after an exit command soo when the exit option is selected from the menu it will say somthing like goodbye and then close the program.
    How do you end the program now? (Hint: It rhymes with "schmeturn".)

    I am also after a thing where the program has finished running a part of the menu and it will have like Press enter to continue and when enter is pressed it will clear the screen and then call upon the menu method. i know how to call upon the menu part the rest i cant seem to find out how to do it .
    As far as C is concerned, your program is running on a teletype -- there is no concept of a screen that can be cleared. If you happen to be working in a non-teletype environment, there is probably a system-specific command you can use.

  12. #57
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    whats teletype mean?

  13. #58
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by fortune2k View Post
    whats teletype mean?
    kids these days (not that I ever dealt with any myself)
    http://en.wikipedia.org/wiki/Teletype

  14. #59
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    hahaha riggghtttt ive been wondering if it is possible to have a switch case using words want somthing like this
    Code:
    char point1[10];
    switch[point1]
    {
    case "London":  blhaaaahahadahaa break;
    case "Belfast" : vsvsdvdsvsdvudsfv  break;
    case "other place" fcsacdsafadsfkjadshfij  break;
    }
    is it possible to do somthing like that or is there an alternative way to do this

  15. #60
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Switch requires an integral type to switch on. However: you happen to have some integers hanging around -- or at least you did a minute ago. chart1[k][0] (for k from 0 to 9) were your city names, correct? Compare your string against those strings (hey hey it's a loop), and then you can switch on k.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from a text file
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2008, 03:30 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  3. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 07:13 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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