Hi I've just started to learn c programming at college and I'm having a bit of difficulty in getting my head aroung character arrays.In the program I'm writing I need to be able to display a grid of 11 *11. This is made up with city names and distances. I read these from a text file which is ok but its displaying them as a grid, which is causing me the headache. Any help would be great. Thanks
Dave
heres my code:
#include <stdio.h>
#include <string.h>
void print_header(); /* prototypes */
void display(char [1000]);
int main(void)
{
char option,rtn;
int nums[10][10];
char vals[11][11];
int i,j;
FILE *f;
char s[1000];
f=fopen("matrix.dat","r");
if (!f)
return 1;
while (fgets(s,1000,f)!=NULL)
printf("%s\t",s);
fclose(f);
printf("\nData read from file matrix.dat\n\n");
print_header();
scanf("%c%c",&option,&rtn);
while(option != 'q' && option != 'Q')
{ switch(option)
{
case 'd': /*display */
display(s);
break;
case 'j': /* journey */
printf("\nEnter the first destination ");
scanf("%d",&vals[i]);
printf("\nEnter the second destination ");
scanf("%d",&vals[j]);
printf("\nThe Total Miles:= ");
printf("%d",vals[i][j]);
scanf("%c",&rtn);
break;
case 'c': /* Cost of journey */
printf("\nThe Total cost of the journey is:£");
printf("%3.2f",vals[j][i]*0.4);
break;
default: /* invalid choice */
printf("Invalid choice: please try again");
}
print_header();
scanf("%c%c",&option,&rtn);
}
return 0;
}
void print_header()
{
printf("\n\nDisplay d");
printf("\n\nJourney j");
printf("\n\nCostofJourney c");
printf("\n\nExit q");
printf("\n\nENTER YOUR CHOICE:");
}
void display(char s[1000])
{
printf("\nThe destinations are\n\n");
printf("%s\t",s);
printf("\n");
}
#include <stdio.h>
#include <string.h>
void print_header(); /* prototypes */
void display(char [1000]);
int main(void)
{
char option,rtn;
int nums[10][10];
char vals[11][11];
int i,j;
FILE *f;
char s[1000];
f=fopen("matrix.dat","r");
if (!f)
return 1;
while (fgets(s,1000,f)!=NULL)
printf("%s\t",s);
fclose(f);
printf("\nData read from file matrix.dat\n\n");
print_header();
scanf("%c%c",&option,&rtn);
while(option != 'q' && option != 'Q')
{ switch(option)
{
case 'd': /*display */
display(s);
break;
case 'j': /* journey */
printf("\nEnter the first destination ");
scanf("%d",&vals[i]);
printf("\nEnter the second destination ");
scanf("%d",&vals[j]);
printf("\nThe Total Miles:= ");
printf("%d",vals[i][j]);
scanf("%c",&rtn);
break;
case 'c': /* Cost of journey */
printf("\nThe Total cost of the journey is:£");
printf("%3.2f",vals[j][i]*0.4);
break;
default: /* invalid choice */
printf("Invalid choice: please try again");
}
print_header();
scanf("%c%c",&option,&rtn);
}
return 0;
}
void print_header()
{
printf("\n\nDisplay d");
printf("\n\nJourney j");
printf("\n\nCostofJourney c");
printf("\n\nExit q");
printf("\n\nENTER YOUR CHOICE:");
}
void display(char s[1000])
{
printf("\nThe destinations are\n\n");
printf("%s\t",s);
printf("\n");
}