4

Im having trouble with an assignment where I have to convert three variables of a clock (int hour, int minutes, and bool afternoon) to be converted to a string in a method. I tried converting the int to a char and then replacing each string with the char. The function is suppose to return T/F if the conversion worked or not. Here is what I have so 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; } 
6
  • Look up atoi and itoa functions Commented May 3, 2013 at 0:30
  • i forgot to mention that my professor prefers we do this without using functions like those. he wants it from scratch Commented May 3, 2013 at 0:33
  • Let me give you some hints, then: 1) What is '1' - '0'? What is 1 + '0'? 2) / 10 divided by 10, % 10 gets the remainder you'd get from dividing by 10 (e.g. whatever is in the units column) Now you know enough to solve the problem. Commented May 3, 2013 at 0:34
  • '1' - '0' = '1' and 1 + '0' = ascii code right? and as for the /10, im not sure how this helps me. I am new to c++. would doing this get me any closer to my goal, this is basically what out professor wants (swap a given int (eg. 09) into a given string (eg. 00:00 am), but i dont know exactly how to do it: int intday = 15; string strDay = "00"; char day = intday + '0'; strDay[1] = intDay[1]; Commented May 3, 2013 at 0:49
  • 1
    No. '1' - 0 is '1' but '1' - '0' is different. Commented May 3, 2013 at 0:50

2 Answers 2

14

it is straight-forward in fact, but may involve a little twist in mind at first.

I am not going to give you the actual code, but some snippet that, if you can understand them, you should be able to solve the problem by yourself:

What you want to do is converting an integer to a string/char. The most basic thing you need to do is to convert a single digit int to corresponding char.

// for example you have such integer int i = 3; // and you want to convert it to a char so that char c = '3'; 

what you need to do is, by adding i to '0'. The reason why it works is because '0' actually means an integer value of 48. '1'..'9' means 49..57. This is a simple addition to find out corresponding character for an single decimal digit integer:

i.e. char c = '0' + i;

If you know how to convert a single decimal digit int to char, what's left is how you can extract individual digit from a more-than-one-decimal-digit integer

it is simply a simple math by making use of / and %

int i = 123 % 10; // give u last digit, which is 3 int j = 123 / 10; // give remove the last digit, which is 12 

The logic left is the homework you need to do then.

Sign up to request clarification or add additional context in comments.

1 Comment

thankyou so much, i have been fiddling with this all week. for anyone else who finds this, I just did the following: `string time = "00:00; int minutes = 15; int tensMin = minutes / 10;// to get the tens digit of the minutes int onesMin = minutes % 10;// to get the ones digit of the minutes char tens = tensMin; char ones = onesMin; time[0] = tensMin;
0

Assuming you want to manually convert each char into an int, here is a very rough idea. Use a switch statement to convert each char into its ascii value. For example, switch(char_to_convert)'s case 38 would return 1. Basically, "1" converted to ASCII is 49(Thank you Ben Voigt for the correction). You don't actually need to convert it; you compiler will notice and convert it for you. Then you make the comparisons. See Ascii Table for a complete list.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.