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
    i just need that for loop to work in the method i dunno what i need to add to it to get it working

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    atm i aint really worried bout the buffer ect atm im trying to get this function working



    Quote Originally Posted by matsp View Post
    Yes, but I need to see BOTH the function code and the code that calls the function - because if it's not working, then you are doing something wrong either in the calling of the function, or inside the function.

    It would also help if you describe what actually goes wrong (e.g. it doesn't find the right city).

    --
    Mats

    ok ill start it off simple

    this is my function
    Code:
    void places()
    {
       char pos[2][16];
       char place[11][11];
       int point[2];
       int i, x, wrong, flag;
       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;       
                                }
                }

    This is how i call it
    Code:
    places();
    and this is my output:

    http://i57.photobucket.com/albums/g2...Untitled-2.jpg

    it deosnt recognize any valid places which are input


    thats what i have for a starter

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    ive been playing around with all sorts of ways but still i get nothing

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fortune2k View Post
    ive been playing around with all sorts of ways but still i get nothing
    Post what you have tried, and explain what "nothing" means.

    --
    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
    Post what you have tried, and explain what "nothing" means.

    --
    Mats

    Code:
    char 2points( char place[11][11], int point[2], char pos[2][16])
    {
        
        int x, wrong, flag
       for(i=0;i<2;i++) // loops round  twice allowing the input of 2 points
                    {
                        if(wrong!=1) // if wrong is 0 it will out put the following
                        {
                            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;  // set wrong to 0
                        scanf("%16s", pos[i]);
                        flag = 0; // sets flag to 0 so place is invalid until found
                        for(x = 0; x < sizeof(place); x ++) {
                            if(strcmp(place[x+1], pos[i])==0) {
                                // place found
                                point[i] = x; // point at position i is set to the value of x
                                flag=1; //sets flag as 1 because its a valid place
                            }
                        }
                        if(flag==0) // place not found
                        {
                            printf("\n Invalid place name please re enter: ");
                            i--; // decrements i by 1 sooo it will loop round again
                            wrong=1;   // sets wrong to 1 so the ask for start or finsh point is not asked for again
                        }
                    }
       }
    it runs but when i put in a a valid place name it doesnt find it

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    sooo far for the method i have just put it in like this and well called upon it
    Code:
    void places(char pos[2][16], int point[2], char place[11][11])
    {
       int i, x, wrong, flag;
    
       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("%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;       
                                }
                }
    and well im getting no strcmp and anything it wont see valid place names anyone have any reading material

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