Thread: Reading in data from Text file

  1. #136
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    lol my instructer will say look at the lecture notes and well theres nothing on there bout what im stuck on lol i aint the best at C but its giving me headaches but im gonna try figure this out

  2. #137
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    right i have managed to get it to put out an error message but having problems with if its true setting the point for the places.

    i decided to use flag as a int and set it to 1 or 0.

    this is what i have
    Code:
             wrong=0;  
                        scanf("%16s", pos[i]);
                              size_t x;  
                              flag = 0;
                              for(x = 0; x < sizeof(cities) / sizeof(*cities); x ++) 
                                {
                                  if(!strcmp(cities[x], pos[i])==0){point[i] = x;} // assigns point a number depends what place name was enterd  
                                  else {flag=1;}    
                                }
                                if(flag==0)
                                {
                                   printf("\n Invalid place name please re enter: ");
                                   i--;
                                   wrong=1;       
                                }
    output :
    http://i57.photobucket.com/albums/g230/fortune2k/dd.jpg

  3. #138
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should get yourself a debugger. Perhaps Visual Studio.
    And note that this line
    Code:
     if(!strcmp(cities[x], pos[i])==0){point[i] = x;}
    Looks suspicious.
    Strcmp returns 0 if the strings match. Yet the "!" operator inverses the result so if strcmp returns 1, it will be changed to 0 before the comparison. Likewise, if it returns 0 or < 0, it will be 1 before the compare. Watch out.
    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.

  4. #139
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    im using dev c++ maybe i should change sooo what shall i change it to then i tryed !=0 and taking it out completly and well it doesnt work

  5. #140
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    if(strcmp(cities[x], pos[i])==0){point[i] = x;}
    Remove the "!".
    Strcmp returns 0 if the strings are equal.
    I suggest you get a debugger and learn basic debugging facilities. Stepping through code and checking the value of variables.
    Basic stuff. There will come a day when you need a debugger.,
    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.

  6. #141
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Likewise, if it returns 0 or < 0, it will be 1
    How is it so?

    Code:
    #include<stdio.h>
    int main() 
    {
        printf("&#37;d %d %d\n", !(-1), !0 , !1);
        return 0;
    }
    output
    Code:
    0 1 0
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #142
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's what I get for not testing
    I assumed since < 0 will result in false, reversing the value will turn it "true."
    Well, that goes to show that using ! can be dangerous if not used properly.
    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. #143
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    values that are < 0 as well as the values that are >0 all evaluate to true
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #144
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    ok uve confused me now

  10. #145
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by vart View Post
    values that are < 0 as well as the values that are >0 all evaluate to true
    Brain error!
    The area in the brain where the information was stored has been short circuited, so it fell back on trying to guess with the information it had which obviously turned out wrong!

    Fortune: Don't use !strcmp, just do strcmp and compare to 0 to see if the strings match.
    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. #146
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by fortune2k View Post
    ok uve confused me now
    What exactly?
    in C:
    0 - is false
    all other values - are true
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #147
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    yes i understand that i mean how to have it set in the if statement

  13. #148
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by fortune2k View Post
    yes i understand that i mean how to have it set in the if statement
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #149
    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



    right this is what i have
    Code:
       scanf("%16s", pos[i]);
                              size_t x;  
                              flag = 0;
                              for(x = 0; x < sizeof(cities) / sizeof(*cities); x ++) 
                                {
                                  if(!strcmp(cities[x], pos[i])==0){point[i] = x;} // assigns point a number depends what place name was enterd  
                                  else if(!strcmp(cities[x], pos[i])!=0) {flag=1;}    
                                }
                                if(flag==0)
                                {
                                   printf("\n Invalid place name please re enter: ");
                                   i--;
                                   wrong=1;       
                                }
    i still get no distance output its still the same as

    http://i57.photobucket.com/albums/g230/fortune2k/dd.jpg

  15. #150
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    You didn't change what they told you to change. Start there.

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