Newbie to C++ needs help with Car Race errors

I thought I was getting closer to finishing and now, I have lots of errors to fix ASAP! I'm researching them one at a time, but really need some help! Thanks in advance!!!

Object: Car Race > 3 laps > user inputs info > program prints winner of the race.

Please assist me with the following: ( I was able to fix the others)
1.) Compile errors:

(113): error C2955: 'std::array': use of class template requires template argument list

note: see declaration of 'std::array'

(150): error C2447: '{': missing function header (old-style formal list?)


2.) Please assist with error validation messages. I get them to print, but need the program to quit if invalid input from user.

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

#include <iostream>
#include <iomanip>
#include <string>
//#include <stdio.h>

using namespace std;
using std::cout;
using std::endl;
using std::cin;
using std::string;

//=========================================================
//FUNCTION PROTOTYPES - These always go above Main

void printArray(double[], int);
void userInputArray(double array[], int size);
int userInputDriver(std::string[], int size);
int userInputCarColor(std::string[], int size);
//int getMin() {return seconds / 60);
//int getSec() {return seconds % 60};
int userInputLapTime(std::string[], int size);
void printPosition(double array[], int size);


//==========================================================
//MAIN BLOCK - Functions get called from this location

int main()
{
    double carnumber[3];				
	string driver_name[3];
	string CarColor[3];
	string LapTime[3];
	double position[3];
	
	
    //carnumber[0] = 0;
    //carnumber[1] = 1;			//Can add a Welcome Message
    //carnumber[2] = 2;
	

    userInputArray(carnumber, 3);

   	userInputDriver(driver_name, 3);
	
	userInputCarColor(CarColor, 3);
	
	userInputLapTime(LapTime, 3);
	
	printPosition(position, 3);

}
//===========================================================
//DEFINE FUNCTIONS HERE
void printArray(double array[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << array[i] << endl;
    }
}

//Input Array for User to choose a Car Number 
void userInputArray(double array[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << "Choose a carnumber ? " << endl;			
        cin >> array[i];
	if (cin.fail()) {
		cout << "Sorry, that was not a valid input" << endl;		//This prints, but I need the program to end, when triggered.
		cin.clear();											
		cin.ignore();											
	}
  }
	
}	

//Input Array for the Drivers Name
int userInputDriver(std::string array[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << "What is the drivers name ? " << endl;			
        cin >> array[i];
		if (cin.fail()) {
		cout << "Sorry, that was not a valid input" << endl;		//This prints, but I need the program to end, when triggered.
		cin.clear();												
		cin.ignore();
			return 0;												
	}
  }
}	

//Input Array for the Car Color
int userInputCarColor(std::string arrray[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << "What color is the car ? " << endl;			
        cin >> array[i];
		if (cin.fail()) {
		cout << "Sorry, that was not a valid input" << endl;		//This prints, but I need the program to end, when triggered.
		cin.clear();												
		cin.ignore();
			return 0;
		}
	}
}


//Input Array for the LapTime
int userInputLapTime(std::string array[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << "Please enter the Lap Time ? " << endl;			
        cin >> array[i];
		if (cin.fail()) {
		cout << "Sorry, that was not a valid input" << endl;		//This prints, but I need the program to end, when triggered.
		cin.clear();											
		cin.ignore();
			return 0;
		}
    }
    
}

void printPosition(double array[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << array[i] << endl;
    }
}

//check if car1 came in first
{if(time1 < time2 && time1 < time3)
  {
    if(time1 < time2)  //if he did, then check to see where car2 and car3 placed
     {
		cout << car1 << color1 << " came in first with a score of " << time1 << endl;
        cout << car2 << color2 <<" came in second with a score of "<< time2 << endl;
        cout << car3 << color3 <<" came in third with a score of "<< time3 << endl;
     }
    else
     {
        cout << car1 << color1 << " came in first with a score of "<< time1 << endl;
        cout << car3 << color3 << " came in second with a score of "<< time3 <<endl;
        cout << car2 << color2 << " came in third with a score of "<< time2 << endl;
     }
  }
  {
// check if car2 came first
if(time2 < time1 && time2 < time3)
  {
    if(time1 < time3) //if he did, then check to see where car1 and car3 placed
     {
        cout << car2 << color2 << " came in first with a score of "<< time2 << endl;
        cout << car1 << color1 << " came in second with a score of "<< time1 << endl;
        cout << car3 << color3 << " came in third with a score of "<< time3 << endl;
     }
    else
     {
        cout << car2 << color2 << " came in first with a score of "<< time2 << endl;
        cout << car3 << color3 << " came in second with a score of "<< time3 << endl;
        cout << car1 << color1 << " came in third with a score of "<< time1 << endl;
     }
  }

  // check if car3 came first
if(time3 < time1 && time3 < time2)
  {
    if(time2 < time1) //if he did, then check to see where car1 and car2 placed
     {
        cout << car3 << color3 << " came in first with a score of "<< time3 << endl;
        cout << car2 << color2 << " came in second with a score of "<< time2 << endl;
        cout << car1 << color1 << " came in third with a score of "<< time1 << endl;
     }
    else
     {
        cout << car3 << color3 << " came in first with a score of "<< time3 << endl;
        cout << car1 << color1 << " came in second with a score of "<< time1 << endl;
        cout << car2 << color2 << " came in third with a score of "<< time2 << endl;

		return 0;
     }
  }
  }
}
//system("PAUSE");
  //  return EXIT_SUCCESS; 
