Help with arrays

I have this program I am working on and I need to add a few things on it. I am new to programming and I am going by what books and a few videos say, but I can sure do more with some help from more experienced programmers.

this is what I need help with:
-I need to let the user know what format to put in time
-missing part where not asking user how many laps to enter.
this was supposed to be a program that calculates the output of three car positions for a two lap race and a three lap race. The time input is supposed to be in MM:SS.
the second part I need to Store the information of each car in an array or multiple arrays if needed. Example: car numbers, colors of cars, lap time for each car, etc.
then 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
lastly I need it to Print out an error message if the user gives invalid input.

Here is the code so far

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
 /*Three car race*/

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string driverA, driverB, driverC,
		colorRed, colorBlue, colorGreen;
	int number1, number2, number3,
		laptimeM1, laptimeS1,
		laptimeM2, laptimeS2,
		laptimeM3, laptimeS3;

	// Car1 Info
	cout << "Who is the driver for car one? ";
	getline(cin, driverA);
	cout << "What is the color for car one? ";
	getline(cin, colorRed);
	cout << "What is car one's number? ";
	cin >> number1;
	cout << "What was car one's laptime? enter time in MM:SS "; // format M:S
	cin >> laptimeM1 >> laptimeS1;
	cin.ignore();

	// Car2 Info
	cout << endl << "Who is the driver for car two? ";
	getline(cin, driverB);
	cout << "What is the color for car two? ";
	getline(cin, colorBlue);
	cout << "What is car two's number? ";
	cin >> number2;
	cout << "What was car two's laptime? enter time in MM:SS";
	cin >> laptimeM2 >> laptimeS2;
	cin.ignore();

	// Car3 Info
	cout << endl << "Who is the driver for car three? ";
	getline(cin, driverC);
	cout << "What is the color for car three? ";
	getline(cin, colorGreen);
	cout << "What is car three's number? ";
	cin >> number3;
	cout << "what is car three's laptime? enter time in MM:SS";
	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 << driverA << " " << colorRed << " " << number1 << " " << s1 << "seconds" << endl;
	if (s2 <= s1 && s2 <= s3)
		cout << driverB << " " << colorBlue << " " << number2 << " " << s2 << "seconds" << endl;
	if (s3 <= s2 && s3 <= s1)
		cout << driverC << " " << colorGreen << " " << number3 << " " << s3 << "seconds" << endl;

	cout << endl << "Slowest: " << endl;
	if (s1 >= s2 && s1 >= s3)
		cout << driverA << " " << colorRed << " " << number1 << " " << s1 << "seconds" << endl;
	if (s2 >= s1 && s2 >= s3)
		cout << driverB << " " << colorBlue << " " << number2 << " " << s2 << "seconds" << endl;
	if (s3 >= s2 && s3 >= s1)
		cout << driverC << " " << colorGreen << " " << number3 << " " << s3 << "seconds" << endl;
	
	system("pause");
	return 0;
}
if I need to modify or change the code please let me know as I am still learning.
this is what I had in mind. let me know if I'm even doing it right please!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<string>

int main()
{

	Car cars[3];//Create an array of three cars
	for (int i = 1; i < 4; i++)
	{
		cout << "Enter the color of car #" << i << endl;
		string carcolor = "Red,Blue,Green";
		cin >> carcolor;
		cars[i - 1].color = carcolor;
		cout << "Enter the lap time for car #" << i << endl;
		int laptime = 0;
		cin >> laptime;
		cars[i - 1].SetLapTime(i - 1, laptime);
	}
}
Last edited on
Help anyone
Huh maybe looking at the following tutorials about arrays, data structures, and classes might help:
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/structures/
http://www.cplusplus.com/doc/tutorial/classes/

Also maybe trying something like:
1
2
3
4
5
6
7
8
9
//create a 2-dimensional array by Array array_name [1][4] 
// *or* 
//make a data structure that has name of driver, color of car, number of car and use 
// a lap variable with type of integer (probably not going to deal with a half a lap)
// have the user input the information.
// use one of the data structures to store & retrieve that information.
// do the math with the retrieve info
// print-out results.
// *fin* 


Have fun,

~ Hirokachi
Last edited on
Topic archived. No new replies allowed.