Thread: Reading in data from Text file

  1. #121
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    anyone know how i can make it soooo if i was to put in france it would check and say sorry invalid place name please re enter i have been trying for ages and still cant get it to work

  2. #122
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    right this is the code im stuck on

    Code:
       char pos[2][16];
        const char *cities[] = {"London", "Glasgow", "Birmingham",	"Manchester", "Luton", "Poole", "Stafford", "Stoke", "Cardiff", "Derby"};
    
        scanf("%15s", pos[i]);
                              size_t x;  
                              for(x = 0; x < sizeof(cities) / sizeof(*cities); x ++) {
                              if(!strcmp(cities[x], pos[i])){point[i] = x;}
                              }
    The user will input a place name saved in pos and if pos is equal to cities which contains = "London", "Glasgow", "Birmingham","Manchester", "Luton", "Poole", "Stafford", "Stoke", "Cardiff", "Derby" ..... i want it to to put out a error message and ask for re input of the place sooo if i was to enter France it would check cities find out its not there and ask for re input can anyone help

    thankyou

  3. #123
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Eh? You want it to print an error if the user enters a city such as London that exists within the array or only if they enter one that isn't in the array?
    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. #124
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by Elysia View Post
    Eh? You want it to print an error if the user enters a city such as London that exists within the array or only if they enter one that isn't in the array?
    yess if it doesnt exist in the array i want a print error ( soz i was sleepy when i wrote that bad english ol )

  5. #125
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What you can do is use a flag, a bool, and initialize it to false.
    Then loop through your array and check if you can find a string that matches what the user inputted.
    If you find one, set flag to true and break the loop.
    That way you know that if the flag is false, no match was found, and if it's true, then the city was found.
    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. #126
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    hmmm im a little confused about how i would implement this

  7. #127
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Start with what you can. Take it one step at a time.
    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. #128
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    atm im messing around with this idea
    Code:
        scanf("&#37;16s", pos[i]);
                              size_t x;  
                              for(x = 0; x < sizeof(cities) / sizeof(*cities); x ++) {
                              if(!strcmp(cities[x], pos[i])==0){point[i] = x;}
                              else if (!strcmp (cities[x],pos[i])!=0)
                                 {
                                   flag++;
                                 }
                              }
                              while(flag==10)
                               {
                                 printf("Invalid place name please re enter: ");
                                 wrong=1;
                                 i++;       
                               }

  9. #129
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    flag found = false;
    for every element in the array
    {
        if (input is the same as array at position i) then
        {
            set flag to true;
            break out of loop;
        }
    }
    if flag is false, the string was not found in the array.
    Pseudo.
    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.

  10. #130
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Thread has a 129 posts and it appears that the OP has made little progress.

    Surely, someone can post a basic input validation function consisting of about 12 lines of code to help this guy out

  11. #131
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And how exactly will that help?
    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.

  12. #132
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Well, the last 130 posts didn't help him much with a basic homework assignment, an assignment that would only take approximately 80 lines of code to write. Maybe it's time to spoon feed him actual code.

  13. #133
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And what will this accomplish? You're just handing code over to someone. Then they'll make the same thing over and over and come back for help.
    It's pretty much the same as handing over some code just to make someone pass their test. In other words, cheating. They won't get anywhere.
    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.

  14. #134
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    The only thing it'll accomplish is to bring an end to this long drawn out thread a lot sooner. I would strongly encourage the OP to discuss his/her homework assignment with his/her instructor for additional guidance and direction since it's apparent he/she is not making much progress on a very basic homework assignment.

  15. #135
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Absolutely. I concur. But handing out the solution is not ideal (note that I'm not implying that are you suggesting this).

    Fortune, perhaps it's time you tried to get some guidance from your instructor?
    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.

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