Last edited on
line 31 : string LapTime[3]; looks like you want to use string to represent numbers and than later you want to compare those times. You should be using numbers for that like int, double or something else
line 47 : you are using position as argument but its not defined anywhere.
line 98 : you named function argument as arrray (3r) but using it as array (2r)
line 135, 152, 169 looks like you want to name your functions if. Wrap that if around with void checkTimes(int time1, int time2, int time3); or something like that. (assuming u wont use strings for times anymore)
(113): error C2955: 'std::array': use of class template requires template argument list


You have using namespace std; so when you use a variable called array, the compiler thinks it's a std::array which is a template. Somehow or somewhere it has included the array header, which one normally needs to do to get this error. So, don't have using namespace std; just put std:: before each std thing. And array is not a good name for a variable IMO.

First of all, you don't need lines 8-11. When you input the header file "iostream," you're allowing your program to access a library of functions. This particular library holds cin, cout, endl, etc. Then, you're importing "string," which holds the string class. So no need for line 11. I imagine you have a reason for using "iomanip," assuming "cin.fail()" is part of that library.

Anyways, if you look at line 95 (after deleting lines 8-11), where you create the "printPosition function," you have closing braces before all of those if statements. This means that those if statements aren't within the function you seem to be wanting them in. Also, you may want to consider inserting the if statements within your FOR LOOP on line 97.

Third, your program seems to be catching a bunch of errors where car1, car2, car3, etc. are undeclared inside that function. I recommend reading up on scope if you haven't yet: http://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm

Lastly, you never set any of your time or car variables equal to user input. Therefore, the program is trying to print something that doesn't exist. That's your error.
For example: cin >> time1;
But as a matter of fact, by your code's logic, you will have to set the array[i] equal to one of your variables and you might have conversion issues.

Hope this helps.
Last edited on
Thanks everyone. I will implement these changes and let you know how it goes. My submission for class is late, but I really want to know how to do this correctly.

Hi Everyone, I've updated my code and now I'm getting the following errors:

(97): error C2447: '{': missing function header (old-style formal list?)
(116): error C2447: '{': missing function header (old-style formal list?)
(134): error C2447: '{': missing function header (old-style formal list?)
(153): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(153): error C2365: 'system': redefinition; previous definition was 'function'

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


using namespace std;
using std::cout;
using std::endl;
using std::cin;
using std::string;

//=========================================================
//FUNCTION PROTOTYPES - These always go above Main

void printArray(string[], int);
void userInputArray(string array[], int size);
int userInputDriver(std::string[], int size);
int userInputCarColor(std::string[], int size);
//int getMin() {return seconds / 60);
//int getSec() {return seconds % 60};
int userInputTime(string array[], int size);



//==========================================================
//MAIN BLOCK - Functions get called from this location

int main()
{
    //double carnumber[3];				
	string driver_name [3];
	string CarColor[3];
	string Time[3];
	//double position[3];
	
	//Notes: Create a string array 
	string car1[3];
	string car2[3];
	string car3[3];
	string time1[3];
	string time2[3];
	string time3[3];
	
	

    userInputArray(car1, 3);
	userInputArray(car2, 3);
	userInputArray(car3, 3);
	userInputArray(time1, 3);
	userInputArray(time2, 3);
	userInputArray(time3, 3);
	printArray(car1, 3);
	printArray(car2, 3);
	printArray(car3, 3);
	
   	}
//===========================================================
//DEFINE FUNCTIONS HERE
void printArray(string array[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << array[i] << endl;
    }
}

