Thread: Stuck and need help.

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Stuck and need help.

    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. #2
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74
    Code:
    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;
    }
    Is this routine written in C/C++? It is full of errors.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i started to answer this, then realised its just too difficult to make head or tail of what you are up to at moment without me having to concentrate ( = too much effort! )

    this is all over the place, please sort out your indentation to make it easier to read, and really review your code and ask yourself why you are doing some of the things you are doing...

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Here's a little help, but I have to run to the DMV so I can't look through it much more this morning

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    // This is practically the same as
    // #define SP 0
    // #define NW 1
    // #define SW 2
    // #define AA 3
    // However, your implementation doesn't use it correctly
    
    enum airlineType {
    	SP,
    	NW,
    	SW,
    	AA
    };
    
    // You should return a string, and take an integer arg
    airlineType convertString(string str)
    // Not sure if these const deals are correct: I'm bad at it
    // const string& convertString(int airline) {
    {
    	// You don't really want to look at the first
    	// letter of the airline name, because you have
    	// SP and SW, str.at(0) will return S and be useless
    	switch(toupper(str.at(0)))
    	{
    		// When using switch statements, it's always good to
    		// through a break; after each so it doesn't fall
    		// through to the next condition, although this
    		// particular switch statement is broken
    		
    		case 'SP': return "Spirit Airlines"; // break;
    		case 'NW': return "Northwest Airlines"; // break;
    		case 'SW': return "Southwest Airlines"; // break;
    		case 'AA': return "American Airlines"; // break;
    		default: // None of the above
    			// Bad airline name
    	}
    	
    	// switch (airline) {
    	//		case SP: return "Spirit Airlines"; break;
    	//		case NW: return "Northwest Airlines"; break;
    	//		...
    	//		default: return "NONE";
    	// }
    }
    // NONE is undefined and outside the function
    return NONE;
    // Extra bracket: here's an instance where proper indentation helps
    }
    
    // Take an integer argument, not airlineType
    void display(airlineType air)
    // void display(int airline) {
    {
    	// Good job, however, you should probably use your convertString 
    	// function here
    	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;
    	}
    	// Could be:
    	//	cout << convertString(airline) << endl; break;
    }
    } // Extra bracket
    
    ...
    Good luck!

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Code:
    ...
    
    // I don't think you need a string reference to store the
    // city. I _believe_ that a copy is returned.
    void getCity(string line, string& city)
    // string getCity(string line) {
    {
    	//	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.
    	
    	// To temporarly store the city
    	string cityAbr = "";
    	string::size_type pos; // Bravo
    
    	pos = line.find(" "); //find the first space
    	// What if there is no space in the city name?
    	// Returns the position of the first occurrence in
    	// the string of the searched content.
    	//
    	// If the content is not found, the member value npos is returned.
    
    	//	cityAbr = line.substr(0, 1); // First letter
    	
    	// 	if (pos == string::npos) // City is one word
    	//		cityAbr += line.substr(line.length() - 1, 1); // Last letter of city name
    	//	else
    	//		cityAbr += line.substr(pos + 1, 1); // First letter of second word
    		
    	//	std::transform(cityAbr.begin(), cityAbr.end(), cityAbr.begin(), toupper);
    	//	return cityAbr;
    	
    	// ???????? :o
    	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()
    {
    	// tripType is undefined
    	// Also, cout/cin and such should probably go
    	// into the main loop, and functions should
    	// just be doing work on that data
    
    	int x;
    	
    	cout << "Please enter [1] for one-way or [2] for round trip ";
    	cin >> x;
    
    	// Undefined
    	return static_cast<tripType>(x);
    }
    
    int main()
    {
    	ifstream fin;
    	int price = 0;	// Monetary amounts are not integers
    	int salesTax = 0; // ---^
    	string line = "", city = ""; // Unused
    	bool upper = false; // Useless
    	
    	fin.open("myinput.txt");
    	// Check for failure -- perhaps the file
    	// doesn't exist or something more tragic
    	
    	// Broken
    	city = getCity(fin);
    	
    	// Redundant
    	cout << "You have entered the city " << city;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed