time and wrong input message

hi, the question says to get the current time and display a message if the input is incorrect.
i got the time but how do i get a message? im a beginner who knows almost nothing about it so could you please explain

#include<iostream>
#include<ctime>
using namespace std;
int main()
{
time_t tt;
struct tm * ti;
time (&tt);
ti = localtime(&tt);
cout << "Current time = "
<< asctime(ti);
return 0;
}
You didn't explain what the "input" is you're expecting.
Maybe you want to do stuff like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<ctime>

int main()
{
    
    std::string user_input, message = "hello", failed = "You failed.\n";
    
    std::cout << "Say 'hello' to me.\n";
    std::getline( std::cin, user_input );
    
    if( user_input != message )
    {    
        std::time_t tt = std::time( nullptr );
        std::tm * ti = std::localtime( &tt );
        std::cout << failed <<"Current time = " << std::asctime(ti) << '\n';
    }
    else
        std::cout << "Thank you.\n";
}
Hello fidarova,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



This is what your code could look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<ctime>

using namespace std;

int main()
{
    time_t tt;
    struct tm * ti;
    time(&tt);
    ti = localtime(&tt);

    cout << "Current time = " << asctime(ti);

    return 0;  // <--- Not required, but makes a good break point.
}

The blank lines really help in readability.

line 13 is short enough to just be 1 line, but what you did is fine. Your format works much better with 2 or more lines.

As Ganado said you did not explain what is good input and what is bad input. Nor does your program accept any user input.

This may help: http://www.cplusplus.com/forum/beginner/1/#msg6680
Also http://www.catb.org/~esr/faqs/smart-questions.html

I have found this to be useful first as a tutorial then as a reference https://www.learncpp.com/

Another good place for reference is: https://en.cppreference.com/w/

Your program is a good start, but I believe you need a "cin" to get some user input and then maybe and "if" statement to check with.

One part you will need to tell everyone is what your input will look like. A single word or multiple words separated by spaces.

Andy

Edit: typos.
Last edited on
thank you guys! i don't get what the wrong input is either, but the guestion asks for it, thats why i was so confused. thank you for all the help, i really appreciate it :)
maybe 'get the time' means 'from the user' ?? In which case you were supposed to (?? validate its format, and possibly validate that its say within 10 min of 'now' ??) This seems like an odd thing to do.
fidarova, sounds like you should post the assignment text if you want more help, otherwise we'll just be endlessly guessing.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <ctime>

int main()
{
	std::time_t tt;
	time(&tt);

	std::cout << "Current time = " << std::ctime(&tt);
}

Last edited on
Here's the text. Guess i totally misunderstood it after all

c) the current time is determined by the user in the form of hours and minutes. Determine what time of day it is now. If the value is incorrect, display a message;
To obtain the current hour and minute:

1
2
3
4
5
6
7
8
9
10
11
12
#include <chrono>
#include <ctime>
#include <iostream>

int main()
{
    const auto t_c = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
    const auto* t = std::localtime(&t_c);

    std::cout << "Hour: " << t->tm_hour << '\n';
    std::cout << "Minute: " << t->tm_min << '\n';
}


So the user is prompted to enter hour and minute. These are then compared to t->tm_hour and t->tm_min. If they are the same, OK. If not then display a message.
Hello fidarova,

So what have you done to correct your original program?

Andy
fidarova, forget your initial attempt at using time_t or localtime, etc. That's not what your question is about.

Your prompt is simpler than that. It's saying that the current time is determined by user input. It's saying that the user is entering the hour and minutes, and you have to determine the "time of day" based on that.

Your prompt is still a bit ambiguous:
- 24-hour clock? 12-hour clock?
- Is entering the : to separate hour from minute necessary, allowed, or disallowed?
- What does "time of day" mean? Does it just mean "morning", "afternoon"?

Overall, we don't have enough information. But it could be as simple as:
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
cout << "Enter current hour followed by the current minute (ex: 13 49): ";
int hour;
int minute;

if (!(cin >> hour >> minute))
{
    cout << "Invalid time!";
    return 1;
}

if (hour < 0 || hour > 23 || minute < 0 || minute > 59)
{
    cout << "Invalid time!";
    return 1;
}

if (hour < 12)
{
    cout << "Morning!\n";
}
else if (hour == 12 && minute == 0)
{
    cout << "Noon!\n";
}
else
{
    cout << "Afternoon!\n";
}

Last edited on
Topic archived. No new replies allowed.