HELP GREATLY APPRECIATED!!!

I wrote the code listed below last week and need to change it to Storing the information of each car in an array (use multiple arrays if needed). Example: car numbers, colors of cars, lap time for each car, etc.
Validate user input via the following rules:
Car number must be an integer
Car color must be a string
Lap time format must be MM:SS
Print out an error message if the user gives invalid input.

I am lost! 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
61
 #include <iostream>
#include <string>
using namespace std;

int main()
{
	string driver1, driver2, driver3,
		color1, color2, color3;
	int number1, number2, number3,
		laptimeM1, laptimeS1,
		laptimeM2, laptimeS2,
		laptimeM3, laptimeS3;

	// Car1 Information
	cout << "Who is the driver for car one? ";
	std:: getline(cin, driver1);
	cout << "What is the color for car one? ";
	std::getline(cin, color1);
	cout << "What is car one's number? ";
	cin >> number1;
	cout << "What was car one's laptime? ";
	cin >> laptimeM1 >> laptimeS1;
	cin.ignore();

	// Car2 Information
	cout << endl << "Who is the driver for car two? ";
	std::getline(cin, driver2);
	cout << "What is the color for car two? ";
	std::getline(cin, color2);
	cout << "What is car two's number? ";
	cin >> number2;
	cout << "What was car two's laptime? ";
	cin >> laptimeM2 >> laptimeS2;
	cin.ignore();

	// Car3 Information
	cout << endl << "Who is the driver for car three? ";
	std::getline(cin, driver3);
	cout << "What is the color for car three? ";
	std::getline(cin, color3);
	cout << "What is car three's number? ";
	cin >> number3;
	cout << "what is car three's laptime? ";
	cin >> laptimeM3 >> laptimeS3;

	// Convert all to seconds
	int s1 = laptimeS1 + laptimeM1 * 60;
	int s2 = laptimeS2 + laptimeM2 * 60;
	int s3 = laptimeS3 + laptimeM3 * 60;

	cout << endl << "Fastests: " << endl;
	if (s1 <= s2 && s1 <= s3)
		cout << driver1 << " " << color1 << " " << number1 << " " << s1 << "seconds" << endl;
	if (s2 <= s1 && s2 <= s3)
		cout << driver2 << " " << color2 << " " << number2 << " " << s2 << "seconds" << endl;
	if (s3 <= s2 && s3 <= s1)
		cout << driver3 << " " << color3 << " " << number3 << " " << s3 << "seconds" << endl;


	return 0;
}
Last edited on
Have you considered creating a struct Car with number, color and time as the data fields? Then simply make an array of Car objects.
How would I do that? I am very new to this and I have been working on it for 6 hours now.
Are you familiar with structs?
Read about them here: http://www.cplusplus.com/doc/tutorial/structures/

Thus, your struct can look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Car
{
    string color;
    string driver_name;
    int number;
    int t_minutes;
    int t_second;
}

// then down in main you can make an array of Cars
int main()
{
    Car cars[3]; // creates an array of type Car, of size 3. 
    
    // fill up the array in the rest of the code. 
}
Would I then need to make multiple array's?
And where and how would I add the error message?
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
#include <iostream>

struct Car
{
    std::string color;
    std::string driver_name;
    int number;
    int t_minutes;
    int t_seconds;
};


int main()
{
    const int SIZE = 3;
    Car cars[SIZE];
    // fill up the array with Cars
    for(int i = 0; i < SIZE; i++)
    {
        std::cout << "Who is the driver for car " << i+1 << "? ";
        std::getline(std::cin, cars[i].driver_name);
        std::cout << "What is the color for car " << i+ 1 << "? ";
        std::getline(std::cin, cars[i].color);
        std::cout << "What is car number for car " << i + 1 << "? ";
        std::cin >> cars[i].number;
        std::cout << "What was the laptime for car " << i + 1 << "? ";
        std::cin >> cars[i].t_minutes >> cars[i].t_seconds;
        std::cin.ignore(1000, '\n');
        std::cout << "\n";
    }
    
    // print out info
    for(int i = 0; i < SIZE; i++)
    {
        std::cout << cars[i].driver_name << std::endl;
        std::cout << cars[i].color << std::endl;
        std::cout << cars[i].number << std::endl;
        std::cout << cars[i].t_minutes << ":" << cars[i].t_seconds << std::endl;
        std::cout << "\n";
    }
}


You would check for errors in an if-statement after every std::cin, and add an error message in the if-clause.
Last edited on
tHANK YOU VERY MUCH!!! SO I CAN PRETTY MUCH GET RID OF THE REST OF MY PREVIOUS CODE? LAST QUESTION
Yea, pretty much. All that extra work is being done by the loop. Never duplicate code, always use loops.
Do I need to include anything at the top to use the array?
No, the program runs as posted.
you are the best! Thank you!
Topic archived. No new replies allowed.