Thread: Reading in data from Text file

  1. #181
    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

  2. #182
    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.

  3. #183
    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.

  4. #184
    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

  5. #185
    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.

  6. #186
    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

  7. #187
    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()"

  8. #188
    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.

  9. #189
    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

  10. #190
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Those are valid parameters - I can't really say if that's the RIGHT thing to do or not, as I don't know enough of what the rest of the code looks like.

    What does the call look like?

    And of course, you still need some code INSIDE the function.

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

  11. #191
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    Those are valid parameters - I can't really say if that's the RIGHT thing to do or not, as I don't know enough of what the rest of the code looks like.

    What does the call look like?

    And of course, you still need some code INSIDE the function.

    --
    Mats
    illl paste my code below firstly i appologize for the poor formatting and layout it still needs quite a bit of work doing

    my program code :
    Code:
    // Header Files
    #include <stdio.h>
    #include <stdlib.h>
    
    //prototypes
    int menu(void);
    int strcmp( const char *str1, const char *str2 );
    void read(char place[11][11], int dist[10][10]);
    void display(char place[11][11], int dist[10][10]);
    
    int main (void)
    {
        char place[11][11];
        int dist[10][10]; 
        int row, col, i, numstops, end, wrong, flag, option;
        double price;
        int stops[3], point[2];
        char pos[2][16];
        char stopname[3][16];
    
        
        read(place,dist); 
        
        while(end !=1)
        {
             option = menu();  // calls upon the menu
           
             switch(option) // using a switch statement for the menu option
             {
              case 1:   display(place,dist);
              break;  
              
              case 2:   
               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;       
                                }
                }
                      
      
                 printf("\n Please Enter the number of stops 0-3: ");
                 scanf("%5d",&numstops);
                  while(numstops >3 || numstops <0)
                    {
                      printf("\n Invalid number of stops please re enter: ");
                      scanf("%5d",&numstops);
                    }               
                  
                    if(numstops==0)
                     {
                       printf("\n From %s To %s is %d miles",pos[0],pos[1],dist[point[0]][point[1]]);     
                       printf("\n Total distance  : %d miles", dist[point[0]][point[0]]+dist[point[0]][point[1]]); // Adds the 2 points togteher to work out the distance 
                       printf("\n\n\n");
                     }
    
    	 if(numstops>0)
    	 {
    
    		 for(i=0;i<numstops;i++) //loops round depeneding on how many stops was enterd
    		 {
    			 if(wrong !=1)
    			 {                     
    				 printf("\n Please enter the name for Stop %-1d: ",i+1);
    			 }
    			 wrong=0;     
    			 scanf("%16s",stopname[i]);
    			 size_t x;
    			 flag =0;
    		        for(x = 0; x < sizeof(place) / sizeof(*place); x ++) 
                     {
                       if(strcmp(place[x+1], stopname[i])==0)
                        {
                           stops[i] = x;
                           flag=1;
                         }
                      }
                     if(flag==0)
                      {
                        printf("\n Invalid place name please re enter: ");
                        i--;
                        wrong=1;       
                       }
    
    		 }
                        //will print out the distances depending on how many stops was enterd
                        if(numstops==1)
                          {
                          printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],dist[point[0]][stops[0]]);
                          printf("\n From %s To %s is %d miles",stopname[0],pos[1],dist[point[1]][stops[0]]); 
                          printf("\n Total Distance : %d",dist[point[0]][stops[0]]+dist[point[1]][stops[0]]); 
                          printf("\n\n\n");
                          }
                          else if(numstops==2)
                          {
                          printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],dist[point[0]][stops[0]]);
                          printf("\n From %s To %s is %d miles",stopname[0],stopname[1],dist[stops[0]][stops[1]]); 
                          printf("\n From %s To %s is %d miles",stopname[1],pos[1],dist[point[1]][stops[1]]);
                          printf("\n Total Distance : %d",dist[point[0]][stops[0]]+dist[stops[0]][stops[1]]+dist[point[1]][stops[1]]);
                          printf("\n\n\n");
                          }
                      
                          else if(numstops==3)
                          {
                          printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],dist[point[0]][stops[0]]);
                          printf("\n From %s To %s is %d miles",stopname[0],stopname[1],dist[stops[0]][stops[1]]); 
                          printf("\n From %s To %s is %d miles",stopname[1],stopname[2],dist[stops[1]][stops[2]]); 
                          printf("\n From %s To %s is %d miles",stopname[2],pos[1],dist[point[1]][stops[2]]);
                          printf("\n Total Distance : %d",dist[point[0]][stops[0]]+dist[stops[0]][stops[1]]+dist[stops[1]][stops[2]]+dist[point[1]][stops[2]]);
                          printf("\n\n\n");
                          }
                  
                   }  
              break; 
              
              
              
              
              case 3:    
                      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;       
                                }
                }
                      
    
                         printf("\n From %s To %s is %d miles",pos[0],pos[1],dist[point[0]][point[1]]);    
                         if(dist[point[0]][point[0]]+dist[point[0]][point[1]] <=100)
                         {
                          price = 0.4;
                          printf("\n Your Journey is Greatert than 100 miles, Price per mile = %lf",price);
                          }
                         else
                         {
                          price =0.3;
                          printf("\n Your Journey is less than 100 miles, Price per mile = %lf",price);
                         }  
    	                 printf("\n The price of your journey is : %5lf", dist[point[0]][point[0]]+dist[point[0]][point[1]]*price); // Adds the 2 points togteher and multiplys by 0.4 to get the ppm
                         printf("\n\n\n"); 
              break; 
              
              case 4: printf("\n Thankyou and goodbye"); 
                      exit(0);
                      break;	
       
             }
        }    
        
        
    }
    
    
    
    int menu(void) // menu funcion
    {       
        int option;
     	printf("\n ============================================");
    	printf("\n      M          E          N          U     ");
    	printf("\n ================++++++++++++================");
    	printf("\n 1. Display a complete mileage place         ");
    	printf("\n 2. Calculate the distance of your journey   "); 
    	printf("\n 3. Calculate the cost of your journey       ");
    	printf("\n 4. Exit                                     ");
    	printf("\n --------------------------------------------");
    	printf("\n\n Please input your choice from the menu:   ");
        scanf("%d",&option); 
        while(option<0 || option>4)
        {
    	 printf("\n Invalid menu input please re enter: ");
         scanf("%d",&option);                 
        }
       printf("\n\n");
       return option; 
    }
    
    
    
    
    
    void read(char place[11][11], int dist[10][10])
    {
        int i, j;
     	char filename[21], p1, p2;
    	FILE *in_file;
    
    	printf("\nEnter the name of the input file, name must be 20 charecters or less: ");
    	scanf("%s", &filename);
    	in_file = fopen(filename,"r");
            if (in_file == NULL)
    	   {
    		printf("\nCannot open %s\n", &filename);
    		exit(0);
    	   }
           else if (in_file != NULL)
           {
    	       for(i=0;i<11;i++)
    	       {
    	     	fscanf(in_file, "%s ", &place[i]);
    	        }
    
    	        for(i=0;i<10;i++)
    	       {
    		        for(j=0;j<10;j++)
    		        {		
    			      fscanf(in_file, "%d ", &dist[i][j]);
    		        }
    	       }
              printf("%s Opened Successfully", &filename);
              printf("\n\n\n");
           }
     fclose(in_file);
    }
    
    
    
    
    
    
    
    
    void display(char place[11][11], int dist[10][10])
    {
            int row, col;
            printf("\n------------------------------------<<]]+++|||||MILEAGE CHART|||||+++[[>>-------------------------------------------\n\n"); 
            
             for (row=0; row<11;row++) 
            {
                printf("%-11s",place[row]);
            }
            printf("\n");
            for (row=0; row<10;row++) 
            {
                printf("%-11s",place[row+1]);
                
                    for (col=0;col<10;col++)
                    printf("%-11d", dist[row][col]);
                    printf("\n");
            }      
            printf("\n--------------------------------------------------------------------------------------------------------------------\n\n");
    }


    see u mayu notice i use that same for loop twice i want it in a mehtod soo i can just call it twice instead

  12. #192
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, just copy one of the bits of code into your "places" function.

    As far as I see, there's no need to have a return value.

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

  13. #193
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    hmmmm i have been pasting stuff in and out of the function into the bracers and still the same

  14. #194
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fortune2k View Post
    hmmmm i have been pasting stuff in and out of the function into the bracers and still the same
    1. Post the code you are working on - it's very difficult to know what you are working on without seeing the code.
    2. Describe in more detail what is or isn't working.

    Using these two things, you will get better help quicker. It may seem like you are doing more typing that way, but there are several benefits:
    - when you describe what is going wrong, you may actually find what is wrong yourself - cancel the post and get on with implementing it.
    - you don't have to wait until someone posts a reply to your post just to ask another clarification.
    - you get help with what is wrong, rather than some other answer that doesn't help you much.

    "still the same" isn't a very clear description of what is working and what is not working...

    --
    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. #195
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    1. Post the code you are working on - it's very difficult to know what you are working on without seeing the code.
    2. Describe in more detail what is or isn't working.

    Using these two things, you will get better help quicker. It may seem like you are doing more typing that way, but there are several benefits:
    - when you describe what is going wrong, you may actually find what is wrong yourself - cancel the post and get on with implementing it.
    - you don't have to wait until someone posts a reply to your post just to ask another clarification.
    - you get help with what is wrong, rather than some other answer that doesn't help you much.

    "still the same" isn't a very clear description of what is working and what is not working...

    --
    Mats



    alright i shall do my best to explain well as you can see mmy program above contains this for loop 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("%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;       
                                }
                }

    what it does it loops round twice allowing you to input 2 valid places which are read from a text file and they are :
    Code:
     -     London	Glasgow	Birmingham	Manchester	Luton	Derby	Poole	Stafford Stoke	Cardiff
    if the input place name is equal to 1 inthe place array it sets flag to 1 (if flags set to 1 it means that the place which has been input maches up with the array if it is 0 it has not been found) and point to the value of x ( point gets used alot further into my program for plot points ect ect )
    if the place name s in valid it takes 1 away from i soo it has to loop again out puts a error message and sets wrong to 1 so it doesnt re ask you to put in start or finishing
    point.

    im not the best at explaining how things work but i tryed and there it is

    what i want is to have that loop in a function so i dont have to have the same thing in case 2 and case 3 i wanna have it in a seperate function so i can just call it in case 2 and 3

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