Help

This is a discussion on Help within the C Programming forums, part of the General Programming Boards category; Hi I have a question that I could do with some help with. 1. Have got a college assignment to ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    10

    Help

    Hi

    I have a question that I could do with some help with.

    1. Have got a college assignment to poduce a mileage chart.
    I have to read 10 place names and a list of miles from a text file and enter it into a chart.

    I have the following code I can get it to display the place names across the top and down the left hand column but I cannot get it to display the miles it just displays 0's.
    I have tried changing the code and it seems to be looping throught to pick up the names from the text file, but not continuing to loop through and pick up the miles.

    I have attached the code below and also the text file.

    If there is anything obvious I have missed or missed out of the text file please let me know.

    **********Code*****************
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int menu(void);
    void readFile(void);
    void showchart(void);
    
    int grid[10][10];
    
    char names[10][5];
    
    void showchart ()
    {
    int x, row, col;
    
    FILE *in_file;
    in_file = fopen("places.txt","r");
    
    for (x=0;x<=9; x++)
    {
    fscanf(in_file, "%4c", names[x]);
    }
    
    printf("\n     ");
    
    for (x=0; x<=9; x++)
    {
    printf("%6s",names[x]);
    }
    
    printf("\n");
    for (row=0; row<=9; row++)
    {
    printf("%s", names[row]);
    	for(col=0; col<=9; col++)
    {
    	fscanf(in_file, "%d", &grid[row][col]);
    	printf("%6d", grid[row][col]);
    }
    printf("\n");
    }
    fclose(in_file);
    }
    ***************end of code*************

    The text file is as follows


    ***************text file****************

    DealHullMullFordMuckButeIonaYorkWallSarkDeal
    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
    89
    123
    90
    35
    100
    732
    234
    19
    732
    89
    0

    *********** End of Text File************

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    10
    This is what I am after

    Hull Wall War Etc......

    Hull 0 12 35 89

    Wall 12 0 67 12

    War 35 35 0 23

    Etc... 56 43 76 0


    But this is what I am getting

    Hull Wall War Etc......

    Hull 0 0 0 0

    Wall 0 0 0 0

    War 0 0 0 0

    Etc... 0 0 0 0
    Last edited by dgibson2004; 05-08-2006 at 11:42 AM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    where is main

    besides that your first line actually has 11 four character words not 10

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    10
    I cut the main out when posting on this forum to make the code shorter.

    Here is the total code

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int menu(void);
    void cost(void);
    void distance(void);
    void showchart(void);
    
    
    float  price;
    int option, x, y, dist;
    
    int grid[10][10];
    
    char names[10][5];
    
    main()
    {
    
    	int choice;
    
    	choice = menu();
    	
    	while (choice<0 || choice >3)
    	{
    		choice = menu();
    	}
    	
    	while (choice !=0)
    	{
    		switch (choice)
    		{
    			case 1:  distance(); break;
    			case 2:  cost(); break;
    			case 3:  showchart(); break;
    		}
    		choice = menu();
    	}
    	
    	printf("\n***Thank you for using this programme!***\n");
    }
    			
    
    
    int menu()
    {
    
    	int opt;
    
    	printf("\n\n\nDo you want to\n");
    	printf("<1>Calculate the distance between X and Y\n");
    	printf("<2>Calculate the cost between X and Y\n");
            printf("<3>Display the chart\n");
    	printf("<0>Quit\n\n\n");
    
    	printf("Please enter your choice:  ");
    	
    	scanf("%d", &opt);
    
    	printf("\n");
    	
    	return (opt);
    }
    
    void cost()
    {
    	printf("\nLocation 1:  ");
    	scanf("%d", &x);
    
    	while (x < 1 || x >10)
    	{
    		printf("\nPlease enter a valid Location 1:  ");
    		scanf("%d", &x);
    	}
    
    	printf("Location 2:  ");
    	scanf("%d", &y);
    
    	while (y < 1 || y >10)
    	{
    		printf("\nPlease enter a valid Location 2:  ");
    		scanf("%d", &y);
    	}
    
    	price = grid[x][y] * 0.4;
    	
    	printf ("\n");
    	printf ("The price for your journey is: ");
    	printf ("%6.2f", price);
    	printf ("\n");
    
    }
    
    void distance ()
    {
    
    	printf("\nLocation 1:  ");
    	scanf("%d", &x);
    
    	while (x < 1 || x >10)
    	{
    		printf("\nPlease enter a valid Location 1:  ");
    		scanf("%d", &x);
    	}
    
    	printf("Location 2:  ");
    	scanf("%d", &y);
    
    	while (y < 1 || y >10)
    	{
    		printf("\nPlease enter a valid Location 2:  ");
    		scanf("%d", &y);
    	}
    
    	dist = grid[x][y];
    	
    	printf ("\n");
    	printf ("The distance for your journey is: ");
    	printf ("%d", dist);
    	printf (" miles\n");
    }
    
    void showchart ()
    {
    int x, row, col;
    
    FILE *in_file;
    in_file = fopen("places.txt","r");
    
    for (x=0;x<=9; x++)
    {
    fscanf(in_file, "%4c", names[x]);
    }
    
    printf("\n     ");
    
    for (x=0; x<=9; x++)
    {
    printf("%6s",names[x]);
    }
    
    printf("\n");
    for (row=0; row<=9; row++)
    {
    printf("%s", names[row]);
    	for(col=0; col<=9; col++)
    {
    	fscanf(in_file, "%d", &grid[row][col]);
    	printf("%6d", grid[row][col]);
    }
    printf("\n");
    }
    fclose(in_file);
    }

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    10
    I does contain 11 four letter names, I will delete the lastone and give that a go.

    Thanks

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    10
    You were correct, the text file had 11 names instead of 10. Once I deleted the last name the program ran fine.

    Thanks

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    In the function "distance" replace
    Code:
    dist = grid[x][y];
    with
    Code:
    dist = grid[x-1][y-1];
    Because array subscript starts with zero.

    The same thing sould be done in function "cost" for
    "price".

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    10

    Question

    thanks for that.

    I had already resolved that issue all by myself (wow I am clever)

    but seriously thanks for pointing it out.

    Just got to figure how to enter the start location and end destination by typing the place names and then displaying the mileage.

    Any help on this would be appreciated as I have no clue where to start.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    10
    Also anyone know how to tidy the following code up to make it look better, maybe put it under one function etc..... it works as it is but looks a bit untidy.

    The code basically allows the user to input either 0, 1, 2 or 3 stops before the destination.

    I have declared the variables in the top part of the code.

    Code:
    void distance ()
    {
    	int choice;
    
    	choice = calcdis();
    	
    	while (choice<0 || choice >4)
    	{
    		choice = calcdis();
    	}
    	
    	while (choice !=0)
    	{
    		switch (choice)
    		{
    			case 1:  dis0(); break;
    			case 2:  dis1(); break;
    			case 3:  dis2(); break;
    			case 4:  dis3(); break;
                            case 5:  menu(); break;
    		}
    		choice = calcdis();
    	}
    	
    	printf("\n***Thank you for using this programme!***\n");
    }
    
    int calcdis ()
    {
    int option;
    
    	printf("\n\n\nHow many stops do you want?\n");
    	printf("<1>No Stops\n");
    	printf("<2>1  Stops\n");
            printf("<3>2  Stops\n");
    	printf("<4>3  Stops\n");
    	printf("<0>  Main Menu\n\n\n");
    
    	printf("Please enter your choice:  ");
    	
    	scanf("%d", &option);
    
    	printf("\n");
    	
    	return (option);
    }
    void dis0()
    
    {
    
    	printf("\nLocation 1:  ");
    	scanf("%d", &x);
    
    	while (x < 1 || x >10)
    	{
    		printf("\nPlease enter a valid Location 1:  ");
    		scanf("%d", &x);
    	}
    
    	printf("Location 2:  ");
    	scanf("%d", &y);
    
    	while (y < 1 || y >10)
    	{
    		printf("\nPlease enter a valid Location 2:  ");
    		scanf("%d", &y);
    	}
            x = x-1;
            y = y-1;
    	dist = grid[x][y];
    	
    	printf ("\n");
    	printf ("The distance for your journey is: ");
    	printf ("%d", dist);
    	printf (" miles\n");
    }
    
    
    void dis1()
    
    {
    
    	printf("\nLocation 1:  ");
    	scanf("%d", &x);
    
    	while (x < 1 || x >10)
    	{
    		printf("\nPlease enter a valid Location 1:  ");
    		scanf("%d", &x);
    	}
    
    	printf("Stop 1:  ");
    	scanf("%d", &y);
    
    	while (y < 1 || y >10)
    	{
    		printf("\nPlease enter Stop 1:  ");
    		scanf("%d", &y);
    	}
    
    
    	printf("Location 2:  ");
    	scanf("%d", &z);
    
    	while (z < 1 || z >10)
    	{
    		printf("\nPlease enter a valid Location 2:  ");
    		scanf("%d", &z);
    	}
            x = x-1;
            y = y-1;
    	z = z-1;
    
    	dist1 = grid[x][y];
    	dist2 = grid[y][z];
    	dist = dist1 + dist2;
    	printf ("\n");
    	printf ("The distance for your journey is: ");
    	printf ("%d", dist);
    	printf (" miles\n");
    }
    
    void dis2()
    
    {
    
    	printf("\nLocation 1:  ");
    	scanf("%d", &x);
    
    	while (x < 1 || x >10)
    	{
    		printf("\nPlease enter a valid Location 1:  ");
    		scanf("%d", &x);
    	}
    
    	printf("Stop 1:  ");
    	scanf("%d", &y);
    
    	while (y < 1 || y >10)
    	{
    		printf("\nPlease enter Stop 1:  ");
    		scanf("%d", &y);
    	}
    
    	printf("Stop 2:  ");
    	scanf("%d", &z);
    
    	while (z < 1 || z >10)
    	{
    		printf("\nPlease enter Stop 2:  ");
    		scanf("%d", &z);
    	}
    	
    	printf("Location 2:  ");
    	scanf("%d", &xz);
    
    	while (xz < 1 || xz >10)
    	{
    		printf("\nPlease enter a valid Location 2:  ");
    		scanf("%d", &xz);
    	}
            x = x-1;
            y = y-1;
    	z = z-1;
            xz = xz-1;
    
    	dist1 = grid[x][y];
    	dist2 = grid[y][z];
            dist3 = grid[z][xz];
    
    	dist = dist1 + dist2 + dist3;
    
    	printf ("\n");
    	printf ("The distance for your journey is: ");
    	printf ("%d", dist);
    	printf (" miles\n");
    }
    
    
    void dis3()
    
    {
    
    	printf("\nLocation 1:  ");
    	scanf("%d", &x);
    
    	while (x < 1 || x >10)
    	{
    		printf("\nPlease enter a valid Location 1:  ");
    		scanf("%d", &x);
    	}
    
    	printf("Stop 1:  ");
    	scanf("%d", &y);
    
    	while (y < 1 || y >10)
    	{
    		printf("\nPlease enter Stop 1:  ");
    		scanf("%d", &y);
    	}
    
    	printf("Stop 2:  ");
    	scanf("%d", &z);
    
    	while (z < 1 || z >10)
    	{
    		printf("\nPlease enter Stop 2:  ");
    		scanf("%d", &z);
    	}
    	
       	printf("Stop 3:  ");
    	scanf("%d", &xz);
    
    	while (xz < 1 || xz >10)
    	{
    		printf("\nPlease enter Stop 3:  ");
    		scanf("%d", &xz);
    	}
    
    	printf("Location 2:  ");
    	scanf("%d", &xy);
    
    	while (xy < 1 || xy >10)
    	{
    		printf("\nPlease enter a valid Location 2:  ");
    		scanf("%d", &xy);
    	}
            x = x-1;
            y = y-1;
    	z = z-1;
            xz = xz-1;
            xy = xy-1;
    
    	dist0 = grid[x][y];
    	dist1 = grid[y][z];
            dist2 = grid[z][xz];
    	dist3 = grid[xz][xy];
    
    	dist = dist0 + dist1 + dist2 + dist3;
    
    	printf ("\n");
    	printf ("The distance for your journey is: ");
    	printf ("%d", dist);
    	printf (" miles\n");
    }

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Code:
    void distance ()
    {
    	int choice;
    
    	choice = calcdis();
    	
    	while (choice<0 || choice >4)
    	{
    		choice = calcdis();
    	}
    	
    	while (choice !=0)
    	{
    		switch (choice)
    		{
    			case 1:  dis0(); break;
    			case 2:  dis1(); break;
    			case 3:  dis2(); break;
    			case 4:  dis3(); break;
                            case 5:  menu(); break;
    		}
    		choice = calcdis();
    	}
    	
    	printf("\n***Thank you for using this programme!***\n");
    }
    I would rewrite the above as:
    Code:
    void distance()
    {
        int choice;
    
        do
        {
            choice = calcdis();
    
            switch(choice)
            {
                case 0: break;  // Do nothing. do-while will exit for us
                case 1:  dis0(); break;
                case 2:  dis1(); break;
                case 3:  dis2(); break;
                case 4:  dis3(); break;
                default: puts("BZZZZZZZT! Invalid option");
            }
        } while(choice);
    
        printf("\n***Thank you for using this programme!***\n");
    }
    I'm not sure where you got your switch case 5, because it's not part of the menu you show.

    Also, you repeat this code a lot:
    Code:
    	printf("\nLocation 1:  ");
    	scanf("%d", &x);
    
    	while (x < 1 || x >10)
    	{
    		printf("\nPlease enter a valid Location 1:  ");
    		scanf("%d", &x);
    	}
    You might consider making a function like so:
    Code:
    int get_location(char *dispstr)
    {
      int var;
    
      do
      {
        printf("\nPlease enter a valid %s: ", dispstr);
        fflush(stdout);  // Should always flush stdout when not printing a newline
        sscanf("%d", &var);
      } while(var < 1 || var > 10);
    
      return var;
    }
    Then you can just call that for each location like so:
    Code:
    x = get_location("Location 1");
    or
    Code:
    xz = get_location("Stop 1");
    Last edited by itsme86; 05-08-2006 at 03:33 PM.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    10
    Sorry the selection for case 5 just returns to the previous options.

    I will give the code a try it certainely looks neater than the code I have writen.

    what does

    fflush(stdout); do? Not heard of this before.

    Thanks

    Dom

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    Quote Originally Posted by man fflush(FILE *stream)
    DESCRIPTION
    If stream points to an output stream or an update stream in
    which the most recent operation was not input, fflush(
    causes any unwritten data for that stream to be written to
    the file, and the st_ctime and st_mtime fields of the under-
    lying file are marked for update.

    If stream is a null pointer, fflush() performs this flushing
    action on all streams for which the behavior is defined
    above.
    Now, in plain English:
    Lets say you want to write to stdout to give the user some instructions. If you plug that statement in a loop and the last thing that happened was not the user typing, you will need to flush stdout to make sure that the statement is actually written.

    Disclaimer: fflush(stdin) results in undefined behavior.
    Last edited by whiteflags; 05-09-2006 at 02:34 AM.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    10
    Oh I see.

    Thats for the explanation I will give it ago and see what results I get.

Popular pages Recent additions subscribe to a feed

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