Thread: Reading in data from Text file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    What "conctructor"?

    --
    Mats
    right i have this statically set
    Code:
    const char *cities[] = {"London", "Glasgow", "Birmingham",	"Manchester", "Luton", "Poole", "Stafford", "Stoke", "Cardiff", "Derby"};
    i need it to either get the data from the text file or from the places array
    Last edited by fortune2k; 04-04-2008 at 09:51 AM.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Mats, Patience not deniable. Statement made pertaining to over all of posts. Talented - yes, a little abrasive on some responses - yes. Please understand this is not an attack. It's a matter of seeking improvement on information transfer. It was something that needed to be said. Others on this forum would have loved to present what was said, but they do not post in fear of possible reduction in information. Please note that it goes without saying that there are allot of members on this board that do appreciate the contributions that you, Elysia, and all others who know who they are provide. All members on this forum should receive positive direction at all times.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    right i have replaced cities with place and the validation is ok if i put a invalid place in it asks for re entry
    however the distances are incorrect
    http://i57.photobucket.com/albums/g2...Untitled-1.jpg


    the place names in the txt file : ( the - is included as a place name to but doesnt need to be searched for )
    Code:
     -     London	Glasgow	Birmingham	Manchester	Luton	Derby	Poole	Stafford	Stoke	Cardiff
    Last edited by fortune2k; 04-04-2008 at 10:28 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's probably easiest to just skip the first item on the cities row, by adding a dummy fscanf() before the for-loop that reads the city names.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    It's probably easiest to just skip the first item on the cities row, by adding a dummy fscanf() before the for-loop that reads the city names.

    --
    Mats
    what do you mean by that ?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fortune2k View Post
    what do you mean by that ?
    That you add code to read the "no-city" marker at the first position before you read the 10 city names.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    i think ive cracked it still testing this is what ive got
    Code:
         for(x = 0; x < sizeof(place) / sizeof(*place); x ++) 
                                {
                                   if(strcmp(place[x+1], pos[i])==0)
                                   {
                                   point[i] = x;
                                   flag=1;
                                   }
                                }
                                if(flag==0)
                                {
                                   printf("\n Invalid place name please re enter: ");
                                   i--;
                                   wrong=1;       
                                }

    x+1 pushes the array up 1 so it forgets bout -

    sorry for poor formatting

    what you think outputs seem good ect

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    anyone know why i am expierencing do i need a return? i am really confused
    Last edited by fortune2k; 04-04-2008 at 01:53 PM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is your function supposed to do? Does it make sense to return something?

    I can't say for sure if you do or don't - it all depends on what you actually need it to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    well i use this twice
    Code:
       for(i=0;i<2;i++) // loops round  twice allowing the input of 2 points
                {
                     if(wrong!=1)
                     {           
                        if(i==0){printf("\n Please enter your Start point: ");} // input first point 
                        if(i==1){printf("\n Please enter your Finishing point: ");} // input first point  
                        
                     }  
                        wrong=0;  
                        scanf("&#37;16s", pos[i]);
                   size_t x;  
                              flag = 0;
                              for(x = 0; x < sizeof(place) / sizeof(*place); x ++) 
                                {
                                   if(strcmp(place[x+1], pos[i])==0)
                                   {
                                   point[i] = x;
                                   flag=1;
                                   }
                                }
                                if(flag==0)
                                {
                                   printf("\n Invalid place name please re enter: ");
                                   i--;
                                   wrong=1;       
                                }
    i just want it in a method so i can just call upon it twice i have a book here on methods but its not really saying much

    i think if i have to return somthing it will probs be the point array

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Pedantery: Since this is not C++, and your code consequently isn't in a class, it's called a function, not a method.

    The general way to create a function from some code that is part of another function involves something like this:
    * write a new function that is empty inside. E.g.:
    Code:
    void func()
    {
    }
    * mark the code you want to move to a function (just the one portion of code, if it happens to be there multiple times)
    * cut the code (e.g. ctrl-X in Windows type machines).
    * paste it into the new function, between the braces.
    * fix up indentation.
    * Introduce any variables and parameters necessary to the new function.
    * write calls to the function where the code used to be.
    * replace any other instances of the code with calls.

    * Compile, fix any errors/warnings, recompile, <repeat as needed>
    * Run the code, fix any bugs, <repeat as needed>

    Job done.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    lol i get mixxed from methods and functions i think its coz im doin java 2 ok haha lol uve simply expliand how to do them in a few lines where as a book just rambles on thankyou i shall attempt this now

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    right im using
    Code:
    void places()
    {
    }
    program runs however if i was to put in London as a place which is a valid place it doesnt recognize it i call upon the function "void places()"

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You presumably will need something inside the braces to make that function do anything other than waste a few nanoseconds of time (assuming the compiler doesn't remove it all together).

    And if you pass arguments to a function, you need to specify the function parameters, of course.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    welll earlier i was messing around with stuff like
    Code:
    int places(char pos[2][16], char place[11][11], int point[2])
    {
    }
    but i dont really understand functions well sooo it was really guessing messing around

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, 02:30 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03: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, 06: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