Like Tree2Likes
  • 2 Post By dennis.cpp

Help!!!

This is a discussion on Help!!! within the C Programming forums, part of the General Programming Boards category; I need help to calculate the ticket price from the number of stops between arrival and departure station Code: #include ...

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

    Help!!!

    I need help to calculate the ticket price from the number of stops between arrival and departure station
    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");
        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");
        }
        printf("Arrival 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");
        }
        {{float  ticketcost=2.00f, returncost=0.0f,singlecost=0.0f, priceofjourney =0.0f;
        int x,y,s;
      i >=0&& i<=10;
        i>=0 && i<=10;
    
    s = i + i;
    singlecost=ticketcost*s;
      returncost=singlecost * (singlecost/2);
    } }
    {float  ticketcost=2.00f, returncost=0.0f, s =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("\nPrice of single journey = %.2f\n\n", singlecost);
            scanf("%f", &singlecost);
            }
            else if (choice==2)
          {
                printf("\nprice of return journey = %.2f\n\n", returncost);
            scanf("%f", &returncost);
            }
            else
            {
                printf("\nInvalid Option");
            }
    
    
    
    
    
    
        getchar();
        return 0;
    
    }
    }
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    nonoob told you already in the other thread that you need two different variables to store the arrival and departure station.

    And please fix your indentation.

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Dec 2012
    Location
    London, United Kingdom, United Kingdom
    Posts
    6
    Quote Originally Posted by AndiPersti View Post
    nonoob told you already in the other thread that you need two different variables to store the arrival and departure station.

    And please fix your indentation.

    Bye, Andreas
    I'm honestly not sure what I'm doing right or wrong, this is my first time doing programming
    Code:
    #include <stdio.h>
    
    int main(void) {
        int input, i;
        int output, d;
        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");
        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");
        }
        for (d = 0; d < 10; d++) {
            printf ("%d. %s\n", d + 1, station[d]);
        }
        printf("11. Exit\n");
        printf("Arrival Station:");
        scanf("%d",&output);
        if ((1 <= output)&&(output <= 11)) {
             if (output == 11) printf ("Thanks\n");
             else printf("%s\n", station[output - 1]);
             } else {
             printf ("Invalid input, quitting!\n");
             }
         {float  ticketcost=2.00f, returncost=0.0f, s =0.0f, singlecost=0.0f, p=0.0f;
    
        {int choice;
        printf("\nEnter Either 1 for Single Journey or 2 for Return Journey:");
        scanf("%d", &choice);
        if (choice==1)
        { s = i-d;
        singlecost = ticketcost * s;
            printf("\nPrice of single journey = %.2f\n\n", singlecost);
            scanf("%f", &singlecost);
            }
            else if (choice==2)
          {s = i-d;
           p = ticketcost * s;
           returncost = p-(p/2);
                printf("\nprice of return journey = %.2f\n\n", returncost);
            scanf("%f", &returncost);
            }
            else
            {
                printf("\nInvalid Option");
            }
        getchar();
        return 0;
    }
         }
    }

  4. #4
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Your main problem does not seem to be one of the language primarily, but rather one of your algorithm. It does not seem to me that you really have any idea what you want to achieve. Instead you're throwing braces and variables all around your code hoping that somehow your compiler will magically guess what you're aiming at.

    My suggestion would be to turn off your computer for a while, grab a pencil and a paper and write down what your program is supposed to do. Next, write down step by step how *you* would perform this task if there was no computer around. And by 'step by step', I actually *mean* 'step by step', no matter how obvious they seem to your mind.

    If you did that, you can try to translate your steps into the programming language.

    Apart from that, you should really work on your indentation style. The visual structure of your code is nothing but a mess.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

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