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.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
int main ()
{
//AW Declaring variables
usingnamespace 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;
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]}'
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?
#include <iostream>
#include <string>
#include <iomanip>
#include <string>
int main ()
{
usingnamespace 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;
}
}
}
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
}