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
ive done this because it seems easier to work withCode: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} };
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
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).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
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.
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
please excuse my poor formating ill make it pretty at the end when its doneCode: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("%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 }
Last edited by fortune2k; 03-15-2008 at 09:36 AM.
Important:Code:scanf("%s", &pos[i]);
http://cboard.cprogramming.com/showp...37&postcount=9
also note that | is binary or not logical or(||)
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
im confuzzed now
While extremely unreadable,
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.Code:else if(chart[stops[i]][stops[i]]<chart[point[0]][point[0]] || chart[stops[i]][point[1]] > chart[point[0]][point[1]])
with this code:
im getting funny outputs for the place namesCode: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("%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
http://i57.photobucket.com/albums/g2...e2k/lobath.jpg
LoBath lol its ment to be London anyone know why
You still haven't fixed this:
http://cboard.cprogramming.com/showp...37&postcount=9