Like Tree1Likes
  • 1 Post By qny

joining codes

This is a discussion on joining codes within the C Programming forums, part of the General Programming Boards category; I need help with completing and some codes together Key aims: 1. The user can select a departure station 2. ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Location
    London, United Kingdom, United Kingdom
    Posts
    6

    joining codes

    I need help with completing and some codes together
    Key aims: 1. The user can select a departure station
    2. The user can select an arrival station
    3. the user can select either single or return journey.

    i need the code to be able to show the result of the arrival and departure station and a way to count the stops so i could add to the calculation.
    Code:
    #include <stdio.h>
    
    void station1()
    {
        printf( "Ashford Station" );
    }
    void station2()
    {
        printf( "Brentworth Station" );
    }
    void station3()
    {
        printf( "Canonbury Cross Station" );
    }
    void station4()
    {
        printf("Dowgate Station");
    }
    void station5()
    {
        printf("Edbury Station");
    }
    void station6()
    {
        printf("Fenchurch Street Station");
    }
    void station7()
    {
        printf("Gresham Station");
    }
    void station8()
    {
        printf("Hampstead Station");
    }
    void station9()
    {
        printf("Islington Station");
    }
    void station10()
    {
        printf("Jamaica Road Station");
    }
    int main()
    {
        int input;
    
        printf( "1. Ashford Station\n" );
        printf( "2. Brentworth Station\n" );
        printf( "3. Canonbury Cross Station\n" );
        printf( "4. Dowgate Station\n");
        printf( "5. Edbury Station\n");
        printf( "6. Fenchurch Street Station\n");
        printf( "7. Gresham Station\n");
        printf( "8. Hampstead Station\n");
        printf( "9. Islington Station\n");
        printf( "10. Jamaica Road Station\n");
        printf( "11. Exit\n" );
        printf( "Arrival Station:" );
        scanf( "%d", &input );
        printf("Depature Station:");
        scanf("%d",&input);
        switch ( input ) {
            case 1:
                station1();
                break;
            case 2:
                station2();
                break;
            case 3:
                station3();
                break;
            case 4:
                station4();
                break;
            case 5:
                station5();
                break;
            case 6:
                station6();
                break;
            case 7:
                station7();
                break;
            case 8:
                station8();
                break;
            case 9:
                station9();
                break;
            case 10:
                station10();
                break;
            case 11:
                printf( "Thanks\n" );
                break;
            default:
                printf( "Invaild input, quitting!\n" );
                break;
        }
        getchar();
    
    }
    I have a price code which i can't complete
    Code:
    #include<stdio.h>
    int main(void)
    {float  ticketcost=2.00f, returncost=0.0f, stops =0.0f, singlecost=0.0f, priceofjourney =0.0f;
    
        {int choice;
        printf("\nEnter Either 1 for Single Journey or 2 for Return Journey:");
        scanf("%d", &choice);
        if (choice==1)
        {
            printf("\nSingle Journey:");
            scanf("%f", &singlecost);
            }
            else if (choice==2)
          {
                printf("\nReturn Journey:");
            scanf("%f", &returncost);
            }
            else
            {
                printf("\nInvalid Option");
            }
        {float  ticketcost=2.00f, returncost=0.0f, stops =0.0f, singlecost=0.0f, priceofjourney =0.0f;
    
    
      singlecost=ticketcost*stops;
      returncost=singlecost * (singlecost/2);
      printf("\nPrice of single journey = %.2f\n\n", singlecost);
      printf("\nprice of return journey = %.2f\n\n", returncost);
    
     getchar();
    
    return 0;}
        }
    }
    I would really appreciate any advice or help.
    thanks

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    In main you are assigning departure station and arrival station to the same variable:
    Code:
    printf( "Arrival Station:" );
    scanf( "%d", &input );
    printf("Depature Station:");
    scanf("%d",&input);
    so you are losing the 'arrival station' value. Once you keep the two values separate you can calculate the number of station by arrival - departure. If it's negative make it positive.

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Please, please, please ... learn to use arrays!

    Code:
    #include <stdio.h>
    
    int main(void) {
        int input, i;
        const char *station[] = {
            "Ashford Station",         "Brentworth Station",
            "Canonbury Cross Station", "Dowgate Station",
            "Edbury Station",          "Fenchurch Street Station",
            "Gresham Station",         "Hampstead Station",
            "Islington Station",       "Jamaica Road Station"
        };
    
        for (i = 0; i < 10; i++) {
            printf("%d. %s\n", i + 1, station[i]);
        }
        printf("11. Exit\n");
        /* code commented out
        ** because it has no effect other than
        ** cluttering the display
        printf("Arrival Station: ");
        scanf("%d", &input);
        */
        printf("Departure Station: ");
        scanf("%d", &input);
        if ((1 <= input) && (input <= 11)) {
            if (input == 11) printf("Thanks\n");
            else printf("%s\n", station[input - 1]);
        } else {
            printf("Invalid input, quitting!\n");
        }
        getchar();
        return 0;
    }
    smk3000 likes this.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    If you use an array to store all those stations and 'for loops' to read from it, it'll make it so much easier to print/read from them

    C++ Tutorial - 10 - Beginning Arrays - YouTube

    once you figure it out it'll make your life much easier and your code a lot better

  5. #5
    Registered User
    Join Date
    Dec 2012
    Location
    London, United Kingdom, United Kingdom
    Posts
    6
    thanks for the help, i'm stuggling to caculate the diffrence between the arrival and departure, any ideas with for that?

  6. #6
    Registered User
    Join Date
    Dec 2012
    Location
    London, United Kingdom, United Kingdom
    Posts
    6
    Quote Originally Posted by nonoob View Post
    In main you are assigning departure station and arrival station to the same variable:
    Code:
    printf( "Arrival Station:" );
    scanf( "%d", &input );
    printf("Depature Station:");
    scanf("%d",&input);
    so you are losing the 'arrival station' value. Once you keep the two values separate you can calculate the number of station by arrival - departure. If it's negative make it positive.
    please how would i calculate that? thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Joining thread before creating it
    By John Erlandsson in forum C Programming
    Replies: 5
    Last Post: 05-16-2011, 07:09 PM
  2. Joining strings.
    By DaveHope in forum C++ Programming
    Replies: 10
    Last Post: 06-21-2005, 08:15 AM
  3. Strings joining
    By Lord CyKill in forum C++ Programming
    Replies: 1
    Last Post: 04-13-2003, 05:02 AM
  4. Joining arrays?
    By C-Struggler in forum C Programming
    Replies: 3
    Last Post: 04-07-2003, 07:55 PM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 03:06 PM

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