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 tabstop Switch requires an integral type to switch on. However: you happen to have some integers hanging ...

  1. #61
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by tabstop View Post
    Switch requires an integral type to switch on. However: you happen to have some integers hanging around -- or at least you did a minute ago. chart1[k][0] (for k from 0 to 9) were your city names, correct? Compare your string against those strings (hey hey it's a loop), and then you can switch on k.
    hmmmm im not here with u on this one, im confussed

  2. #62
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Ignoring switches for the moment: I'm assuming you'll need to take some input (whether from a file or from a user) and match it against your railroad table or whatever that was. You already have the names of the cities stored in your array o' doom that we've been working with. F'rinstance: if your user enters "Cardiff", it's going to be nicer to deal with 3 (since that's Cardiff's row/column -- chart1[0][3] and chart1[3][0] both are "Cardiff") rather than keep "Cardiff" around. Also, that index is going to be what you need -- you won't be able to do chart1["Cardiff"]["York"], but you can do chart1[3][10].

  3. #63
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by tabstop View Post
    Ignoring switches for the moment: I'm assuming you'll need to take some input (whether from a file or from a user) and match it against your railroad table or whatever that was. You already have the names of the cities stored in your array o' doom that we've been working with. F'rinstance: if your user enters "Cardiff", it's going to be nicer to deal with 3 (since that's Cardiff's row/column -- chart1[0][3] and chart1[3][0] both are "Cardiff") rather than keep "Cardiff" around. Also, that index is going to be what you need -- you won't be able to do chart1["Cardiff"]["York"], but you can do chart1[3][10].
    the array of doom is just being used just to print out on screen the way im working out the distance is by using another array which follows
    Code:
        int chart [10][10] = { // Array for the Table and Plot points
       {0,23,12,89,456,123,46,732,345,123},
       {23,0,46,234,123,46,89,234,567,90},
       {12,46,0,767,456,46,234,123,732,35},
       {89,234,767,0,732,32,48,67,98,100},
       {456,123,456,732,0,234,46,89,89,732},
       {123,46,46,32,234,0,123,46,123,234},
       {46,89,234,48,46,123,0,46,89,19},
       {732,234,123,67,89,46,46,0,123,732},
       {345,567,732,98,89,123,89,123,0,78},
       {123,90,35,100,732,234,19,732,78,0}
        };
    ive done this because it seems easier to work with

    i also have 2 ints startpoint and endpoint
    i want it soooo when the user inputs their start point for instance london it will set startpoint to chart[0][0] and if the startpoint is for instance bath startpoint will be set as the value in chart[1][1].... welll alll the time the start point willl be 0 its the end point which will be confussing to work out this is how im tyrying to get it to run in my program:

    Point1 : London = 0
    Point2 : Cardiff = 12
    The total distane is 12miles


    i used to have my program like this which worked well but i cant have it so that im putting in a number which corresponds to the place name. i have to put in the place name thats why i was thinking of a switch statement or a if statement
    Code:
       printf(" 1: London\n 2: Bath\n 3: Cardiff\n 4: Carlisle\n 5: Durham\n 6: Exeter\n 7: Leeds\n 8: Norwich\n 9: Truro\n 0: York\n -------------------\n\n");
    
                printf("\n Please enter your start point: "); // input first point
    	        scanf("%5d", &point1);
    				while(point1 < 1 | point1 >10)  // if the point is  not equal to a plot point  it will ask the user to re input
    				 {
    					printf("\n Invalid point pelase re enter : ");
    					scanf("%5d", &point1);
    				 }
    			
    			printf("\n Please enter your end point: "); // input secound point
    	        scanf("%5d", &point2);
    				while(point2 < 1 | point2 >10) 
    				 {
    					printf("\n Invalid point pelase re enter: "); // if the point is  not equal to a plot point  it will ask the user to re input
    					scanf("%5d", &point2);
    				 }
    				 
    	
    			printf("\n Point1:%5d",chart[point1][point1]);
                        printf("\n Point2:%5d",chart[point1][point2]);
    			printf("\n Total distance: %5d", chart[point1][point1]+chart[point1][point2]); // Adds the 2 points togteher to work out the distance 
    			printf("\n\n\n");

    thankyou

  4. #64
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by fortune2k View Post
    the array of doom is just being used just to print out on screen the way im working out the distance is by using another array which follows
    Code:
        int chart [10][10] = { // Array for the Table and Plot points
       {0,23,12,89,456,123,46,732,345,123},
       {23,0,46,234,123,46,89,234,567,90},
       {12,46,0,767,456,46,234,123,732,35},
       {89,234,767,0,732,32,48,67,98,100},
       {456,123,456,732,0,234,46,89,89,732},
       {123,46,46,32,234,0,123,46,123,234},
       {46,89,234,48,46,123,0,46,89,19},
       {732,234,123,67,89,46,46,0,123,732},
       {345,567,732,98,89,123,89,123,0,78},
       {123,90,35,100,732,234,19,732,78,0}
        };
    splutter... If you've already got this array, then you didn't need the array of doom, just an additional array of the city names.

    Anyway.

    ive done this because it seems easier to work with

    i also have 2 ints startpoint and endpoint
    i want it soooo when the user inputs their start point for instance london it will set startpoint to chart[0][0] and if the startpoint is for instance bath startpoint will be set as the value in chart[1][1].... welll alll the time the start point willl be 0 its the end point which will be confussing to work out this is how im tyrying to get it to run in my program:

    Point1 : London = 0
    Point2 : Cardiff = 12
    The total distane is 12miles


    i used to have my program like this which worked well but i cant have it so that im putting in a number which corresponds to the place name. i have to put in the place name thats why i was thinking of a switch statement or a if statement
    Code:
       printf(" 1: London\n 2: Bath\n 3: Cardiff\n 4: Carlisle\n 5: Durham\n 6: Exeter\n 7: Leeds\n 8: Norwich\n 9: Truro\n 0: York\n -------------------\n\n");
    
                printf("\n Please enter your start point: "); // input first point
    	        scanf("%5d", &point1);
    				while(point1 < 1 | point1 >10)  // if the point is  not equal to a plot point  it will ask the user to re input
    				 {
    					printf("\n Invalid point pelase re enter : ");
    					scanf("%5d", &point1);
    				 }
    			
    			printf("\n Please enter your end point: "); // input secound point
    	        scanf("%5d", &point2);
    				while(point2 < 1 | point2 >10) 
    				 {
    					printf("\n Invalid point pelase re enter: "); // if the point is  not equal to a plot point  it will ask the user to re input
    					scanf("%5d", &point2);
    				 }
    				 
    	
    			printf("\n Point1:%5d",chart[point1][point1]);
                        printf("\n Point2:%5d",chart[point1][point2]);
    			printf("\n Total distance: %5d", chart[point1][point1]+chart[point1][point2]); // Adds the 2 points togteher to work out the distance 
    			printf("\n\n\n");

    thankyou
    So the last 98.7% of the work is done: all you have to do is to turn the input into the index point2. You have all the names of the cities, conveniently numbered, in your array. Compare the input to the city names in turn until you find a match (via strcmp). When you find a match, that's your point2 (actually it's point2+1, since your cities start at 1).

  5. #65
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by tabstop View Post
    splutter... If you've already got this array, then you didn't need the array of doom, just an additional array of the city names.

    Anyway.



    So the last 98.7% of the work is done: all you have to do is to turn the input into the index point2. You have all the names of the cities, conveniently numbered, in your array. Compare the input to the city names in turn until you find a match (via strcmp). When you find a match, that's your point2 (actually it's point2+1, since your cities start at 1).

    do you know of any tutorials or reading material on "strcmp" i have never came accross it

  6. #66
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Plenty of documentation out there.
    Basically, strmp takes two strings - the two to compare. IF the strings equal each other, it returns 0. Otherwise it can return < 0 and > 0 depending on if the one string "is bigger" or "smaller" than the other, but that's rarely helpful unless you're doing a sorting algorithm.
    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.

  7. #67
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by Elysia View Post
    Plenty of documentation out there.
    Basically, strmp takes two strings - the two to compare. IF the strings equal each other, it returns 0. Otherwise it can return < 0 and > 0 depending on if the one string "is bigger" or "smaller" than the other, but that's rarely helpful unless you're doing a sorting algorithm.
    i need to compare 10 strings set some values and print out on screen how can i do this using strcmp

    London
    Bath
    Cardiff
    Carlisle
    Durham
    Exeter
    Leeds
    Norwich
    Truro
    York

    i want it soo the user can input anyone of this is how im trying to get it to run

    Strarting Point: // asks user
    London // User inputs london
    point1= 1 // point 1 is set to the value of chart 0 0

    End point; // asks user
    Leeds // user inputs leeds
    point2=7 // point 2 is set to 7


    kinda like that if the user was to input somthing like apple it will throw up an error and ask the user to re input the string
    Last edited by fortune2k; 03-09-2008 at 08:57 AM.

  8. #68
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    right i have been working on my program and i have managed to get it to work by putting in the place names now i have a new problem i need the program to firstly ask where your startiong where your finishing then ask will you be making any intermidiate stops 0-3 i have been trying that but my porgram keeps crashing and stuff wonder if you guys know why this is what i have
    Code:
     printf(" 1: London\n 2: Bath\n 3: Cardiff\n 4: Carlisle\n 5: Durham\n 6: Exeter\n 7: Leeds\n 8: Norwich\n 9: Truno\n 0: York\n -------------------\n\n");	   
                for(i=0;i<2;i++)
                {
                   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         
                   scanf("&#37;s", &pos[i]);
    	             if( strcmp( pos[i], "London" ) == 0 ){point[i]=0;}
                 else if( strcmp( pos[i], "Bath" ) == 0 ){point[i]=1;}
                 else if( strcmp( pos[i], "Cardiff" ) == 0 ){point[i]=2;}
                 else if( strcmp( pos[i], "Carlisle" ) == 0 ){point[i]=3;}
                 else if( strcmp( pos[i], "Durham" ) == 0 ){point[i]=4;}
                 else if( strcmp( pos[i], "Exeter" ) == 0 ){point[i]=5;}
                 else if( strcmp( pos[i], "Leeds" ) == 0 ){point[i]=6;}
                 else if( strcmp( pos[i], "Norwich" ) == 0 ){point[i]=7;}
                 else if( strcmp( pos[i], "Truno" ) == 0 ){point[i]=8;}
                 else if( strcmp( pos[i], "York" ) == 0 ){point[i]=9;}
                 else{printf("\n Invalid place please Re enter: ");i--;}
                 }
                
                  printf("\n Please Enter the number of stops 0-3: ");
                  scanf("%5d",&numstops);
                  if(numstops>0)
                   {
                      for(i=0;i<numstops;i++)
                      {
                       printf("\n Please enter the name for Stop %-1d: ",i+1);
                       scanf("%16s",&stopname[i]);
                       if( strcmp( stopname[i], "London" ) == 0 ){stops[i]=0;}
                       else if( strcmp( stopname[i], "Bath" ) == 0 ){stops[i]=1;}
                       else if( strcmp( stopname[i], "Cardiff" ) == 0 ){stops[i]=2;}
                       else if( strcmp( stopname[i], "Carlisle" ) == 0 ){stops[i]=3;}
                       else if( strcmp( stopname[i], "Durham" ) == 0 ){stops[i]=4;}
                       else if( strcmp( stopname[i], "Exeter" ) == 0 ){stops[i]=5;}
                       else if( strcmp( stopname[i], "Leeds" ) == 0 ){stops[i]=6;}
                       else if( strcmp( stopname[i], "Norwich" ) == 0 ){stops[i]=7;}
                       else if( strcmp( stopname[i], "Truno" ) == 0 ){stops[i]=8;}
                       else if( strcmp( stopname[i], "York" ) == 0 ){stops[i]=9;}
                       else if(chart[stops[i]][stops[i]]<chart[point[0]][point[0]] | chart[stops[i]][point[1]] > chart[point[0]][point[1]])
                         {
                            printf("\n Invalid place please Re enter: ");
                            i--;                                                 
                         }
                       else{printf("\n Invalid place please Re enter: ");i--;}
                      } 
                     
                      for(i=0;i<numstops;i++)
                      {
                      printf("\n %-16s: %-5d miles",stops[i],chart[point[0]][stops[2]]);
                      }
      
                      printf("\n %-16s: %-5d miles",pos[1],chart[point[0]][point[1]]);
                      printf("\n Total distance  : %-5d miles", chart[point[0]][point[0]]+chart[point[0]][point[1]]);        
                   }  
                   
                   else if(numstops==0)
                   {
                     printf("\n %-16s: %-5d miles",pos[0], chart[point[0]][point[0]]);
                     printf("\n %-16s: %-5d miles",pos[1],chart[point[0]][point[1]]);
    			     printf("\n Total distance  : %-5d miles", chart[point[0]][point[0]]+chart[point[0]][point[1]]); // Adds the 2 points togteher to work out the distance 
                    }
    please excuse my poor formating ill make it pretty at the end when its done
    Last edited by fortune2k; 03-15-2008 at 09:36 AM.

  9. #69
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Code:
                   scanf("%s", &pos[i]);
    Important:
    Need help with altering info on a txt file
    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. #70
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    also note that | is binary or not logical or(||)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #71
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    im confuzzed now

  12. #72
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    While extremely unreadable,
    Code:
    else if(chart[stops[i]][stops[i]]<chart[point[0]][point[0]] | chart[stops[i]][point[1]] > chart[point[0]][point[1]])
    Should be:
    Code:
    else if(chart[stops[i]][stops[i]]<chart[point[0]][point[0]] || chart[stops[i]][point[1]] > chart[point[0]][point[1]])
    A simple | is not what you want. || is the logical or operator, which you want.
    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.

  13. #73
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    with this code:
    Code:
     printf(" 1: London\n 2: Bath\n 3: Cardiff\n 4: Carlisle\n 5: Durham\n 6: Exeter\n 7: Leeds\n 8: Norwich\n 9: Truno\n 0: York\n -------------------\n\n");	   
                for(i=0;i<2;i++)
                {
                   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         
                   scanf("&#37;s", &pos[i]);
    	             if( strcmp( pos[i], "London" ) == 0   ){point[i]=0;}
                 else if( strcmp( pos[i], "Bath" ) == 0        ){point[i]=1;}
                 else if( strcmp( pos[i], "Cardiff" ) == 0     ){point[i]=2;}
                 else if( strcmp( pos[i], "Carlisle" ) == 0    ){point[i]=3;}
                 else if( strcmp( pos[i], "Durham" ) == 0   ){point[i]=4;}
                 else if( strcmp( pos[i], "Exeter" ) == 0     ){point[i]=5;}
                 else if( strcmp( pos[i], "Leeds" ) == 0      ){point[i]=6;}
                 else if( strcmp( pos[i], "Norwich" ) == 0   ){point[i]=7;}
                 else if( strcmp( pos[i], "Truno" ) == 0       ){point[i]=8;}
                 else if( strcmp( pos[i], "York" ) == 0         ){point[i]=9;}
                 else{printf("Invalid place please Re enter");i--;}
                 }
          
                 printf("\n %-16s: %-5d miles",pos[0], chart[point[0]][point[0]]);
                 printf("\n %-16s: %-5d miles",pos[1],chart[point[0]][point[1]]);
    	     printf("\n Total distance  : %-5d miles", chart[point[0]][point[0]]+chart[point[0]][point[1]]); // Adds the 2 points togteher to work out the distance
    im getting funny outputs for the place names
    http://i57.photobucket.com/albums/g2...e2k/lobath.jpg
    LoBath lol its ment to be London anyone know why

  14. #74
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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.

  15. #75
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by Elysia View Post
    right i have fixxed that prob but when i put London as Start and Bath as finish

    i get LoBath and Bath as outputs for place names as you can see in the picture

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, 06: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, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21