Jump to content

C++ (Question) setprecision & if else if

Go to solution Solved by Mr_KoKa,
3 minutes ago, TheGosuStandard said:

I was trying to get the time between the range of 1-59 depending on current time (mins) that is retrieved from ctime  & sunset time (mins_set) which was a user input, thank you for clarifying that it cannot be both. 

 

I meant formatting output using iomnip based on the input placed, so if I placed 30 for minutes the output would display X:30 instead of X:3, unless I am not fully understanding formatting input vs output. 

You can use this to format your output whatever it is (strings, numbers):

#include <iostream>
#include <iomanip>

int main () {
  int hours, minutes;
  
  std::cin >> hours;
  std::cin >> minutes;
    
  std::cout << std::setfill('0') << std::setw(2) << hours << ":" << std::setfill('0') << std::setw(2) << minutes << std::endl;
  return 0;
}

Or you can format your time so it will  be HH:MM with http://www.cplusplus.com/reference/ctime/strftime/ function.

 

I don't know if this is what you want. But either of those will force leading zeros. Following zeros - I haven't heard anyone would need that cause it will never display 30 as 3, that is totally different number.

 

P.S.: Even simple check if number is  > 9 and if not adding 0 before would do.

Hello, I'm trying to figure out the remainder of a program having to deal with different time formats & I've seem to have gotten stuck (currently there are no tutors available).

 

I personally am not looking for answers, rather I would like guidance in terms of how to properly apply certain aspects of the code I have processed for the question. If any of these fall under the category of an "answers for homework" do not provide it as I know a lot of people do not like doing work for others & I would like to work on the code to better prepare myself. 

 

The parts I am currently getting stuck at include the time formatting of minutes where I either need to have a leading 0 for numbers > 10, while also having following 0's for numbers such as 10, 20, 30, etc. For example if I set the time to 5:30 it will output 5:3 & if I set the time to 5:03 it will also set time to 5:3.  I remember briefly that I can added fixed << showpoint < setprecision (2) for outgoing numbers, but unsure if it can be applied to both scenarios. 

 

