Can someone help me out with this C++ code? Watch

solaplusplus
Badges: 0
Rep:
?
#1
Report Thread starter 8 years ago
#1
Code:
#ifndef TIME1_H
#define TIME1_H

class Time {
      
      public: 
      Time();
      void setTime (int, int, int);
      void printmilitary();
      void printstandard();
      private:
      int hour;
      int minute;
      int second;
      };
      #endif
 
#include <iostream>      
#include "Time1.h"

using namespace std;
Time::Time () { 
           hour = minute = second;
           }
           
void Time::setTime(int h, int m, int s)
{
     hour = (h>= 0 && h< 24) ? h: 0;
     minute = (m>= 0 && m< 60) ? m: 0;
     second = (s>= 0 && s< 60) ? s: 0;

}

void Time::printmilitary()
{
     cout<< (hour< 10 ? "0" : "") << hour << ":"
         << ( minute < 10 ? "0" : "") << minute;
         }
void Time::printstandard()
{
     cout<<( (hour == 0 || hour == 12) ? 12 : hour %12)
     <<":" << (minute < 10 ? "0" :"")<< minute
     << ":" << ( second < 10 ? "0" : "" ) << second
     << (hour < 12 ? " AM " : " PM");
     
     }
     
     
     
     #include <iostream>
     #include "Time1.h"

using namespace std;
int main()
{
    Time t;
    cout<< "the initial military time is ";
    t.printmilitary();
    cout<< "the initial standard time is ";
    t.printstandard();
    
    t.setTime(13, 27, 26);
    cout<<"military time after set time is :";
    t.printmilitary();
    cout<< "the standard time after ste time is";
    t.printstandard();

    t.setTime(99, 99, 99);
cout<<" after attempt invalid settings\n military time:";
 t.printmilitary();
 cout<<"standard time";
 t.printstandard();
 cout<< endl;
 
    system("pause");
 return 0;
 
}
I keep getting the error : Time1.h: no such file or directory
1
reply
Wichu
Badges: 0
Rep:
?
#2
Report 8 years ago
#2
You do have this part:
Code:
#ifndef TIME1_H
#define TIME1_H

class Time {

public: 
Time();
void setTime (int, int, int);
void printmilitary();
void printstandard();
private:
int hour;
int minute;
int second;
};
#endif
saved in a file called 'Time1.h', right? Also, use [code] tags
1
reply
solaplusplus
Badges: 0
Rep:
?
#3
Report Thread starter 8 years ago
#3
Thanks for the suggestion. I didn't know I had to do that.
1
reply
Wichu
Badges: 0
Rep:
?
#4
Report 8 years ago
#4
The '#include "Time1.h"' line was looking for a file called "Time1.h" to include. Somehow I get the feeling you copy and pasted this code from somewhere rather than writing it yourself :P
0
reply
INTit
Badges: 17
Rep:
?
#5
Report 8 years ago
#5
Haha love the use of the ternary operator its ugly as sin
0
reply
Wichu
Badges: 0
Rep:
?
#6
Report 8 years ago
#6
I do like the ternary operator sometimes. But something I noticed in the code is that the output stream isn't being used that effectively. I haven't used standard I/O in a while, but IIRC, there's a way to specify the number of digits and fill character to be used when outputting numbers; this would replace the check for the hour/minute being less than 10. Also, to get the 12-hour time, I would use this:
( (hour < 13) ? hour : hour - 12)
instead of this:
( (hour == 0 || hour == 12) ? 12 : hour %12)
EDIT: Or not; for some reason I thought it would be a good idea to have 0AM :P
I guess you could go for:
((hour % 12 == 0) ? 12 : hour % 12)

Speaking of ternary operators, could someone tell me an alternative to them when assigning a reference to different variables depending on a condition? At the moment I'm using this:
T& reference = condition ? t1 : t2;
I can't remember whether you can create a reference without an object to refer to. I'm too lazy to check my C++ book :P
0
reply
roblee
Badges: 2
Rep:
?
#7
Report 8 years ago
#7
(Original post by Wichu)
I do like the ternary operator sometimes. But something I noticed in the code is that the output stream isn't being used that effectively. I haven't used standard I/O in a while, but IIRC, there's a way to specify the number of digits and fill character to be used when outputting numbers; this would replace the check for the hour/minute being less than 10. Also, to get the 12-hour time, I would use this:
( (hour < 13) ? hour : hour - 12)
instead of this:
( (hour == 0 || hour == 12) ? 12 : hour %12)
EDIT: Or not; for some reason I thought it would be a good idea to have 0AM :P
I guess you could go for:
((hour % 12 == 0) ? 12 : hour % 12)

Speaking of ternary operators, could someone tell me an alternative to them when assigning a reference to different variables depending on a condition? At the moment I'm using this:
T& reference = condition ? t1 : t2;
I can't remember whether you can create a reference without an object to refer to. I'm too lazy to check my C++ book :P
Sorry, a day after the fact but that hour-calculating code made me twitch a bit... I recommend ((hour+11)%12)+1 since conditionals hurt OOE on modern processors.

You can't create a reference without an object, that's the beauty of references. A bindless reference is a pointer (actually, so is a bound reference). The only way of creating a reference without explicit initialisation is to wrap it in a struct, which feels quite messy to me.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • Cranfield University
    Cranfield Forensic MSc Programme Open Day Postgraduate
    Thu, 25 Apr '19
  • University of the Arts London
    Open day: MA Footwear and MA Fashion Artefact Postgraduate
    Thu, 25 Apr '19
  • Cardiff Metropolitan University
    Undergraduate Open Day - Llandaff Campus Undergraduate
    Sat, 27 Apr '19

Have you registered to vote?

Yes! (381)
37.39%
No - but I will (78)
7.65%
No - I don't want to (71)
6.97%
No - I can't vote (<18, not in UK, etc) (489)
47.99%

Watched Threads

View All