Issue with bracket balancing and checking to make sure string is only in letters

Hello! I am working on an assignment where I need to validate the string input for car colors are letters only. I was able to get my car numbers to only be integers, but I am stuck on how to validate the string for car colors. In addition, when I attempt to sort by race time, I keep getting an issue saying my brackets are not balanced, but to me, they are. Anyone have some insight on my two issues? This is my sample code, deleted some parts b.c I exceed the character count.

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;


int main ()
{

//AW Declaring variables


using namespace std;

int CarNum[3];
string colors[3];
int LapTimeMinutes[3];
int LapTimeSeconds[3];
int ConvertedLapTimeSeconds[3];
int total[3];
int RaceTimeMin[3];
int RaceTimeSec[3];


//AW Car Number Validator for First Car

{
	int CarNum[3];
	bool valid = false;

	while (!valid)
	{
		valid = true; //Assume the cin will be an integer.

		cout << "Enter a car number in an integer for the first car." << endl;
		cin >> CarNum[0];


		if(cin.fail()) //cin.fail() checks to see if the value in the cin
					//stream is the correct type, if not it returns true,
					//false otherwise.
		{
			cin.clear(); //This corrects the stream.
			cin.ignore(); //This skips the left over stream data.
			cout << "Please enter an integer only for the car." << endl;
			valid = false; //The cin was not an integer so try again.
		}

	}

	cout << "You entered: " << CarNum[0] << endl;

	system("PAUSE");
	
}





//AW User input for car colors

cout<<"Enter a color for the first car.";
cin >> colors[0];
cin.ignore();

 
 

cout<<"Enter a different color for the second car.";
//getline

//AW ATTEMPT TO CHECK THAT STRING IS ONLY IN LETTERS
bool colors[3]
{
	bool verify = true;
	for (int i = 0; i < check.length(); i++)
}
{
		if (colors[1] >= 'a' && colors[1] <= 'z' || colors[1] >= 'A' && checkcolors[1] <= 'Z')
			verify = true;
		else
		
}		
			verify = false;
			break;
	
	return verify;
}

cout<<colors[0]<<endl;
cout<<colors[1]<<endl;
cout<<colors[2]<<endl;


//AW User input for Lap Times


cout << "Please enter the Lap Time in minutes for the first car." << endl;			
cin >> LapTimeMinutes[0];
cin.ignore();

//AW converts lap time minutes into seconds
ConvertedLapTimeSeconds[0] = LapTimeMinutes[0] * 60;



cout << "Now enter the Lap Time in seconds for the first car, no more than 59 seconds." << endl;	
cin >> LapTimeSeconds[0];
cin.ignore();

//AW Need total to obtain racetime
total[0] =  LapTimeSeconds[0] + ConvertedLapTimeSeconds[0];


//AW Puts lap times in MM:SS format

cout<<LapTimeMinutes[0]<<":"<<LapTimeSeconds[0]<<endl; 



//AW used to calculate race time
RaceTimeMin[0] = LapTimeMinutes[0] * 2;
RaceTimeSec[0] = LapTimeSeconds[0] * 2;



//practice


cout << "Please enter the Lap Time in minutes for the second car." << endl;			
cin >> LapTimeMinutes[1];
cin.ignore();

cout << "Now enter the Lap Time in seconds for the second car, no more than 59 seconds." << endl;	
cin >> LapTimeSeconds[1];
cin.ignore();

//AW Puts lap times in MM:SS format
cout<<LapTimeMinutes[1]<<":"<<LapTimeSeconds[1]<<endl; 


//AW converts lap time minutes into seconds
ConvertedLapTimeSeconds[1] = LapTimeMinutes[1] * 60;

//AW Need total to obtain racetime
total[1] =  LapTimeSeconds[1] + ConvertedLapTimeSeconds[1];


//AW used to calculate race time
RaceTimeMin[1] = LapTimeMinutes[1] * 2;
RaceTimeSec[1] = LapTimeSeconds[1] * 2;

//AW REMOVE USED FOR TESTING

cout<<"The race time for car 1 is" << ' ' << RaceTimeMin[1]<<":"<<RaceTimeSec[1]<<endl; 


for (int i = 0; i < 3; i ++)


return 0;
}

//AW This will check if the first car got 1st place

if(total[0] < total[1] && total[0] < total[2])
{
  if(RaceTimeMin[0] < RaceTimeMin[1])
  {   

    cout << CarNum[0] << ' ' << colors[0] << " placed first with a race time of " << RaceTimeMin[0] << ":" << RaceTimeSec[0] << endl;
     cout << CarNum[1] << ' ' << colors[1] <<" placed second with a race time of "<< RaceTimeMin[1]  << ":" << RaceTimeSec[1]  << endl;
     cout << CarNum[2] << ' ' << colors[2] <<" placed third with a race time of "<< RaceTimeMin[2]  << ":" << RaceTimeSec[2] << endl;
  }
  else
  {

     cout << CarNum[0]<< ' ' << colors[0] << " placed first with a race time of "<< RaceTimeMin[0] << ":" << RaceTimeSec[0] << endl;
     cout << CarNum[2] << ' ' << colors[3] << " placed second with a race time of "<< RaceTimeMin[2] << ":" <<  RaceTimeSec[2] <<endl;
     cout << CarNum[1] << ' ' << colors[2] << " placed third with a race time of "<< RaceTimeMin[1] << ":" <<  RaceTimeSec[1] << endl;
  }
}
if(total[1] < total[0] && total[1] < total[2])    
{
  if(total[0] < total[2])
  {
     cout <<  CarNum[1] << ' ' << colors[1] << " placed first with a race time of "<<RaceTimeMin[1] << ":" << RaceTimeSec[1] << endl;
     cout << CarNum[0]<< ' ' << colors[0] << " placed second with a race time of "<< RaceTimeMin[0] << ":" << RaceTimeSec[0] << endl;
     cout << CarNum[2] << ' ' << colors[2] << " placed third with a race time of "<< RaceTimeMin[2] << ":" << RaceTimeSec[2] << endl;
  }
  else
  {
     cout << CarNum[1] << ' ' << colors[1] << " placed first with a race time of "<< RaceTimeMin[1] << ":" << RaceTimeSec[1] << endl;
     cout << CarNum[2] << ' ' << colors[2] << " placed second with a race time of "<< RaceTimeMin[2] << ":" << RaceTimeSec[2] << endl;
     cout << CarNum[0] << ' ' << colors[0] << " placed third with a race time of "<< RaceTimeMin[0] << ":" << RaceTimeSec[0] << endl;
  }
}


