PostHelpC++ HW

Posts 1–4 of 4 · Page 1 of 1
C++ HW
Prompt:
Write a program that asks the names of three runners and

the time it took each of them to finish a race. The program should display who came in first, second, and third place.

Input Validation: only accept positive integers for the times.



My try:
// ThreeNames.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
bool runner1;
bool runner2;
bool runner3;


cout << "What was the time of the first runner? " << endl;
//cingetline(runner1);
cin >> runner1;
cout << "What was the time of the second runner?" << endl;
cin >> runner2;
cout << "What was the time of the third runner? " << endl;
cin >> runner3;

// spacex5 = " ";
cout <<"Runners: "<<endl<< runner1<<endl << runner2<<endl << runner3<<endl;

if (runner1 > runner2 > runner3)
{
cout << "1st place: " << runner1 << endl;
cout << "2nd place: " << runner2 << endl;
cout << "3rd place: " << runner3 << endl;
}
else if (runner2 > runner3 > runner1)
{
cout << "1st place: " << runner2 << endl;
cout << "2nd place: " << runner3 << endl;
cout << "3rd place: " << runner1 << endl;
}
else if (runner3 > runner1 > runner2)
{
cout << "1st place: " << runner3 << endl;
cout << "2nd place: " << runner1 << endl;
cout << "3rd place: " << runner2 << endl;
}
else if(runner3 == runner2 == runner1)
{
cout << "All runners finished at the same time" << endl;
}
std::cout << "End of Code!\n";
return 0;

}



Thank you for helping out!
I have no currently no Compiler till the evening, so it's freestyle, but something like this:
Code:
#include <string>       // std::string
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

struct Racer 
{
    unsigned int index;
    unsigned int time;
    bool operator() (Racer& a, Racer& b) 
    { 
        return a.time < b.time;
    }
};

int main () 
{
    std::vector<Racer> racers;
    int buffer;
    const std::string runnerNames[] = 
    {
        "1st",
        "2nd",
        "3rd",
    };
    int count = 1;
    for (std::string& str : runnerNames) 
    {
        std::cout << "What was the time of the " << str << " runner? " << std::endl;
        std::cin >> buffer;
        racers.push_back({ count, buffer });
        count++;
    }
    std::sort(racers.begin(), racers.end(), Racer);
    count = 0;
    for (std::string& str : runnerNames) 
    {
        std::cout << str << " runner: " << racers[count].index << " with " << racers[count].time << std::endl;
        count++;
    }
    return 0;
}
Quote Originally Posted by MikeRohsoft View Post
I have no currently no Compiler till the evening, so it's freestyle, but something like this:
Code:
#include <string>       // std::string
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

struct Racer 
{
    unsigned int index;
    unsigned int time;
    bool operator() (Racer& a, Racer& b) 
    { 
        return a.time < b.time;
    }
};

int main () 
{
    std::vector<Racer> racers;
    int buffer;
    const std::string runnerNames[] = 
    {
        "1st",
        "2nd",
        "3rd",
    };
    int count = 1;
    for (std::string& str : runnerNames) 
    {
        std::cout << "What was the time of the " << str << " runner? " << std::endl;
        std::cin >> buffer;
        racers.push_back({ count, buffer });
        count++;
    }
    std::sort(racers.begin(), racers.end(), Racer);
    count = 0;
    for (std::string& str : runnerNames) 
    {
        std::cout << str << " runner: " << racers[count].index << " with " << racers[count].time << std::endl;
        count++;
    }
    return 0;
}
no worries and thank yu for sharing.
unfortunately, your code is out of the scope of my knowledge. I am barely learning C++ and I am unable to understand and/or integrate your code into my own.
sorry for being a noob :/
i just used the sort function from algorithm unit, you can watch out the documentary for it. It's not too complicated.
But the Struct i push into the array have 2 values, the index and the time, but we only want to sort it by time, so we had to write the function which makes sure it's greater then compare one by ourself. That we solved via the () operator, you also can write a seperate function for doing this.
Just check out the std::sort function in the docs
I did it, because i have to know which Index was it which is now sorted out
Posts 1–4 of 4 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Talk with us