-2

I have a homework - I must to create script with time of broadcasting. Broadcasting is when time is for example 12:21 or 02:20 - just the same from behind and from the begin too. I want that time writes in HH:MM format. All works good but up to one thing.. When time < 10 it not was in HH:MM format but is in H:MM format example - 04:40 outputs as 4:40 - It's a problem.

#include <iostream>
#include <string>
#include <queue>
#include <tuple>
#include <limits>

int main() {
std::cout << "Give me a number: " << std::endl;
int c;
std::cin >> c;

if (c < 1) {
    std::cout << "Incorrect!." << std::endl;

    main();
}
else if (c > 2000) {
    std::cout << "Incorrect!" << std::endl;

    main();
}
else {
    std::cout << "Correct value." << std::endl;

    std::queue<std::tuple<int, int>> casy;

    for (int i = 0; i < c; i++) {
        std::cout << "Give me a time number. " << i << " (HH:MM): ";
        int hour, minute;
        char c; 
        std::cin >> hour >> c >> minute;
        std::cin.clear();
        std::cin.ignore(10000, '\n');

        /* Uložení času do fronty [std::queue] */
        casy.push(std::make_tuple(hour, minute));
    }

    for (int u = 0; u = times.size(); u++) {
        int broadcastingTimes[15][2] = { { 01, 10 },{ 02, 20 },{ 03, 30 },{ 04, 40 },{ 05, 50 },{ 10, 01 },{ 11, 11 },{ 12, 21 },{ 13, 31 },{ 14, 41 },{ 15, 51 },{ 20, 02 },{ 21, 12 },{ 22, 22 },{ 23, 32 } };
        int actuallyBroadcastingIndex = 0;

        for (int o = 0; o < 15; o++) {

            if (std::get<0>(times.front()) == broadcastingTimes[o][0]) {
                if (std::get<1>(times.front()) <= broadcastingTimes[o][1]) {

                    /*if(o != 0) {
                    if(std::get<0>(times.front()) > broadcastingTimes[o-1][0]) {
                    if(std::get<0>(times.front()) > broadcastingTimes[o-1][1]) {
                    actuallyBroadcastingIndex = o;
                    } else {
                    actuallyBroadcastingIndex = o-1;
                    }
                    } else {
                    actuallyBroadcastingIndex = o;
                    }
                    } else {
                    actuallyBroadcastingIndex = o;
                    }*/


                    actuallyBroadcastingIndex = o;
                }
                else {
                    actuallyBroadcastingIndex = o + 1;
                }

                if (std::get<0>(times.front()) == 23) {
                    if (std::get<1>(times.front()) > 32) {
                        actuallyBroadcastingIndex = 0;
                    }
                }
            }
            else if (std::get<0>(times.front()) > broadcastingTimes[o][0] & std::get<0>(times.front()) < broadcastingTimes[o + 1][0]) {
                //std::cout << std::get<0>(casy.front()) << "/" << broadcastingTimes[o][0] << "\n";

                actuallyBroadcastingIndex = o + 1;
            }

        }

        int hourInput = std::get<0>(times.front());
        std::string timeInputedByUser = std::to_string(hourInput) + ":" + std::to_string(std::get<1>(times.front()));
        std::string broadcastingTimeActually = std::to_string(broadcastingTimes[actuallyBroadcastingIndex][0]) + ":" + std::to_string(broadcastingTimes[actuallyBroadcastingIndex][1]);

        //std::cout << "sss" <<  broadcastingTimes[actuallyBroadcastingIndex][0] << "\n";
        std::cout << "Nearest broadcasting from " << timeInputedByUser << " will be at " << broadcastingTimeActually << std::endl;

        casy.pop();
        std::cin.get();
    }

}

return 0;
}

Expected results are: if nearest broadcasting time will be 04:40 - it will be outputs as 04:40 - Not 4:40 as now. If nearest broadcasting time is 14:41, all works alright..

  • 7
    Never ever call `main` yourself. It's not allowed in C++ at all. Use *loops*. – Some programmer dude Jan 20 '19 at 14:48
  • 1
    [`std::setw(2)`](https://en.cppreference.com/w/cpp/io/manip/setw) – Yksisarvinen Jan 20 '19 at 14:49
  • 1
    Also reconsider your variable naming... Like for example you have the loop `for (int i = 0; i < c; i++)`, then inside the loop you define a new variable named `c` with a different type. That's mighty confusing. Good variable naming is very important for the readability of the code. – Some programmer dude Jan 20 '19 at 14:49
  • what _times_ and _aktualniVysilaniIndex_ are ? – bruno Jan 20 '19 at 14:53
  • Out of the problem my father was radioamateur (F9RP) – bruno Jan 20 '19 at 14:57
  • 1
    Possible duplicate: [Print leading zeros with C++ output operator?](https://stackoverflow.com/q/530614/7571258) – zett42 Jan 20 '19 at 15:03
  • Your program now has a stack overflow if somehow `cin` gets hooked to the file system and the file consists of a million `0`'s. Don't call `main()`. – PaulMcKenzie Jan 20 '19 at 15:16
  • @bruno variables, sorry, I have really variables in my language (Czech), and i tried to translate variables to english for better overview – Dawe grass Jan 20 '19 at 15:19

1 Answers1

0

Using stringstream allow you to set the the width of the string following number, and set the filling character:

std::stringstream ss;
ss.fill('0');

ss << std::setw(2) << hourInput << ":";
ss << std::setw(2) << std::get<1>(casy.front());

std::string timeInputedByUser = ss.str();

ss.str(std::string()); // clear the buffer
ss << std::setw(2) << broadcastingTimes[actuallyBroadcastingIndex][0] << ":";
ss << std::setw(2) << broadcastingTimes[actuallyBroadcastingIndex][1];

std::string broadcastingTimeActually = ss.str();

std::cout << "Nearest broadcasting from " << timeInputedByUser << " will be at " << broadcastingTimeActually << std::endl;

By the way, you may use stringsteam instead of string opearator+ for formatting.

OdedR
  • 133
  • 7