The other issue that I am having is within the if else if statements of greeting a person based on the current time. I tested out the afternoon times that seemed to have worked for hours correctly, however including the morning & evening greetings seem to mess up (adding minutes into the problem didn't help either). 

 

I am kind of rusty on the topic leading me to think I might be either over thinking certain aspects or not applying enough to other parts. 

 

Question

Spoiler

 

*Write a program that outputs "Good morning <instructor>.", "Good afternoon <instructor>.", or "Good evening <instructor>." depending on whether the current time is midnight-noon, noon-sunset, or sunset-midnight respectively.  

Your program should first prompt the user for the sunset time with the following prompts, inputting data using cin (e.g., "cin >> cur_hour") after each prompt:

Enter the hours part of today's sunset time (1-12):
Enter the minutes part of today's sunset time (0-59):

 

Make sure that your program does error checking and re-prompts the user if needed. 

You will also need to retrieve the current time (24 hour format). A standard platform-independent way to do this is through the ctime library.

 

*Modify the above program as follow:

Output a sentence saying "It will be dark in <num> hours and <num> minutes.". Your program should handle all current times. So, if its after sunset, you should print the time until the following day's sunset. 

Add a prompt asking "How many minutes from now do you expect to be home?", and output a sentence saying "When you get home, it will be {dark,light}". You may assume that the following sunrise has not yet happened. 

My current code

Spoiler

#include <iostream>
#include <ctime>
using namespace std;

int main()
{

    time_t t;
    struct tm*now;
    t = time(0);             // get current time
    now = localtime(&t);     // adjust for local timezone
    int hour = now->tm_hour; // retrieve current hour
    int mins = now->tm_min;   // retrieve current min

    cout << "Current time is " << hour << ":" << mins << endl; //Current time

    cout << "Enter the hours part of today's sunset time (1-12)" << endl;
    int hour_set;
    cin >> hour_set;

    while((hour_set < 1)||(hour_set > 12)) //Input Validation for hours
    {
        cout << "Please input the correct hours (0-12)" << endl;
        cin >> hour_set;
    }

    cout << "Enter the minutes part of today's sunset time (0-59)" << endl;
    int mins_set;
    cin >> mins_set;

    while((mins_set < 0)||(mins_set > 59)) //Input Validation for minutes
    {
        cout << "Please input the correct minutes (0-59)" << endl;
        cin >> mins_set;
    }

    cout << "Current sunset time is " << hour_set << ":" << mins_set << " PM" << endl;

    //WIP
    int sun_set = hour_set + 12;
    if (hour >= 1 && hour <= 12)
    {
       if(mins_set >= mins && mins <= mins_set)
        cout << "Good Morning, " << endl;
    }
    else if (hour >= 12 && hour <= sun_set)
    {
        if(mins_set >= mins && mins <= mins_set)
            cout << "Good Afternoon, " << endl;
    }
    else if (hour >= sun_set && hour <= 24)
    {
        if(mins_set >= mins && mins <= mins_set)
        cout << "Good Evening, " << endl;
    }

    //Hours until it becomes dark
    int dark_hr = sun_set - hour;
    int dark_min = mins_set - mins;
    if(sun_set >= hour)
    cout << "It will be dark in " << dark_hr << " hours & " << dark_min << " minutes" << endl;
    cout << endl;
    
    //Hours until the next sunset
    int next_hr = 25 - hour_set;
    int next_min = 60 - mins;
    if(hour >= sun_set)
    cout << "The following day's sunset will occur in " << next_hr << " hours & " << next_min << " minutes" << endl;


    return 0;


}

 

Desktop: Intel 4770k - 12GB Vengeance Pro 1866Mhz RAM - Asus Maximus VI Formula Mobo - Asus Strix 970 SLI - Cooler Master V850 PSU -  Nzxt Phantom 630 Case  - 1TB WD HDD - Samsung 840 Evo 250GB SSD - Nzxt Kraken X60 - 24" Asus VG248QE 1080p Monitor - Logitech G35 Headset -  G502 Proteus Core - Logitech G710+ Keyboard - Nzxt Hue - Windows 10

Link to comment
https://linustechtips.com/topic/540088-c-question-setprecision-if-else-if/
Share on other sites

Link to post
Share on other sites

I wonder how you are getting 5:3 entering 5 and 30, also if you can provide examples of input data, what is result and what the result should be.

 

What I see so far is that you have statement like if(mins_set >= mins && mins <= mins_set) which is equal to if(mins_set == mins)

Link to post
Share on other sites

4 hours ago, Mr_KoKa said:

I wonder how you are getting 5:3 entering 5 and 30, also if you can provide examples of input data, what is result and what the result should be.

 

What I see so far is that you have statement like if(mins_set >= mins && mins <= mins_set) which is equal to if(mins_set == mins)

I ended up switching the format of how the program utilizes the time as it was getting problematic dealing with both hours & minutes. 

 

I understand the point about the if statement you listed, but that would only be true if it was actual the same minute correct?

 

Reworked code

Spoiler

#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int main()
{
    time_t t;
    struct tm*now;
    t = time(0);             // get current time
    now = localtime(&t);     // adjust for local timezone
    int hour = now->tm_hour; // retrieve current hour
    int mins = now->tm_min;   // retrieve current min
  
    cout << "Current time is " << hour << ":" << mins << endl << endl; //Current time
  
    cout << "Enter the hours part of today's sunset time (1-12)" << endl; //Input time in 12 hour format
    int hour_set;
    cin >> hour_set;
  
    while((hour_set < 1)||(hour_set > 12)) //Input Validation for hours
    {
        cout << "Please input the correct hours (0-12)" << endl;
        cin >> hour_set;
    }
  
    cout << "Enter the minutes part of today's sunset time (0-59)" << endl;//Input minutes part of 12 hour format
    int mins_set;
    cin >> mins_set;
  
    while((mins_set < 0)||(mins_set > 59)) //Input Validation for minutes
    {
        cout << "Please input the correct minutes (0-59)" << endl;
        cin >> mins_set;
    }
  
    int time_min = (hour_set * 60) + mins_set; //Conversion of sunset time into minutes format
    int current_time = (hour * 60) + mins; //Conversion of current time into minutes format
  
    //Greets person depending of time of day
    int sun_set = time_min + 720;
    if (current_time >= 0 && current_time <= 720) //Compares current time to the interval of midnight to noon in minutes format
        cout << "Good Morning," << endl << endl;
    if (current_time >= 720 && current_time <= sun_set) //Compares current time to the interval of noon to sunset in minutes format
        cout << "Good Afternoon," << endl << endl;
    if (current_time >= sun_set && current_time <= 1440) //Compares current time to the interval of sunset to midnight in minutes format
        cout << "Good Evening," << endl << endl;

    //Hours until the next sunset
    int next_set = current_time - (sun_set);
    int n_hours = next_set/60; //Conversion back to hours
    int n_mins = next_set % 60; //Conversion back to minutes per time format
    if(current_time > sun_set) //Compares current time to sunset time to determine following days sunset
        cout << "The following day's sunset will occur in " << n_hours << " hours & " << n_mins << " minutes" << endl << endl;
  
    //Hours until it becomes dark
    int dark_hr = sun_set - current_time;
    int d_hours = dark_hr/60; //Conversion back to hours
    int d_mins = dark_hr % 60; //Conversion back to minutes per time format
    if(sun_set > current_time)
        cout << "It will be dark in " << d_hours << " hours & " << d_mins << " minutes." << endl << endl;
  
    //Time to get home
    cout << "How many minutes from now do you expect to be home?" << endl;
    int time_home;
    cin >> time_home;
    int test_time = time_home + current_time; //Adds minutes till you get home to current time
    if (sun_set > test_time) //Tests if time to get home is earlier than sunset
        cout << "When you get home it will be light outside" << endl;
    if (sun_set < test_time) //Tests if time to get home is later than sunset
        cout << "When you get home it will be dark outside" << endl;

    return 0;

}

 

Run Test

Spoiler

Program Lab.png

 

I took out sunset time showing, but still included 24 hour time & as you can see it isn't showing as 1800, the hours being retrieved show it as "18", but the minutes show it as "0"

 

Also if anyone would know how to use the code tag within a spoiler/hidden content page that can help me figure it out that would be great, it doesn't seem to work. 

Desktop: Intel 4770k - 12GB Vengeance Pro 1866Mhz RAM - Asus Maximus VI Formula Mobo - Asus Strix 970 SLI - Cooler Master V850 PSU -  Nzxt Phantom 630 Case  - 1TB WD HDD - Samsung 840 Evo 250GB SSD - Nzxt Kraken X60 - 24" Asus VG248QE 1080p Monitor - Logitech G35 Headset -  G502 Proteus Core - Logitech G710+ Keyboard - Nzxt Hue - Windows 10

Link to post
Share on other sites

Double Post (Thought there was a delete reply button).

Desktop: Intel 4770k - 12GB Vengeance Pro 1866Mhz RAM - Asus Maximus VI Formula Mobo - Asus Strix 970 SLI - Cooler Master V850 PSU -  Nzxt Phantom 630 Case  - 1TB WD HDD - Samsung 840 Evo 250GB SSD - Nzxt Kraken X60 - 24" Asus VG248QE 1080p Monitor - Logitech G35 Headset -  G502 Proteus Core - Logitech G710+ Keyboard - Nzxt Hue - Windows 10

Link to post
Share on other sites

1 hour ago, TheGosuStandard said:

I understand the point about the if statement you listed, but that would only be true if it was actual the same minute correct?

I don't know what do you mean, but what I was saying about was, when you have:

  if(mins_set >= mins && mins <= mins_set)

there are two variables, mins and mins_set, and mins_set >= mins will be true only if mins_set will be greater or equal to mins, but at the same time it has to be less or equal, so both will give true only when the will be equal, cause it cannot be lesser and greater at the same time.

 

And about "outgoing numbers" do you mean formatting input rather than formatting output by iomnip?

Link to post
Share on other sites

27 minutes ago, Mr_KoKa said:

I don't know what do you mean, but what I was saying about was, when you have:


  if(mins_set >= mins && mins <= mins_set)

there are two variables, mins and mins_set, and mins_set >= mins will be true only if mins_set will be greater or equal to mins, but at the same time it has to be less or equal, so both will give true only when the will be equal, cause it cannot be lesser and greater at the same time.

 

And about "outgoing numbers" do you mean formatting input rather than formatting output by iomnip?

I was trying to get the time between the range of 1-59 depending on current time (mins) that is retrieved from ctime  & sunset time (mins_set) which was a user input, thank you for clarifying that it cannot be both. 

 

I meant formatting output using iomnip based on the input placed, so if I placed 30 for minutes the output would display X:30 instead of X:3, unless I am not fully understanding formatting input vs output. 

 

 

Desktop: Intel 4770k - 12GB Vengeance Pro 1866Mhz RAM - Asus Maximus VI Formula Mobo - Asus Strix 970 SLI - Cooler Master V850 PSU -  Nzxt Phantom 630 Case  - 1TB WD HDD - Samsung 840 Evo 250GB SSD - Nzxt Kraken X60 - 24" Asus VG248QE 1080p Monitor - Logitech G35 Headset -  G502 Proteus Core - Logitech G710+ Keyboard - Nzxt Hue - Windows 10

Link to post
Share on other sites

3 minutes ago, TheGosuStandard said:

I was trying to get the time between the range of 1-59 depending on current time (mins) that is retrieved from ctime  & sunset time (mins_set) which was a user input, thank you for clarifying that it cannot be both. 

 

I meant formatting output using iomnip based on the input placed, so if I placed 30 for minutes the output would display X:30 instead of X:3, unless I am not fully understanding formatting input vs output. 

You can use this to format your output whatever it is (strings, numbers):

#include <iostream>
#include <iomanip>

int main () {
  int hours, minutes;
  
  std::cin >> hours;
  std::cin >> minutes;
    
  std::cout << std::setfill('0') << std::setw(2) << hours << ":" << std::setfill('0') << std::setw(2) << minutes << std::endl;
  return 0;
}

Or you can format your time so it will  be HH:MM with http://www.cplusplus.com/reference/ctime/strftime/ function.

 

I don't know if this is what you want. But either of those will force leading zeros. Following zeros - I haven't heard anyone would need that cause it will never display 30 as 3, that is totally different number.

 

P.S.: Even simple check if number is  > 9 and if not adding 0 before would do.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×