How to create Helper Function (Please Help!)

New to programming and C++. I need to incorporate a helper function in my code. One example is called: GetCar


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

using namespace std;
class Time;

int main()  //This is the Main Method



{
	
	string car1, car2, car3;
	string time1, time2, time3;
	string color1, color2, color3;
	cout << "Welcome Enter the name of the racers, their time, and car color.\n Seperate name and time with a space.";
	//Enter information for Car 1
	cout << "Who is driving Car 1?" << endl;
	cin >> car1;
	cout << "What is the time of Car 1?" << endl;
	cin >> time1;
	cout << "What is the color of Car 1?" << endl;
	cin >> color1;
	//Enter information for Car 2
	cout << "Who is driving Car 2?" << endl;
	cin >> car2;
	cout << "What is the time of Car 2?" << endl;
	cin >> time2;
	cout << "What is the color of Car 2?" << endl;
	cin >> color2;
	//Enter information for Car 3
	cout << "Who is driving Car 3?" << endl;
	cin >> car3;
	cout << "What is the time of Car 3?" << endl;
	cin >> time3;
	cout << "What is the color of Car 3?" << endl;
	cin >> color3;
	cin.clear();
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	
	//THIS PIECE OF CODE REQUESTS INPUT OF CRITERIA ALL ON SAME LINE
	//cout << "\n Enter the name of racer 1, time, and car color: ";
	//cin >> car1 >> time1 >> color1;
	//cout << " Enter the name of racer 2, time, and car color: ";
	//cin >> car2 >> time2 >> color2;
	//cout << " Enter the name of racer 3, time, and car color: ";
	//cin >> car3 >> time3 >> color3;
	//cin.clear();
	//cin.ignore(numeric_limits<streamsize>::max(), '\n');



{    //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;
}
Well you could create car class and create function that creates specific car like this
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
class Car{
    int time; //or if you create your class Time than -> Time time;
    string color; //u can create Color class or use -> class enum Color{yellow, red, ...};
    string drivers_name;
    
public:
    Car(const string& name, const string& c, int t) : time{t}, color{c}, drivers_name{name}{}
    
    string getName() const {return drivers_name;}
    string getColor() const {return color;}
    int getTime() const {return time;}
    
    //...
};

Car getCar()
{
    int time;
    string name, color;
    
    cout << "Who is driving Car?" << endl;
    cin >> name;
    cout << "What is the time of Car?" << endl;
    cin >> time;
    cout << "What is the color of Car?" << endl;
    cin >> color;
	
    return Car{name, color, time};        
}


Than you can use it like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    cout << "Welcome Enter the name of the racers, their time, and car color.\n Seperate name and time with a space.";

    cout << "\nEnter information about Car 1?" << endl;
    Car car1 = getCar();
	
    cout << "\nEnter information about Car 2?" << endl;
    Car car2 = getCar();
	
	//...
    cout << "car2 drivers name is " << car2.getName() << endl;

    return EXIT_SUCCESS;
}


note that you shouldn't use string to represent time, use at least int
Last edited on
Thank you soooo very much!!! Going to try this now. Also, would you know how I can represent the time as MM:SS?
I'm trying to incorporate the suggestions above into my code, but receiving errors. Am I supposed to take this piece and create a separate file and name it (i.e., car.h)?

class Car{
int time; //or if you create your class Time than -> Time time;
string color; //u can create Color class or use -> class enum Color{yellow, red, ...};
string drivers_name;

public:
Car(const string& name, const string& c, int t) : time{t}, color{c}, drivers_name{name}{}

string getName() const {return drivers_name;}
string getColor() const {return color;}
int getTime() const {return time;}

//...
};

Car getCar()
{
int time;
string name, color;

cout << "Who is driving Car?" << endl;
cin >> name;
cout << "What is the time of Car?" << endl;
cin >> time;
cout << "What is the color of Car?" << endl;
cin >> color;

return Car{name, color, time};
}




Then do a #include <car.h>?

Any help would be greatly appreciated. I'm a bit confused.
No you can just put this Car definition, getCar definiton somewhere in your file what you already using (probably before main function coz thats where you use it) You are probably getting errors because of stuff like this
1
2
3
4
5
6
    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;
     }


The rest of your code probably isnt fixed to be used together with Car and time1, time3, string car, color2 and stuff like that is not defined anymore. You have to adjust it to be compatible with Car like
1
2
3
4
5
 if(car1.getTime() < car2.getTime()) //if he did, then check to see where car1 and car3 placed
     {
        cout << car1.getDriver() << car1.getColor() << " came in first with a score of "<< 
        //...
     }


You didnt show those error messages so I really dont know what you did wrong there but I suppose this was it.

About your time class you can create class similar to car that holds its time in seconds for example. Then calculate how many minutes that is if needed for example
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
#include <iostream>

class Time{
    int seconds{0};   //time in sec
    
public:
    Time() = default;
    Time(int sec) : seconds{sec}{};
    Time(int min, int sec) : seconds{60* min + sec}{}
    
    int getMin(){return seconds / 60;}   //get how much full minutes it is (if 3 min 40 sec returns 3)
    int getSec(){return seconds % 60;}   //get seconds (if 3 min 40 sec return 40)
    int inSeconds(){return seconds;}     //get time represented in seconds (if 3 min 40 sec returns  3* 60 + 40)
    double inMinutes(){return (seconds / 60) + static_cast<double>(seconds % 60)/60;}
};


int main(){
    Time t{3,40}; //3 min 40 sec
    
    std::cout << "in minutes = " << t.inMinutes() << std::endl;
    std::cout << "in seconds = " << t.inSeconds() << std::endl;
    
    std::cout << "full minutes = " << t.getMin() << std::endl;
    std::cout << "seconds      = " << t.getSec() << std::endl;
    }

in minutes = 3.66667
in seconds = 220
full minutes = 3
seconds      = 40

Just an example
Hey but if you are new to classes and functions you should rather read some book about this stuff or as keskiverto suggested look at some tutorial . Looking at examples wont help you that much. You will also see that these easy examples that im giving you are not that hard to write :)
Last edited on
Topic archived. No new replies allowed.