Stuck and need help.

Discussion in 'C++' started by tvred, Mar 22, 2010.

  1. tvred

    tvred New Member

    Joined:
    Mar 22, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    I need to write this code and am lost at how to call these functions to output what I need. The task is to calculate and print the total cost of a round trip ticket for each line in the data file.

    The input file (airline.txt) has the following layout:

    Price (the cost of the ticket for that particular trip, it should be doubled if it is one way)
    Airport tax (the tax for whole trip, both round trip and one way pay that tax)
    Sales tax (the percentage tax on the total price of the ticket, including the airport tax, such as 6%, 10%, …etc.)
    Airline name (SP, NW, SW, AA)
    Origin City (can be one or two words)
    One-way/roundtrip (1 means one way, 2 means round trip)
    Destination City (can be one or two words)

    These are examples of some trips:

    99.99 10.00 0.06 SP Detroit 1 Palm Beach
    143.95 13.00 0.06 NW Detroit 1 Palm Beach
    99.00 10.00 0.10 SP Las Vegas 2 Detroit

    You need to write a program to read the file one record at a time, then print the itinerary for that trip and calculate the ticket price as a round trip ticket (even if it was given as a one-way trip in the file.) The program will read the code letters for the name of the airline from the input and then expand them to the full name in the output. For example, NW, will become Northwest Airline, and SP will print Spirit Airline. The program also will read the name of the city from the input, then it will abbreviate it. If the city is one word, then the abbreviation will be the first and last letters of the word. Both letters should be capitalized. If the city is two words, then the abbreviation will be the first letter of each word.

    A sample output for 99.99 10.00 0.06 SP Detroit 1 Palm Beach is:

    The round trip price from DT to PB using Spirit Airline is $222.58

    Code:
     #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    enum airlineType {SP, NW, SW, AA};
    
    airlineType convertString(string str)
    {
    switch(toupper(str.at(0)))
    {
    case 'SP': return Spirit Airlines;
    case 'NW': return Northwest Airlines;
    case 'SW': return Southwest Airlines;
    case 'AA': return American Airlines;
    }
    return NONE;
    }
    
    void display(airlineType air)
    {
    switch(air)
    {
    case SP: cout << "Spirit Airlines " << endl; break;
    case NW: cout << "Northwest Airlines " << endl; break;
    case SW: cout << "Southwest Airlines " << endl; break;
    case AA: cout << "American Airlines " << endl; break;
    
    default: cout << "Invalid Airline " << endl; break;
    }
    }
    
    void getCity(string line, string& city)
    {
    string::size_type pos;
    pos = line.find(" "); //find the first space
    city = line.substr(0, pos); //will give me first part of the city name
    if (line.at(pos + 1) >= '0' && line.at(pos + 1) <= '9') //will check the letter after the space...letter or number
    cout << "My city is " << city << endl;
    else
    {
    pos = line.find(" ", pos + 1); //find the second space in the city name
    city = line.substr(0, pos); //gets both words
    cout << "My city is " << city << endl;
    }
    }
    tripType roundTrip()
    {
    int x;
    cout << "Please enter [1] for one-way or [2] for round trip ";
    cin >> x;
    return static_cast<tripType>(x);
    }
    
    int main()
    {
    ifstream fin;
    int price = 0;
    int salesTax = 0;
    string line = "", city = "";
    bool upper = false;
    fin.open("myinput.txt");
    
    city = getCity(fin);
    cout << "You have entered the city " << city;
    
    
    return 0;
    }
    
     
  2. bluecoder

    bluecoder New Member

    Joined:
    Mar 11, 2010
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    I think you have to check your switch case statements .
     
  3. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    I haven't look at your code.But yeah at first glance you need to check your switch statement you need to add break;..What's happening rite now is a fall through and all functions are being called
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice