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 JDGATX You didn't change what they told you to change. Start there. ey ? what you mean...

  1. #151
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by JDGATX View Post
    You didn't change what they told you to change. Start there.
    ey ? what you mean

  2. #152
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    (!strcmp
    get rid of the red
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #153
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by vart View Post
    Code:
    (!strcmp
    get rid of the red
    yes i have done that and well i have been fiddling around n well now i am tottally confussed of wat sympols to use ect ect atm
    Code:
        for(x = 0; x < sizeof(cities) / sizeof(*cities); x ++) 
                                {
                                  if(strcmp(cities[x], pos[i])){point[i] = x;} // assigns point a number depends what place name was enterd  
                                  if(strcmp(cities[x], pos[i])) {flag=1;}    
                                }
                                if(flag==0)
                                {
                                   printf("\n Invalid place name please re enter: ");
                                   i--;
                                   wrong=1;       
                                }
    i have just that atm and just trying differnt symbols like ==0 ==1 >0 <0 ectect im just confussed

  4. #154
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Go check the documentation for the return of strcmp. Don't guess!
    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.

  5. #155
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    all i could find is how to initialize strcmp and well the outcome
    Code:
     
    Negative if s1 is less than s2.
    Zero if s1 and s2 are equal.
    Positive if s1 is greater than s2
    i still dont understand lol can anyone link me somthing so i can understand this stuff its been haunting me for a few days now

  6. #156
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by fortune2k View Post
    all i could find is how to initialize strcmp and well the outcome
    Code:
     
    Negative if s1 is less than s2.
    Zero if s1 and s2 are equal.
    Positive if s1 is greater than s2
    i still dont understand lol can anyone link me somthing so i can understand this stuff its been haunting me for a few days now
    yes Reading in data from Text file
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #157
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Code:
    Negative if s1 is less than s2.
    Zero if s1 and s2 are equal.
    Positive if s1 is greater than s2
    What's the problem? I think both the doc and vart is clear. The answer has been handed to you several times.
    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.

  8. #158
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by vart View Post
    you do not need it here

    Code:
    if(strcmp(...) == 0)
    {
       /* strings match */
    }
    or

    Code:
    if(strcmp(...) != 0)
    {
       /* strings do not match */
    }
    depending on what you need
    doesnt workkkk
    Code:
     if(strcmp(cities[x], pos[i])==0){point[i] = x;} 
     if(strcmp(cities[x], pos[i])!=0) {flag=1;}
    i get no error message no point output
    Last edited by fortune2k; 04-04-2008 at 06:37 AM.

  9. #159
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fortune2k View Post
    doesnt workkkk
    Code:
     if(strcmp(cities[x], pos[i])==0){point[i] = x;} 
     if(strcmp(cities[x], pos[i])!=0) {flag=1;}
    i get no error message
    So when you say "doesn't work" and "i get no error message", what do you expect to happen (that is the "works" I suppose), and how is that different when you say "doesn't work". The above is correct code for point[i] being set to x when pos[i] matches cities[x], and flag will be set to one if pos[i] is not equal to cities[x] - of course, if that's a direct copy of the code, I would prefer:

    Code:
     if(strcmp(cities[x], pos[i])==0)
     {
       point[i] = x;
     } else {
       flag=1;
     }
    Edit: to clarify - if you have already tested that if the two strings are equal, comparing them again to see if they are "not equal" doesn't make much sense - we already know that. And strcmp() can be quite an expensive operation if the strings are nearly equal and long, so it's good practice to not do comparisons when they are not needed.

    Edit2: Having read your code again, I think it looks like flag should be set to 1 when you have found the city, not the other way around. So perhaps what you really want is:
    Code:
     if(strcmp(cities[x], pos[i])==0)
     {
       point[i] = x;
       flag=1;
     }
    Your code as it stands would always set flag = 1 at some point, because there's only at most one city name that matches, and all others will be a non-match - so you probably want set the flag ONLY when you have a match.
    --
    Mats
    Last edited by matsp; 04-04-2008 at 06:38 AM. Reason: Some clarification.
    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. #160
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    The code works, but the logic is all wrong. Think about what you're doing. When does flag need to be set? Perhaps you should rename it to something better, such as bNotFound or bFound or something like that.
    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.

  11. #161
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    i found the problem it was that
    Code:
        const char *cities[] = {"London", "Glasgow", "Birmingham",	"Manchester", "Luton", "Poole", "Stafford", "Stoke", "Cardiff", "Derby"};
    is staticly set and not in the right orderi need to gather that info from the txt file i cant have nothing statically set i may have to do somthing with the array places[11] because that gets all the place names how would i set this

  12. #162
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Elysia, I see that you are a Programming MASTER! Congrats! Now it's time for you to set up an appointment with your counselor to learn to be more patient with people. This forum is a very very helpful tool. It's filled with people’s challenges & questions. Either you contribute to the solutions, like I know you do in many cases or simply opt out and keep YOUR opinions of peoples short comings, errors, or mistakes to yourself. Keep all your posts as factual as possible. I'm sure everyone will love you for it! It's important to keep people positive in c programming!

    Yes first post, but one all in here want to see.
    ROCK

  13. #163
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When the post is the 160th of the thread, it shows quite a bit of patience, I would say.

    --
    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.

  14. #164
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    anyone know how i can have this constructor gather the info from text file or place array

  15. #165
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fortune2k View Post
    anyone know how i can have this constructor gather the info from text file or place array
    What is your current code for reading the cities?

    --
    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.

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