ive been trying for hours and im still gettting the odd out put of http://i57.photobucket.com/albums/g2...e2k/lobath.jpg
ive been trying for hours and im still gettting the odd out put of http://i57.photobucket.com/albums/g2...e2k/lobath.jpg
pos is declared like this
when i put a print statement in the loop it works fyne however when i try to print out pos[0] and pos[1] i get messed up result half of 0 and 1 in 0Code:char pos[16][2];
can anyone see why this is happeningCode: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("%16s", &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--;} //*************IF I PUT PRINT STATEMENT HERE IT WORKS********** } //i need alll the info to print out at the end here 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 printf("\n\n\n"); main(); // calls the menu method once finshed break;
http://i57.photobucket.com/albums/g2...e2k/lobath.jpg
Because you're putting both of them into the same array. You might want to use &pos[0][i] as the actual (correct) argument to scanf, and start printing at &pos[0][i] as well.
Or you could do the define correctly, as pos[2][16].
i have got that problem again and well i cant spot it maybe someone here can spot it ive been looking for ages;
pos is declared asCode: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{printf("\n Invalid place please Re enter: ");i--;} } if(numstops==1) { printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],chart[point[0]][stops[0]]); printf("\n From %s To %s is %d miles",stopname[0],pos[1],chart[point[1]][stops[0]]); printf("\n Total Distance : %-5d",chart[point[0]][stops[0]]+chart[point[1]][stops[0]]); } else if(numstops==2) { printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],chart[point[0]][stops[0]]); printf("\n From %s To %s is %d miles",stopname[0],stopname[1],chart[stops[0]][stops[1]]); printf("\n From %s To %s is %d miles",stopname[1],pos[1],chart[point[1]][stops[1]]); printf("\n Total Distance : %-5d",chart[point[0]][stops[0]]+chart[stops[0]][stops[1]]+chart[point[1]][stops[1]]); } else if(numstops==3) { printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],chart[point[0]][stops[0]]); printf("\n From %s To %s is %d miles",stopname[0],stopname[1],chart[stops[0]][stops[1]]); printf("\n From %s To %s is %d miles",stopname[1],stopname[2],chart[point[0]][stops[2]]); printf("\n From %s To %s is %d miles",stopname[2],pos[1],chart[point[1]][stops[2]]); printf("\n Total Distance : %-5d",chart[point[0]][stops[0]]+chart[point[0]][stops[1]]+chart[point[0]][stops[2]]+chart[point[1]][stops[2]]); } }Code:char pos[1][16]; char stopname[2][16];
its when someone puts in for 3 stops i get this output
i put in london as my starting place but on the output for 3 stops i get a diff place name when i call upon it
http://i57.photobucket.com/albums/g2...k/Untitled.jpg
thankyou
With that declaration, you only get two elements in stopname. stopname[0] and stopname[1]. Don't try to access stopname[2] or you'll run into trouble.Code:char stopname[2][16];
Remember, an array like this
has N elements. But because [0] is the first element, [N-1] is the last element. [N] is out of bounds.Code:type arrayname[N];
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Should not have the &.Code:scanf("%16s",&stopname[i]);
I don't think that's the real problem tho'.
If Truno is Truro, then I think the distance is also quite wrong - Truro to Bath should be 345 miles. It looks like you get the Cardiff to Bath distance instead.
By the way, why not have a table of the city names and use a function to translate a city name to a number?
--
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.
yea the spelling is messed and well im really new to C and just trying to do the rquirements how would you go about using a function
i still need to get a few more elements out of the program done like reading in the table from a text file and storing in a string array then i can tone down the code and add fancy bits to it......
i think i will have the program done in a day or 2 and well then ill post it and the requirements and ill see what everyone thinks and see how i can make it better
Last edited by fortune2k; 03-19-2008 at 06:54 PM.
right i have got my program working all it needs to do is import this data and store in a string array
Code:London Bath Cardiff Carlisle Durham Exeter Leeds Norwich Truro York London - 23 12 89 456 123 46 732 345 123 Bath 23 - 46 234 123 46 89 234 567 90 Cardiff 12 46 - 767 456 46 234 123 732 35 Carlisle 89 234 767 - 732 32 48 67 98 100 Durham 456 123 456 732 - 234 46 89 89 732 Exeter 123 46 46 32 234 - 123 46 123 234 Leeds 46 89 234 48 46 123 - 46 89 19 Norwich 732 234 123 67 89 46 46 - 123 732 Truro 345 567 732 98 89 123 89 123 - 78 York 123 90 35 100 732 234 19 732 78 -
right these are the requirements for my program:
Initialise: Reads items from data file and stores in appropriate data structures. 10 place names and distances between them.
Menu: Displays options and validates selection with appropriate error messaging
Option selection calls correct function and exit functions correctly
DispChart: Displays chart showing distances between all points available
CalcDistance: Enables input of two points and up to 3 intermediate points and returns total distance and distance of each leg of the journey
Calls PlaceSearch function and prompts for re-entry on detection of invalid place name
PlaceSearch: Detects valid and invalid place names
Totalcost: Returns the cost of a journey between two points @ 40ppm (<=100) and 30ppm (>100)
Test Plan: Black box test identification
Test plan showing data and BB tests implemented.
Design: Abstract solution using pseudocode to represent functionality
Robustness and Data Validation: General attention to validation , formatting, clear screen and layout of screen display ,
Overall Impression: To include the use of appropriate data types, operators and control structures (selection, repetition), the structure of the code, the use of separate functions, the use of parameters and pointers where appropriate and the scope of successful implementation
and well after a few weeks of doin bits to it i still havent got it to import from text file and i feel like i have too much if statements well have a look and fiddle and give your advice on how i can improve ... thankyou
Code:// C Programming Assingment #include <stdio.h> #include <stdlib.h> int strcmp( const char *str1, const char *str2 ); int main (void) { int choice, row, col, i, numstops; int stops[2], point[2]; char pos[2][16]; char stopname[3][16]; char chart1[11][11][11] ={ // Array for the Table and Plot points "-", "London", "Bath", "Cardiff", "Carlisle", "Durham", "Exeter", "Leeds", "Norwich", "Truno", "York", "London", "-", "23", "12", "89", "456", "123", "46", "732", "345", "123", "Bath", "23", "-", "46", "234", "123", "46", "89", "234", "567", "90", "Cardiff", "12", "46", "-", "767", "456", "46", "234", "123", "732", "35", "Carlisle", "89", "234", "767", "-", "732", "32", "48", "67", "98", "100", "Durham", "456", "123", "456", "732", "-", "234", "46", "89", "89", "732", "Exeter", "123", "46", "46", "32", "234", "-", "123", "46", "123","234", "Leeds", "46", "89", "234", "48", "46", "123", "-", "46", "89", "19", "Norwich", "732", "234", "123", "67", "89", "46", "46", "-", "123", "732", "Truro", "345", "567", "732", "98", "89", "123", "89", "123", "-", "78", "York", "123", "90", "35", "100", "732", "234", "19", "732", "78", "-"}; 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} }; // menu printf("\n ============================================"); printf("\n M E N U "); printf("\n ================++++++++++++================"); printf("\n 1. Display a complete mileage chart "); 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", &choice); printf("\n\n"); for(i=0;i<1;i++) { if(choice < 1 | choice > 4) // if the menu choice is invalid the program will ask the user to re input it. { printf("\nInvalid choice please re enter: "); scanf("%d", &choice); i--; } } switch (choice) // using a switch statement for the menu choice { case 1: printf("\n The first row and first column are plot points"); printf("\n The mileage chart: \n\n"); for(row=0; row<11;row++) // outputs the array in 2 loops { printf("\n\n"); for(col=0;col<11;col++) { printf("%-10s",chart1[row][col]); } } printf("\n\n\n\n\n"); main();// calls the main method once finshed break; case 2: 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"); 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("%16s", 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], "Truro" ) == 0 ){point[i]=8;} else if( strcmp( pos[i], "York" ) == 0 ){point[i]=9;} else{printf(" 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], "Truro" ) == 0 ){stops[i]=8;} else if( strcmp( stopname[i], "York" ) == 0 ){stops[i]=9;} else{printf("\n Invalid place please Re enter: ");i--;} } if(numstops==1) { printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],chart[point[0]][stops[0]]); printf("\n From %s To %s is %d miles",stopname[0],pos[1],chart[point[1]][stops[0]]); printf("\n Total Distance : %-5d",chart[point[0]][stops[0]]+chart[point[1]][stops[0]]); } else if(numstops==2) { printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],chart[point[0]][stops[0]]); printf("\n From %s To %s is %d miles",stopname[0],stopname[1],chart[stops[0]][stops[1]]); printf("\n From %s To %s is %d miles",stopname[1],pos[1],chart[point[1]][stops[1]]); printf("\n Total Distance : %-5d",chart[point[0]][stops[0]]+chart[stops[0]][stops[1]]+chart[point[1]][stops[1]]); } else if(numstops==3) { printf("\n Starting from %s To %s is %d miles",pos[0],stopname[0],chart[point[0]][stops[0]]); printf("\n From %s To %s is %d miles",stopname[0],stopname[1],chart[stops[0]][stops[1]]); printf("\n From %s To %s is %d miles",stopname[1],stopname[2],chart[stops[1]][stops[2]]); printf("\n From %s To %s is %d miles",stopname[2],pos[1],chart[point[1]][stops[2]]); printf("\n Total Distance : %-5d",chart[point[0]][stops[0]]+chart[stops[0]][stops[1]]+chart[stops[1]][stops[2]+chart[point[1]][stops[2]]); } } else if(numstops==0) { printf("\n From %s To %s is %d miles",pos[0],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 } printf("\n\n\n"); main(); // calls the menu method once finshed break; case 3: 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"); 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], "Truro" ) == 0 ){point[i]=8;} else if( strcmp( pos[i], "York" ) == 0 ){point[i]=9;} else{printf("\n Invalid place please Re enter: ");i--;} } if(chart[point[0]][point[0]]+chart[point[0]][point[1]] <=100) { printf("\n Your Journey is less than 100 miles, Price per mile = 0.40"); printf("\n The price of your journey is : %5lf", chart[point[0]][point[0]]+chart[point[0]][point[1]]*0.4); // Adds the 2 points togteher and multiplys by 0.4 to get the ppm printf("\n\n\n"); } else if(chart[point[0]][point[0]]+chart[point[0]][point[1]] >100) { printf("\n Your Journey is over 100 miles, Price per mile = 0.30"); printf("\n The price of your journey is : %5lf", chart[point[0]][point[0]]+chart[point[0]][point[1]]*0.3); // Adds the 2 points togteher and multiplys by 0.4 to get the ppm printf("\n\n\n"); } main(); // calls the menu method once finshed break; case 4: printf("\n Thankyou and goodbye"); //exits the program break; } }
What DOES it do?i still havent got it to import from text file
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
what the program its for a school assignment it works out distances between places the distances are not correct buttt the program needs to have
that array^^^^ in a text file and then importated and stored in a string array but well the program needs loads more work done on itCode:char chart1[11][11][11] ={ // Array for the Table and Plot points "-", "London", "Bath", "Cardiff", "Carlisle", "Durham", "Exeter", "Leeds", "Norwich", "Truno", "York", "London", "-", "23", "12", "89", "456", "123", "46", "732", "345", "123", "Bath", "23", "-", "46", "234", "123", "46", "89", "234", "567", "90", "Cardiff", "12", "46", "-", "767", "456", "46", "234", "123", "732", "35", "Carlisle", "89", "234", "767", "-", "732", "32", "48", "67", "98", "100", "Durham", "456", "123", "456", "732", "-", "234", "46", "89", "89", "732", "Exeter", "123", "46", "46", "32", "234", "-", "123", "46", "123","234", "Leeds", "46", "89", "234", "48", "46", "123", "-", "46", "89", "19", "Norwich", "732", "234", "123", "67", "89", "46", "46", "-", "123", "732", "Truro", "345", "567", "732", "98", "89", "123", "89", "123", "-", "78", "York", "123", "90", "35", "100", "732", "234", "19", "732", "78", "-"};
I take it those "distances" are not actually the true distances, just typed in "random" numbers - sorry, I thought they actually were "correct" distances.
--
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.
hahah naaa there not im just stuck on how to get the data from text file into the arrayany suggestions on how this can be done....... then i have whats needed for the requirements then i can improve on the program
i have this : data.txt
and well i can read this data and store in a string fyne usingCode:- London Bath Cardiff Carlisle Durham Exeter Leeds Norwich Truro York London 0 23 12 89 456 123 46 732 345 123 Bath 23 0 46 234 123 46 89 234 567 90 Cardiff 12 46 0 767 456 46 234 123 732 35 Carlisle 89 234 767 0 732 32 48 67 98 100 Durham 456 123 456 732 0 234 46 89 89 732 Exeter 123 46 46 32 234 0 123 46 123 234 Leeds 46 89 234 48 46 123 0 46 89 19 Norwich 732 234 123 67 89 46 46 0 123 732 Truro 345 567 732 98 89 123 89 123 0 78 York 123 90 35 100 732 234 19 732 78 0
and well i want to read just the numbers into an int array so i can work with them sooooo the array once the data has gone into the array it will be like this soo [0][1] when printed out will print out 23Code:char chart1[11][11][16]; FILE *infile; infile = fopen("data.txt", "r"); // using relative path name of file if (infile == NULL) { printf("Unable to open file."); } for(row=0; row<11;row++) // reads in the data into the array in 2 loops { for(col=0;col<11;col++) { fscanf(infile,"%s", chart1[row][col]); } }
sooo i can then work with these numbers can anyone show me how i can do thisCode: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