Hey everyone! Thanks for all the help before and now I have another problem. So I'm doing a clock program with three "invalid" attached files. There are zero errors but still won't run the program, any advice out there?
pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster
What language? Explain the problem, this is vague.
Jazmine_1 0 Newbie Poster
Oh sorry I'm in c++ and I have the main file (cpp) and I need to connect three separate files(.h) to the main file. I have zero errors in each file but refuses to run the program, I'm super stuck.
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster
You need to post your code, or at least, a subset of it that reproduces the problem you have. We cannot help you if you don't provide sufficient information.
Jazmine_1 0 Newbie Poster
Ok its kinda long...
#ifndef H_invalidHr
#define H_invalidHr
#include "invalidMin.h"
#include "invalidSec.h"
#include<string>
#include<iostream>
using namespace std;
class invalidHr
{
public:
invalidHr()
{
message = "The value of hr must be between 0 and 12.";
}
invalidHr(string str)
{
message = str + "Invalid input.";
}
string what()
{
return message;
}
public:
string message;
};
#endif
#ifndef H_invalidMin
#define H_invalidMin
#include<string>
#include<iostream>
using namespace std;
class invalidMin
{
public:
public:
invalidMin()
{
message = "Enter minutes: ";
}
invalidMin(string str)
{
message = str + "Invalid input.";
}
string what()
{
return message;
}
public:
string message;
};
#endif
#ifndef H_invalidSec
#define H_invalidSec
#include<string>
#include<iostream>
using namespace std;
class invalidSec
{
public:
invalidSec()
{
message = "Enter seconds: ";
}
invalidSec(string str)
{
message = str + "Invalid input.";
}
string what()
{
return message;
}
public:
string message;
};
#endif
#include <iostream>
#include <string>
#include "invalidHr.h"
#include "invalidMin.h"
#include "invalidSec.h"
Moschops 683 Practically a Master Poster Featured Poster
What I see here is three classes defined. One class named invalidMin, one named invalidHr and one named invalidSec.
I don't see any program to actually run, though. Where is your main function?
David W 131 Practically a Posting Shark
What are you trying to do?
Providing the spec's of the problem you were asked to solve
using classes
would help us to see what you are supposed to be doing.
I'm doing a clock program
What is your clock supposed to do in your program?
Another way to phrase it that might help to get things started ...
What is the input expected (as per the spec's) ?
What output is expected for that sample input ?
Edited by David W
Jazmine_1 0 Newbie Poster
#include <iostream>
#include <string>
#include "invalidHr.h"
#include "invalidMin.h"
#include "invalidSec.h"
using namespace std;
int getHours();
int getMinutes();
int getSeconds();
void print24HourTime(int hr, int min, int sec, string str);
int main ()
{
int hours;
int minutes;
int seconds;
string str;
hours = getHours();
minutes = getMinutes();
seconds = getSeconds();
cout << "Enter AM or PM: ";
cin >> str;
cout << endl;
cout << "24 hour clock time: ";
print24HourTime(hours, minutes, seconds, str);
system("pause");
return 0;
}
int getHours()
{
bool done = false;
int hr = 0;
do
{
try
{
cout << "Enter hours: ";
cin >> hr;
cout << endl;
if (hr < 0 || hr > 12)
throw invalidHr();
done = true;
}
catch (invalidHr hrObj)
{
cout << hrObj.what() << endl;
}
}
while (!done);
return hr;
}
int getMinutes()
{
bool done = false;
int min = 0;
do
{
try
{
cout << "Enter minutes: ";
cin >> min;
cout << endl;
if (min < 0 || min > 59)
throw invalidMin();
done = true;
}
catch (invalidMin minObj)
{
cout << minObj.what() << endl;
}
}
while (!done);
return min;
}
int getSeconds()
{
bool done = false;
int sec = 0;
do
{
try
{
cout << "Enter seconds: ";
cin >> sec;
cout << endl;
if (sec < 0 || sec > 59)
throw invalidSec();
done = true;
}
catch (invalidSec secObj)
{
cout << secObj.what() << endl;
}
}
while (!done);
return sec;
}
void print24HourTime(int hr, int min, int sec, string str)
{
if (str == "AM")
{
if (hr == 12)
cout << 0;
else
cout << hr;
cout << ":" << min << ":" << sec << endl;
}
else if (str == "PM")
{
if (hr == 12)
cout << hr;
else
cout << hr + 12;
cout << ":" << min << ":" << sec << endl;
}
}
Here's he main file. Is the header right?
David W 131 Practically a Posting Shark
You might like to see this edited logic for the 24 hour part ???
Note: if you added min and max values to the int takeIn function ...
then you would NOT need all the code in your exception classes
as all the error messages could be handled by the min.max bounds there
and the relevant errmsg passed.
// test_invalid_time.cpp //
#include <iostream>
#include <iomanip> // re. setw, setfill
#include <string>
#include <cctype> // re. toupper
#include "invalidHr.h"
#include "invalidMin.h"
#include "invalidSec.h"
using namespace std;
int getHours();
int getMinutes();
int getSeconds();
void print24HourTime(int hr, int min, int sec, const string& );
// used here to take in valid numbers //
template < typename T >
T takeIn( const string& msg, const string& errmsg = "\nValid numbers only please ...\n\n" )
{
T val;
while( true )
{
cout << msg << flush;
if( cin >> val && cin.get() == '\n' )
break;
else
{
cout << errmsg;
cin.clear(); // clear error flasgs
cin.sync(); // 'flush' cin stream ...
}
}
return val;
}
int takeInChr( const char* msg )
{
cout << msg << flush;
int c = cin.get();
cin.sync(); // 'flush' cin stream
return c;
}
bool more()
{
int c = takeInChr( "More (y/n) ? " );
if( c == 'n' || c == 'N' ) return false;
// else ...
return true;
}
int main ()
{
do
{
string str;
for( ; ; )
{
// do this part firstly ...
// so can see that following hr is AM/PM //
cout << "Enter AM or PM: ";
cin >> str;
cin.sync(); // 'flush' cin stream
if( str.size() == 2 )
{
str[0] = toupper(str[0]);
str[1] = toupper(str[1]);
if( str == "AM" || str == "PM" ) break;
}
//else ... if reach here ...
cout << "\nAM/am or PM/pm only please ...\n\n";
}
int hours = getHours();
int minutes = getMinutes();
int seconds = getSeconds();
cout << "24 hour clock time: ";
print24HourTime(hours, minutes, seconds, str);
}
while( more() );
}
int getHours()
{
bool done = false;
int hr = 0;
do
{
try
{
hr = takeIn< int >( "Enter hours 0..12: " );
if (hr < 0 || hr > 12)
throw invalidHr();
done = true;
}
catch (invalidHr hrObj)
{
cout << hrObj.what() << endl;
}
}
while (!done);
return hr;
}
int getMinutes()
{
bool done = false;
int min = 0;
do
{
try
{
min = takeIn< int >( "Enter minutes 0..59: " );
if (min < 0 || min > 59)
throw invalidMin();
done = true;
}
catch (invalidMin minObj)
{
cout << minObj.what() << endl;
}
}
while (!done);
return min;
}
int getSeconds()
{
bool done = false;
int sec = 0;
do
{
try
{
sec = takeIn< int >( "Enter seconds 0..59: " );
if (sec < 0 || sec > 59)
throw invalidSec();
done = true;
}
catch (invalidSec secObj)
{
cout << secObj.what() << endl;
}
}
while (!done);
return sec;
}
void print24HourTime(int hr, int min, int sec, const string& str)
{
if (str == "AM")
{
if (hr < 9 )
cout << 0;
cout << hr;
cout << setfill( '0' ) << ":" << setw(2) << min
<< ":" << setw(2) << sec << endl;
}
else // (str == "PM")
{
hr += 12;
if (hr == 24 )
cout << "00";
else
cout << hr;
cout << setfill( '0' ) << ":" << setw(2) << min
<< ":" << setw(2) << sec << endl;
}
}
Edited by David W
David W 131 Practically a Posting Shark
You also might like to see that using classes some times just adds code bloat.
Take a look (no classes) ...
// test_invalid_time_simple.cpp //
#include <iostream>
#include <iomanip> // re. setw, setfill
#include <string>
#include <cctype> // re. toupper
using namespace std;
void print24HourTime(int hr, int min, int sec, const string& );
// used here to take in valid numbers //
int takeInInt( const string& msg, int min, int max,
const string& errmsg = "\nIntegers only please ...\n\n" )
{
int val;
while( true )
{
cout << msg << flush;
if( cin >> val && cin.get() == '\n' )
{
if( val >= min && val <= max ) break;
//else ... if reach here ...
cout << "Valid input range here is: " << min << ".." << max << "\n";
}
else
{
cout << errmsg;
cin.clear(); // clear error flasgs
cin.sync(); // 'flush' cin stream ...
}
}
return val;
}
int takeInChr( const char* msg )
{
cout << msg << flush;
int c = cin.get();
cin.sync(); // 'flush' cin stream
return c;
}
bool more()
{
int c = takeInChr( "More (y/n) ? " );
if( c == 'n' || c == 'N' ) return false;
// else ...
return true;
}
int main ()
{
do
{
string str;
for( ; ; )
{
// do this part firstly ...
// so can see that following hr is AM/PM //
cout << "Enter AM or PM: ";
cin >> str;
cin.sync(); // 'flush; cin stream
if( str.size() == 2 )
{
str[0] = toupper(str[0]);
str[1] = toupper(str[1]);
if( str == "AM" || str == "PM" ) break;
}
//else ... if reach here ...
cout << "\nAM/am or PM/pm only please ...\n\n";
}
int hours = takeInInt( "Enter hours 0..12: ", 0, 12 );
int minutes = takeInInt( "Enter minutes 0..59: ", 0, 59 );
int seconds = takeInInt( "Enter seconds 0..59: ", 0, 59 );
cout << "24 hour clock time: ";
print24HourTime(hours, minutes, seconds, str);
}
while( more() );
}
void print24HourTime(int hr, int min, int sec, const string& str)
{
if (str == "AM")
{
if (hr < 9 )
cout << 0;
cout << hr;
cout << setfill( '0' ) << ":" << setw(2) << min
<< ":" << setw(2) << sec << endl;
}
else // (str == "PM")
{
hr += 12;
if (hr == 24 )
cout << "00";
else
cout << hr;
cout << setfill( '0' ) << ":" << setw(2) << min
<< ":" << setw(2) << sec << endl;
}
}
Edited by David W
Jazmine_1 0 Newbie Poster
You don't know how much I appreciate and actually understood. You are amazing!!! Thank you so much!
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.