Stuck and need assistance please...

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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;
}
In regards to making it work as a class, you can have your functions in a class, and so they will have access to the data inside the class, meaning you may not need to have any parameters in functions. Also did not notice if you do anything after you open the file. If you did not include how you read in that is fine, as long as you can parse it.
First of all, thank you for replying.

I have to admit, this programming thing is completely new to me and I am way at a loss as to what is going on, or how to make it work. I have read the text book three times over and still can't figure this problem out.

Maybe programming isn't my thing, but I don't want to give up just yet.

Here is a quick way to write a class/struct (structs are classes with all members public by default.)

It seems like for this assignment you don't need any constructors that aren't automatically generated. But I would need to know all of the relevant data members for sure. Here is a quick example:

class Name
{
public:
//all of your functions here keep in mind the syntax.
//if you don't mind having everything public, you can do so and don't have to write any getters and setters if you plan to use other code with this class.

//example function:
void PrintData(void);

};

Now when you write the definition of the function (if you write it outside of the classes brackets:

void Name::PrintData(void)
{
//code
}

as a question, do you write things in multiple files, like .h and .cpp file?
Yeah, you shouldn't give up. Learning just takes some time. Anyways there are some problems due to inconsistencies with your program.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
airlineType convertString(string str)
{
    switch(toupper(str.at(0)))       // checks the  first character of the string
    {
        // impossible cases character data types can't be 2 letters
        case 'SP': return Spirit Airlines;                // Spirit Airlines undeclared 
        case 'NW': return Northwest Airlines;      // Northwest Airlines undeclared
        case 'SW': return Southwest Airlines;      // Southwest Airlines undeclared
        case 'AA': return American Airlines;          //  American Airlines undeclared

        // additional note:
        //         you don't have a break after each case either
        //         so it would go through all your cases to the end
        //         even if the case was correct.
    }
    return NONE;          // NONE undefined
}


Anyways your compiler would think that Spirit Airlines, Nortwest Airlines, Southwest Airlines, and American airlines are variables because you don't have quotation marks. If you're trying to return them as a string though then it's inconsistent with the return value of your function which is expecting one of the enum types you made.

I suggest trying to make a separate program for each of your functions to test them individually. And only when they're all working should you combine them into a bigger program, especially if you're still a beginner, it would help you learn faster as well. =)
Last edited on
Topic archived. No new replies allowed.