Thread: help with character arrays

  1. #1
    Unregistered
    Guest

    Question help with character arrays

    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");
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Please use code tags.
    2) Please show us what the expected output is. Saying you want a grid does no good. (On that note, if you're planning on using a grid with 11 columns, they can have no more than 6 characters per column due to limitations of screen width in a console window.)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    2
    Sorry I should have explained things a little better than I did. The program is to calculate the distances between two cities e.g Leeds to Liverpool. There are 10 cities, and would look something like this:

    ********Leeds London Liverpool etc

    Leeds *****0 *****150******70

    London ***150******0******180

    Liverpool **70******180******0


    All the Cities and distances are repeated so if some kind sole could explain to me how to do this that would be great.
    Last edited by dazzer; 03-28-2002 at 04:12 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll want to do something like:
    Code:
    #define TOTAL_CITIES 10
    /*put the indentation */
    printf("%6s", "" );
    /* put the top row of names */
    for( x = 0; x < TOTAL_CITIES; x++ )
        printf("%6s", citynames[x] );
    /* print the city names and data */
    for( x = 0; x < TOTAL_CITIES; x++ )
    {
        printf("\n%6s ", citynames[x] );
        for( y = 0; y < TOTAL_CITIES; y++ )
        {
            printf("%6s ", citydata[y] );
        }
    }
    If you're using a multidimensional array, you should be able to modify this code to suit your needs. I'll not do all of your homework for you.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    Smile

    Quzah thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  4. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 07:54 PM
  5. Spaces in Character Arrays
    By ADLOTS in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2002, 04:24 AM