Hi guys.
I have finished one of the pointer exercises in my book and it works perfectly and I get the correct outputs, but could I have written it anymore efficiently? I have to use a struct with functions as shown below. I had to pass the object to the function which I did also. I know using a class would of been better but I was told to use a struct.
Here is my code:
Code:
#include <iostream>
// struct definition
struct Time
{
int m_Hour;
int m_Minute;
int m_Second;
// struct functions
void setTime( Time *t );
void getTime( Time *t );
};
// function to input the tume by user
void Time::setTime ( Time *t )
{
std::cout << "Enter the hour: ";
std::cin >> m_Hour;
std::cout << "\nEnter the minutes: ";
std::cin >> m_Minute;
std::cout << "\nEnter the seconds: ";
std::cin >> m_Second;
}
// function to display the time
void Time::getTime( Time *t )
{
std::cout << "Time is: " << m_Hour << " :: " << m_Minute << " :: "
<< m_Second << std::endl;
}
// main function - driver //////////////////////////////////////////////////////
//
int main(int argc, char *argv[])
{
Time *t = new Time;
t->setTime( t );
t->getTime( t );
delete t;
t = NULL;
std::cin.get(); // freeze console output window
std::cin.ignore();
return 0; // return value from int main
}