Array question

I am making a program for fun to test out my knowledge of different types of C++ techniques. This program takes different information about airplane tickets someone bought. What i want to try to do, is make my cArray() function return the contents of the array back into main. After the first call to cArray() is returned, i make a call to displayArray from main() which fails to display the contents of ticketArray[].

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

char airline;
int numberOfTickets;
char destination[80];
char departingFrom[80];
int numberOfStops;
char airlineA[80];// good
char airlineB[80];// good
int ticketA[80];
int numberOfAirlines; // good
int ticketArray[10]; // made ticketArray[10] and ticketCost global
int ticketCost;		// from cArray() if problem arises, move back	
int i;

void displayArray(int integerArray[], int sizeOfArray){
	cout << "Ticket costs have been charted as the following:\n";
	for(int i=1; i < sizeOfArray; i++){
		cout.width(3);
		cout << i << ": " << integerArray[i] << "\n";
	}
	cout << "\n";
}

int cArray(){ // ticket cost array function

		int i=1;
		for(; i<10; i++){
			if(i==numberOfTickets+1){
				break;
			}
			
			cout << "Enter cost of ticket: ";
			cin >> ticketCost;
			
			ticketArray[i] = ticketCost;
		}
		displayArray(ticketArray, i);
		return ticketArray[i], i;
}

int main(){
		
	cout << "This program takes the airline,\n"
		 << "number of tickets, stops, and costs and charts them.\n\n\n";
	
	cout << "Enter number of tickets: ";
	cin >> numberOfTickets;
	
	cout << "How many airlines are your flying with: ";
	cin >> numberOfAirlines;

	if(numberOfAirlines>1){
		cout << "Enter first airline: ";
		cin >> airlineA;
		cout << "Enter next airline: ";
		cin >> airlineB;
		}
	else{
		cout << "Enter airline: ";
		cin >> airlineA;
		}
	
	cout << "Where are you departing from: ";
		cin >> departingFrom;
	cout << "Where is your destination?: ";
		cin >> destination;
		cArray();
		
		char y;
		do{
		cout << "Is the information correct so far? (y/n): ";
		cin >> y;
		if(y == 'y'){
			break;
		}
		else
			cout << "Ok, re-insert ticket costs\n";
			cArray();
		}
		while(y == 'n');

		displayArray(ticketArray, i);
		

	system("pause");
	return 0;
}
Last edited on
Because all of your variables are global, main already has access to everything that cArray modifies. The return for cArray can be void, especially because on line 82 you do not use the return value when you call the function.

The reason displayArray does not work the second time is because of the value of i. When the function is called on line 41, i = numberOfTickets+1. When the function is called on line 86, i = 0. You pass 0 as the size of the array, so the display does not work as expected.
Last edited on
Topic archived. No new replies allowed.