简体   繁体   中英

finding the number of seconds since it last struck 12 when user inputs an hour, minute, and seconds (c++)

I'm trying to write a function that takes the time as three integer arguments (hours, minutes and seconds) and returns the number of seconds since the last time the clock “struck 12.” Use this function to calculate the amount of time in seconds between two times, both of which are within one 12-hour cycle of the clock.

This is the code I have and every time I compile it, it won't stop running.

#include <iostream>
using std::cout;
using std::cin;
int passedSeconds(int, int, int);
int difference(int, int, int);

int main()
{
    int hour1, minute1, second1;
    int hour2, minute2, second2;

    cout << "1. Enter hours, minutes and seconds (range 00:00:00 - 23:59:59): ";
    cin >> hour1 >> minute1 >> second1;

    while(hour1 > 23 || minute1 > 59 || second1 > 59)
    {
        if(hour1 < 23)
            cout << "\nEnter correct hours value!";
        if(minute1 < 59)
             cout << "\nEnter correct minutes value!";
        if(second1 < 59)
             cout << "\nEnter correct seconds value!";

        cout << "\n1. Enter hours, minutes and seconds: ";
        cin >> hour1 >> minute1 >> second1;
    }
    cout << "Passed seconds from last 12 (am or pm): " << passedSeconds(hour1, minute1,second1);

    cout << "\n2. Enter hours, minutes and seconds (range 00:00:00 - 23:59:59): ";
    cin >> hour2 >> minute2 >> second2;

    while(hour2 > 23 || minute2 > 59 || second2 > 59)
    {
        if(hour2 < 23)
            cout << "\nEnter correct hours value!";
        if(minute2 < 59)
            cout << "\nEnter correct minutes value!";
        if(second2 < 59)
            cout << "\nEnter correct seconds value!";

        cout << "\n2. Enter hours, minutes and seconds: ";
        cin >> hour2 >> minute2 >> second2;
    }
    cout << "\nDifference between two times is "
         << difference(hour2, minute2, second2) - difference(hour1, minute1, second1) 
         << " seconds.";

    return 0;
}

int passedSeconds(int hour, int minute, int second)
{
    if(hour >= 12)
        hour -= 12;

    return hour * 60 * 60 + minute * 60 + second;
}

int difference(int hour, int minute, int second)
{
    return hour * 60 * 60 + minute * 60 + second;
}

I tried compiling your code with the Visual studio compiler and with a GCC Compiler, and everything works fine here. One obvious bug in your code:

if(hour1 < 23)
    cout << "\nEnter correct hours value!";
if(minute1 < 59)
    cout << "\nEnter correct minutes value!";
if(second1 < 59)
    cout << "\nEnter correct seconds value!";

in the first and second while loop, you want to ouput an error message if your hours are INvalid. Now you display them if they are valid, so change your < to > .

Like I said, The program worked correctly, if I run it in a console, waiting for my input. However, if I ran it some online compilers, I noticed what you said. This was because I could not use the program as a console. Try giving in all data that you read in at the same time...

I went to CodeRunner's website and saw this screenshot: 程序输入

I don't know if you tried it, but try giving in the whole program input, before starting.. So something like this:

23 25 14
14 15 49

(note that this leaves no room for error checking and asking for new input. You will have to handle that in a different way...)

Sadly I could not test this in CodeRunner, since I don't own any OSX devices.

(I also could not respond as a comment, because I don't have enough points yet)

I hope it helped you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM