Comparing a string to a word

I am trying to make a program where the user can input information about a road trip and the program will tell the user about the cost of the trip. The first part works well. My question is in the second part. I made an array and would like to stop inputting once the array is full (at 100 entries) OR when the user enters a key word. I tried telling it to quit when the array is full or when the string equals the key word, but it is giving me an error saying that I can't compare the two. Any help would be greatly appreciated!

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	int cars;
	double miles, gasPrice;
	double gallons = 0;
	double totalMpg = 0;
	double MPG = 0;
	
	cout << "GAS COSTS" << endl;
	cout << "Enter Number of Cars: ";
	cin >> cars;

	int * carMPG;
	carMPG = new int[cars];

	for (int i = 0; i < cars; ++i)
	{
		cout << "Enter MPG for each car " << i + 1 << ": ";
		cin >> carMPG[i];
	}
	for (int i = 0; i < cars; ++i)
	{
		totalMpg = totalMpg + carMPG[i];
	}
	cout << "Enter the number of miles you will be driving: ";
	cin >> miles;

	for (int x = 0; x < cars; x++)
	{
		gallons = gallons + (miles / carMPG[x]);
	}
	
	cout << "Enter the price of gas: ";
	cin >> gasPrice;
	
	cout << "You will use " << (gallons) << " gallon(s) of gas" << endl;
	cout << "The total cost will be: $" << gallons * gasPrice << endl;

	cout << "ENTERTAINMENT COSTS" << endl;
	
	double attractionCost [100];
	string attractionName [100];
	int j = 0;
	for (int j = 0; j < 100 && attractionName == "quit"; ++j)
	{
		cout << "Enter attraction Name: ";
		cin >> attractionName[j];
		cout << "Enter cost of the attraction: ";
		cin >> attractionCost[j];
	}
	cout << attractionName[5] << attractionCost[5];

	system("pause");
	return 0;
}
Last edited on
attractionName is an array, not a string.

Also, you probably want to continue only while the last entered name != "quit".

I would personally add that to the loop and never the array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (j < 100)
{
  cout << "Enter attraction Name: ";
    string name;
    getline( cin, name );
    if (name == "quit") break;
    attractionName[j] = name;

  cout << "Enter cost of the attraction: ";
    cin >> attractionCost[j];
    getline( cin, name );  // ignored

  j += 1;
}

Hope this helps.
Topic archived. No new replies allowed.