Constructor Problem, Displaying Blank

I think I might actually be doing this wrong. Is this allowed? when I try to display the choices, they are just blank. What is the proper way of setting values to the array airports?

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

class ORIGINANDDESTINATION
{
public:
	ORIGINANDDESTINATION()
	{
		string airports[34]= {"Bacolod", "Boracay", "Butuan", "Cagayan de Oro", "Calbayog", "Cataman", 
			"Cauayan", "Cebu", "Clark", "Coron(Busuanga)", "Cotabato", "Davao", "Dipolog", 
			"Dumaguete", "General Santos", "Iloilo", "Kalibo","Laoag", "Legazpi", "Manila",
			"Naga", "Ozamiz", "Pagadian", "PuertoPrincesa", "Roxas", "San Jose(Mindoro)",
			"Siargiao","Surigao", "Tacloban", "Tagbilaran", "Tawi-Tawi", "Tuguegarao",
			"Virac", "Zamboanga"};
	}
	void displayChoices()
	{
		cout << "||||||||||||||||||||||||||ORIGIN AND DESTINATION LIST|||||||||||||||||||||||||||\n";
		for (int i=0; i <34; i++)
		{
			if (i%2==1)
				cout << "[" << i+1 <<"]" << airports[i] << endl;
			else
				cout << "[" << i+1 <<"]" << left << setw(20) << airports[i];
		}
		cout << "\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
	}
	void getOrigin()
	{
		cout << "Please enter the the number of your origin and press enter: ";
		cin >> originChoice;		
		while (originChoice<1 || originChoice >34)
		{
			
			cout << "Invalid Input. Please enter choice origin again: ";
			cin >> originChoice;
		} 
	}
	void getDestination()
	{
		cout << "Please enter the the number of your destination and press enter: ";
		cin >> destinationChoice;
		while (destinationChoice<1 || destinationChoice >34)
		{
			cout << "Invalid input. Please enter destination choice again: ";
			cin >> destinationChoice;
		} 
	}
	void forDisplay(string& origin, string& destination)
	{
		origin=airports[originChoice-1];
		destination=airports[destinationChoice-1];

	}
private:
	string airports[34];
	int originChoice;
	int destinationChoice;	
};





Uh you actually run into some issues trying it this way. You'll have to a copy constructor, assignment operator, and destructor. Vectors are the way to go in this situation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ORIGINANDDESTINATION()
	{
		string airports[34]= {"Bacolod", "Boracay", "Butuan", "Cagayan de Oro", "Calbayog", "Cataman", 
			"Cauayan", "Cebu", "Clark", "Coron(Busuanga)", "Cotabato", "Davao", "Dipolog", 
			"Dumaguete", "General Santos", "Iloilo", "Kalibo","Laoag", "Legazpi", "Manila",
			"Naga", "Ozamiz", "Pagadian", "PuertoPrincesa", "Roxas", "San Jose(Mindoro)",
			"Siargiao","Surigao", "Tacloban", "Tagbilaran", "Tawi-Tawi", "Tuguegarao",
			"Virac", "Zamboanga"};
...

private:
	string airports[34];
	int originChoice;
	int destinationChoice;	
	}

You're re-declaring airports local to the constructor, the array class member isn't changed. Try this in the ctor instead
airports[] = {"blah",...}
Last edited on
following nataku9333's advice, i get this error:

1>d:\documents\ticketing system\ticketing system\methods.h(231): error C2059: syntax error : ']'
1>d:\documents\ticketing system\ticketing system\methods.h(231): error C2143: syntax error : missing ';' before '{'
1>d:\documents\ticketing system\ticketing system\methods.h(236): error C2143: syntax error : missing ';' before '}'
I have a feeling you'd have to do some dynamic memory shenanigans to make this. Or manually enter each element one at a time, which be silly. Id just throw this all in a vector
Sorry, my example wont work for you as c++ only allows initializing an array like that when its declared.

But what you can do is
1
2
3
4
5
6
7
8
std::string temp[] = {"Bacolod", "Boracay", "Butuan", "Cagayan de Oro", "Calbayog", "Cataman", 
			"Cauayan", "Cebu", "Clark", "Coron(Busuanga)", "Cotabato", "Davao", "Dipolog", 
			"Dumaguete", "General Santos", "Iloilo", "Kalibo","Laoag", "Legazpi", "Manila",
			"Naga", "Ozamiz", "Pagadian", "PuertoPrincesa", "Roxas", "San Jose(Mindoro)",
			"Siargiao","Surigao", "Tacloban", "Tagbilaran", "Tawi-Tawi", "Tuguegarao",
			"Virac", "Zamboanga"};
		for(int i = 0; i<34; ++i)
			airports[i] = temp[i];

Its not pretty but it works. A better idea may be storing the strings in a file and read them into the array. Or as ResidentBiscuit suggested, use a vector.
Last edited on
If you have a compiler that supports C++ 2011 then you can write

1
2
3
4
5
6
7
8
9
10
ORIGINANDDESTINATION() : airports( 
{"Bacolod", "Boracay", "Butuan", "Cagayan deOro", "Calbayog", "Cataman", 
  "Cauayan", "Cebu", "Clark", "Coron(Busuanga)", "Cotabato", "Davao", "Dipolog", 
  "Dumaguete", "General Santos", "Iloilo", "Kalibo","Laoag", "Legazpi", "Manila",
  "Naga", "Ozamiz", "Pagadian", "PuertoPrincesa", "Roxas", "San Jose(Mindoro)",
  "Siargiao","Surigao", "Tacloban", "Tagbilaran", "Tawi-Tawi", "Tuguegarao",
 "Virac", "Zamboanga"} ) 
{
//  empty constructor body
}



Will the content of your array ever change? If you're just looking to use it as a lookup table, then you might be better off with a static array inside a function (for easier error checking, and to avoid the dreaded static-initialisation fiasco)

e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ORIGINANDDESTINATION
{
public:
    static bool lookup_airport( size_t n, std::string& name)
    {
        static const size_t num_airports = 34;
        static const std::string airports[num_airports]= {
            "Bacolod", "Boracay", "Butuan", "Cagayan de Oro", "Calbayog", "Cataman", 
            "Cauayan", "Cebu", "Clark", "Coron(Busuanga)", "Cotabato", "Davao", "Dipolog", 
            "Dumaguete", "General Santos", "Iloilo", "Kalibo","Laoag", "Legazpi", "Manila",
            "Naga", "Ozamiz", "Pagadian", "PuertoPrincesa", "Roxas", "San Jose(Mindoro)",
            "Siargiao","Surigao", "Tacloban", "Tagbilaran", "Tawi-Tawi", "Tuguegarao",
            "Virac", "Zamboanga" };

        if( n < 0 || n >= num_airports )
        {
            return false;
        }
        name = airports[n];
        return true;
    }
}; 
Last edited on
Topic archived. No new replies allowed.