//Input Array for User to choose a Car Number 
void userInputArray(string array[], int size)
{
   
        cout << "Choose a carnumber ? " << endl;			
        cin >> array[0];
		
		cout << "Choose a carcolor ? " << endl;			
        cin >> array[1];
		
		cout << "Choose a laptime ? " << endl;			
        cin >> array[2];
}
		
	
	//Create a function that will take the 3 strings and 
	

{ //check if car1 came in first
if(time1 < time2 && time1 < time3);
  {
    if(time1 < time2)  //if he did, then check to see where car2 and car3 placed
     {
		cout << car1 << color1 << " came in first with a score of " << time1 << endl;
        cout << car2 << color2 <<" came in second with a score of "<< time2 << endl;
        cout << car3 << color3 <<" came in third with a score of "<< time3 << endl;
     }
    else
     {
        cout << car1 << color1 << " came in first with a score of "<< time1 << endl;
        cout << car3 << color3 << " came in second with a score of "<< time3 <<endl;
        cout << car2 << color2 << " came in third with a score of "<< time2 << endl;
     }
  }
}


{  //check if car2 came first
if(time2 < time1 && time2 < time3)
  {
    if(time1 < time3) //if he did, then check to see where car1 and car3 placed
     {
        cout << car2 << color2 << " came in first with a score of "<< time2 << endl;
        cout << car1 << color1 << " came in second with a score of "<< time1 << endl;
        cout << car3 << color3 << " came in third with a score of "<< time3 << endl;
     }
    else
     {
        cout << car2 << color2 << " came in first with a score of "<< time2 << endl;
        cout << car3 << color3 << " came in second with a score of "<< time3 << endl;
        cout << car1 << color1 << " came in third with a score of "<< time1 << endl;
     }
  }
}

{  // check if car3 came first
if(time3 < time1 && time3 < time2)
  {
    if(time2 < time1) //if he did, then check to see where car1 and car2 placed
     {
        cout << car3 << color3 << " came in first with a score of "<< time3 << endl;
        cout << car2 << color2 << " came in second with a score of "<< time2 << endl;
        cout << car1 << color1 << " came in third with a score of "<< time1 << endl;
     }
    else
     {
        cout << car3 << color3 << " came in first with a score of "<< time3 << endl;
        cout << car1 << color1 << " came in second with a score of "<< time1 << endl;
        cout << car2 << color2 << " came in third with a score of "<< time2 << endl;

		return 0;
     }
  }
}
    system("PAUSE");
    return EXIT_SUCCESS;


void myfunc()
{
	cout << "Thanks for playing Car Race" << endl;
}
	

Hi,

You still have your if statements from line 85 onwards outside a function. Have a look at the tutorial at the top left of this page.

Good Luck!!
@TheIdeasMan...Yes, I would like some assistance with creating the function. Thanks for your response.
TheIdeasman wrote:
Have a look at the tutorial at the top left of this page.


How did you get on with that?

You already have functions in your code: So why is there a problem?
@TheIdeasMan...I read about the Local and Global Variables. I guess I'm making this much harder than it has to be. I'll keep at it.
You cant make functions like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{  // check if car3 came first
if(time3 < time1 && time3 < time2)
  {
    if(time2 < time1) //if he did, then check to see where car1 and car2 placed
     {
        cout << car3 << color3 << " came in first with a score of "<< time3 << endl;
        cout << car2 << color2 << " came in second with a score of "<< time2 << endl;
        cout << car1 << color1 << " came in third with a score of "<< time1 << endl;
     }
    else
     {
        cout << car3 << color3 << " came in first with a score of "<< time3 << endl;
        cout << car1 << color1 << " came in second with a score of "<< time1 << endl;
        cout << car2 << color2 << " came in third with a score of "<< time2 << endl;

		return 0;
     }
  }
}


You have correct functions in your code thats why TheIdeasMan told you
You already have functions in your code: So why is there a problem?


Make them like this
return_type function_name(Argument_type1 arg, ...){functions_body}
An example :
1
2
3
4
5
6
7
8
9
10
void printArray(string array[], int size)
/*return type  void
   function_name printArray
   comma separated arguments string array[], int size*/
{
    for(int i = 0; i < size; i++)
    {
        cout << array[i] << endl;
    }
}
Last edited on
@TheIdeasMan

It's almost as if he totally ignored our tips in the first few comments... lol
Hi Guys, thank you so much for your assistance here.
Topic archived. No new replies allowed.