too many characters in constant?

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
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

int main()
{
	int gas;
	int batt;
	bool on;
	car cars;
	int i;
	string action;
	string engine;
	do
	{
		cout << "Welcome to the car class test" << endl << "Enter 'straight' to go forward, ";
		cout << "'left' to turn left, and 'right; to turn right." << endl;
		cout << "You can also check the battery and gas level by typing 'battery' and 'gas' respectivly." << endl;
		cout << "type 'start' to start the car.  You can also type 'park' to stop." << endl;
		
		do
		{
			on = cars.geton();	
			batt = cars.batlvl();
			gas = cars.gaslvl();

			cin >> engine;

			if(engine.compare('start') == 0)
			{
				cars.start();
				cout << "The car has started" << endl;
			}
			
			else
				cout << "The car is still in park.  You must start the car first." << endl;
		}
		while(on == false);

		while(batt || gas > 0)
		{
		cout << "What would you like to do?" << endl;
		}

		cin >> action;
		for(i = 0;action[i] != '\0'; i++)
		{
			action[i] = tolower(action[i]);
		}

		if(action.compare('straight' || 'right' || 'left'))
		{
			cars.direction(action);
		}

		if(action.compare('battery'))
		{
			batt = cars.batlvl();
			cout << "The battery level is " << batt << endl;
		}

		if(action.compare('gas'))
		{
			gas = cars.gaslvl();
			cout << "The gas level is " << gas << endl;
		}
	}
	batt = cars.batlvl();
	gas = cars.gaslvl();

	if(gas <= 0)
	{
		cout << "Your car ran out of gas." << endl;
	}

	else if(batt <= 0)
	{
		cout << "Your battery died." << endl;
	}

	system("PAUSE");
    return EXIT_SUCCESS;
}



On all of my string.compare lines I am getting an error that says there are too many characters in constant. Can anyone tell me what this means? Thanks in advance. Oh and this whole thing is part of one large class, but I'm not getting errors from it.
Use the " character rather than the single tick ' character.

The single tick or ' character is for a single character and not a string . The double tick or " is for strings.

ie..
if(engine.compare("start") == 0)

However this could be simplified to:
 
if ("start" == engine)


I find using the .compare() function useful for substring compares.
Last edited on
Topic archived. No new replies allowed.