Calculating

I have been learning about C++ for a couple of weeks, and I have done the most basic operators so far but now I am stumped. I get information on a couple of things for cars and input different lap times. I need to see which of the group times is the fastest and slowest. If anyone could explain how I could get that result it would be much appreciated. I'd like an example if anyone could come with one so I can try and implement into my program. Thank you very much!
What are you asking exactly?
You state that you have the lap times.

One can use ">" or "<" to compare time values and in so doing, determine which are the fastest and slowest times.

What, exactly, stumps you?

You say you'd like an example.

So would we.

Give us an example of what you've written, in your attempt to address your problem. People can then advise you on where you may have erred, and give you some direction as to how your code could be modified.

If your code is properly commented and formatted, well, that'd be pretty much ideal.

You're very welcome.
Last edited on
I am getting a lot of errors for this but here it is.

#include <stdafx.h>
#include <windows.h>
#include <winuser.h>
#include <iostream>



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

int main()
{
int driver;
int color;
int number;
int laptime1 == x;
int laptime2 == y;
int laptime3 == z;



// Car1 Info
cout << "Who is the driver for car one?";
cin >> driver;

cout << "What is the color for car one?";
cin >> color;

cout << "What is car one's number?";
cin >> number;

cout << "What was car one's laptime?"; // format MM:SS
cin >> laptime1;


// Car2 Info
cout << "Who is the driver for car two?";
cin >> driver;

cout << "What is the color for car one?";
cin >> color;

cout << "What is car two's number?";
cin >> number;

cout << "What was car two's laptime?";
cin >> laptime2;


// Car3 Info
cout << "Who is the driver for car one?";
cin >> driver;

cout << "What is the color for car one?";
cin >> color;

cout << "What is car one's number?";
cin >> number;

cout << "what is car one's laptime?";
cin >> laptime3;



if (x <= y <= z)
then (x y z == 1);
else (x y z == 0);
endl;

return 0;
}

my main thing is trying to figure out which of the lap times was fastest when inputted by the user and how to get the proper output. I know the IFTHENELSE statement I have is wrong but its just to have an example there of what I am looking for. If I need to clarify anymore let me know. Thanks again!
Note:
I'm not very familiar with C++ syntax.
Because of that lap time needs to be in format "M S" for now.

1
2
3
int laptime1 == x;
int laptime2 == y;
int laptime3 == z;

== is equality operator, to assign some value you need to use = operator.
x, y and z varables are not declared and initialised, so you can't assign them to other variables.
And you don't need them at all.

"Who is the driver for car one?"
"What is the color for car one?"
int data type is for integer numbers only.
To store a word or sentence - answers to this questions - you need to use string data type.
I think that you can use getline(cin, variable); to read a string from the user. But if you are using cin before getline you need to put cin.ignore() after cin because if you don't put that, program will just skip getline function.

You are overwriting previous inputs when entering a new one.
You need to use different variables for each car.

You can convert all minutes to seconds and then compare lap times.

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
#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 Info
    cout << "Who is the driver for car one? ";
    getline(cin, driver1);
    cout << "What is the color for car one? ";
    getline(cin, color1);
    cout << "What is car one's number? ";
    cin >> number1;
    cout << "What was car one's laptime? "; // format M:S
    cin >> laptimeM1 >> laptimeS1;
    cin.ignore();

    // Car2 Info
    cout << endl << "Who is the driver for car two? ";
    getline(cin, driver2);
    cout << "What is the color for car two? ";
    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 Info
    cout << endl << "Who is the driver for car three? ";
    getline(cin, driver3);
    cout << "What is the color for car three? ";
    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;

    cout << endl << "Slowest: "<< 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
Topic archived. No new replies allowed.