how to convert an int to a char C++ -
im having trouble assignment have convert 3 variables of clock (int hour, int minutes, , bool afternoon) converted string in method. tried converting int char , replacing each string char. function suppose return t/f if conversion worked or not. here have far:
class time { private: int hour; int minutes; bool afternoon; public: void sethour(int hr); void setminutes(int min); void setafternoon(bool aft); int gethour(); int getminutes(); bool getafternoon(); bool setasstring(string time); string getasstring(); time(void); ~time(void); };
and
bool time::setasstring(string time){ char min = minutes; char hr = hour; char hr[0] = time[0]; char hr[1]= time[1]; char min[0] = time[3]; char min[1] = time[4]; char afternoon = time[6]; if ((hourtens > 1) || (mintens > 5)) { return false; } else { return true; } } string time::getasstring(){ return false; }
it straight-forward in fact, may involve little twist in mind @ first.
i not going give actual code, snippet that, if can understand them, should able solve problem yourself:
what want converting integer string/char. basic thing need convert single digit int corresponding char.
// example have such integer int = 3; // , want convert char char c = '3';
what need is, adding '0'. reason why works because '0' means integer value of 48. '1'..'9' means 49..57. simple addition find out corresponding character single decimal digit integer:
i.e. char c = '0' + i;
if know how convert single decimal digit int char, what's left how can extract individual digit more-than-one-decimal-digit integer
it simple math making use of / , %
int = 123 % 10; // give u last digit, 3 int j = 123 / 10; // give remove last digit, 12
the logic left homework need then.
Comments
Post a Comment