if(total[2] < total[0] && total[2] < total[1])
{
  if(total[1] < total[0])
  {
     cout << CarNum[2] << ' ' << colors[2] << " placed first with a race time of "<<RaceTimeMin[2] << ":" <<RaceTimeSec[2] << endl;
     cout << CarNum[1] << ' ' << colors[1] << " placed second with a race time of "<< RaceTimeMin[1] << ":" <<RaceTimeSec[1] << endl;
     cout << CarNum[0] << ' ' << colors[0] << " placed third with a race time of "<< RaceTimeMin[0] << ":" << RaceTimeSec[0] << endl;
  }
  else
  {

     cout << CarNum[2] << ' ' << colors[2]  << " placed first with a race time of "<< RaceTimeMin[2] << ":" << RaceTimeSec[1] << endl;
     cout << CarNum[0] << ' ' << colors[0] << " placed second with a race time of "<< RaceTimeMin[0] << ":" << RaceTimeSec[0] << endl;
     cout << CarNum[1] << ' ' << colors[1] << " placed third with a race time of "<< RaceTimeMin[1] << ":" << RaceTimeSec[1] << endl;
  }
}

return 0;



Last edited on
Well since you removed some code I can only guess about your brace problems but look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//AW ATTEMPT TO CHECK THAT STRING IS ONLY IN LETTERS
bool colors[3]
{
	bool verify = true;
	for (int i = 0; i < check.length(); i++)
}
{
		if (colors[1] >= 'a' && colors[1] <= 'z' || colors[1] >= 'A' && checkcolors[1] <= 'Z')
			verify = true;
		else
		
}		
			verify = false;
			break;
	
	return verify;
}

You seem to have stray braces all over the place in this snippet. For example you have an ending brace directly after the for statement, which is wrong.

Also you may want to look up some to of the <cctype> functions to see if they might be useful, for example isalpha().

Also your colors[] is an array of bool, so your if() statement will probably never evaluate to true since bool only allows two vaues (true or false) or (zero or not zero).

Also note that your check for the number is also incorrect. What happens if the user enters 1ABC?


I have fixed the braces issue! I am still having difficulty trying to check my string array for only letters.

So I did research alpha, but I am encountering an error. Here is what I modified instead of using bool verify = true.

I am trying to make sure the string array only uses letters and to provide an error message and retry again. As I mentioned, I was able to do it with the car number (integers) but cannot figure out how to do it with strings.

For instance, I have no idea even after research on how to save the user input in an array AND make sure the input is only in letters.

1
2
3
4
5
6
7
8
9
10
11
12

    cout<<"Enter a color for the first car.";
    cin >> colors[0];
   cin.ignore();

    [for(i=0; i < colors.size(); i++)

            if(isalpha(colors[i]))

                cout << colors[i];

                	system("PAUSE");


The errors state:
In function 'int main()':
151:5: error: 'i' was not declared in this scope
151:21: error: request for member 'size' in 'colors', which is of non-class type 'std::string [3] {aka std::basic_string<char> [3]}'
So what exactly don't you understand about those error messages?

What type of variable is colors[] (show how it was declared)?

Where did you declare the variable 'i'?

A small complete program that shows your problem would be helpful.
Last edited on
I spent too much time trying to figure out the alpha for string so I am giving up on it.

I am now working on implementing a "while" loop. I am trying to get it where if someone inputs 5:63, it will say that is incorrect and have them try again. However, it terms the program when the answer is wrong instead of giving the user another attempt. What am I doing wrong?


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
#include <iostream>
#include <string>
#include <iomanip>
#include <string>

int main ()
{
using namespace std;


int LapTimeMinutes[3], LapTimeSeconds[3];
char ch;


{
cout << "Please enter the first car's lap time in (HH:MM): ";
cin >> LapTimeMinutes[0] >> ch >> LapTimeSeconds[0];
cout << "The first car's lap time is " << setw(2) << setfill('0') << LapTimeMinutes[0] << ":" << LapTimeSeconds[0] << endl;

if (LapTimeSeconds[0] < 60)

{
    cout << "You entered it correctly" << endl;


}

else
{
cout << "You did not enter the lap time  correctly." << "Please make sure seconds are less than 59" << endl;
while (LapTimeSeconds[0] < 60)



 return 0;


cout << endl;

}
}
}


Normally you would create it like this:
1
2
3
4
5
6
7
8
9
bool input_valid = false;
while(!input_valid)
{
  //  read the input
  //  if the input is valid
  //     input_valid = true; will leave the loop
  //  else
  //     show error msg
}
Topic archived. No new replies allowed.