Still learning in programming and this problem is course related. Although I am not asking for the answer I would appreciate some help and if anyone can point out what I might be doing wrong that would be awesome.
I was able to find other articles addressing the matter although I am still unable to compile...
Thank you for your time.
Issue:
- MyTime.cpp:88: error: ‘itoa’ was not declared in this scope
Program:
- MyTime
- I am currently trying to make a program that is not compiling because of the issue stated above.
- The program is to display the times that I specify in my driver program.
- Although in creating my string I am unable to compile.
Utilities:
- Mac OSX
- Compiling in terminal using g++
***MyTime.h***
#include <string>
#include <iostream>
using namespace std;
class MyTime
{
public:
// Constructors
MyTime();
MyTime(int h, int m, int s);
// Accessors
int getHour();
int getMinute();
int getSecond();
// Modifiers
void setMyTime(int h, int m, int s);
// Operations
MyTime add(MyTime t);
MyTime subtract(MyTime t);
MyTime add(int h);
// String
string toString(); // HH:MM:SS
private:
int hour;
int minute;
int second;
};
***MyTime.cpp***
#include "MyTime.h"
#include <cstdlib>
#include <iostream>
using namespace std;
// Constructors
MyTime::MyTime()
{
hour = 0;
minute = 0;
second = 0;
}
MyTime::MyTime(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
}
// Accessors
int MyTime::getHour()
{
return hour;
}
int MyTime::getMinute()
{
return minute;
}
int MyTime::getSecond()
{
return second;
}
/***
// Modifiers
void MyTime::setMyTime(int h, int m, int s)
{
}
***/
// Operations
MyTime MyTime::add(MyTime t)
{
MyTime result;
result.hour = hour + t.hour;
result.minute = minute + t.minute;
result.second = second + t.second;
return result;
}
MyTime MyTime::subtract(MyTime t)
{
MyTime result;
result.hour = hour - t.hour;
result.minute = minute - t.minute;
result.second = second - t.second;
return result;
}
/***
MyTime MyTime::add(int h)
{
}
***/
// String
string MyTime::toString() // HH:MM:SS
{
// Declare C-String
char hourCstr[10];
char minuteCstr[10];
char secondCstr[10];
// itoa(int, C-String)
itoa(hour, hourCstr, 10);
itoa(minute, minuteCstr, 10);
itoa(second, secondCstr, 10);
// Instantiate C++ string with C-String value
string hourCPstr(hourCstr);
string minuteCPstr(minuteCstr);
string secondCPstr(secondCstr);
return hourCPstr + ":" + minuteCPstr + ":" + secondCPstr;
}
***MyTimeTest.cpp***
#include "MyTime.h"
#include <iostream>
using namespace std;
int main()
{
MyTime t1(2, 25, 35);
MyTime t2(1, 40, 15);
cout << "Time 1: " << t1.toString() << endl;
cout << "Time 2: " << t2.toString() << endl;
MyTime sum = t1.add(t2);
cout << "The sum of both times: " << sum.toString() << endl;
cin.get();
return 0;
}