Trying to convert int to string

Posted 19 September 2012 - 07:53 AM

Hi,

I am trying to work on a section of code that will convert an integers (0-99) into a string. My problem is I have never coded before so to you guys this will look like garbage, apologies. I have looked at numerous examples and cannot seem to figure it out? This is what I have so far.

The sprintf() function only works with C-strings, not std::string. While you can convert to and from a std::string it is considered wasteful in a C++ program. The proper C++ way it to use stringstreams to convert things to std::strings.

Next please post the entire definition of your class. You seem to have quite a few variables in your code that have not been defined, so I need to see how and where all the variables in your function are defined.

Re: Trying to convert int to string

Posted 19 September 2012 - 08:18 AM

The whole premise of this code is to view an input of a song title and it's playtime. I think I have the add, printTime, and normalize functions built correctly. As for the "twoDigits" code, I just ran this through the compiler and it made it without error:

That is what the code orginally looked like. My job is to "pretty it up" along with the previously mentioned add, normalize, and printTime functions. Once done so to have them called in the main() so it works neatly. If you would care for it I could send you what the total before code and my total after code looks like (all functions included).

Re: Trying to convert int to string

Posted 19 September 2012 - 09:00 AM

Do you even need to convert the integers to strings? It looks like all you needed to do was output, why not just use the setw() and setfill() manipulators? You can even use the setw() to do the padding for the title.

Re: Trying to convert int to string

Posted 19 September 2012 - 09:04 AM

Skydiver, on 19 September 2012 - 09:00 AM, said:

Do you even need to convert the integers to strings? It looks like all you needed to do was output, why not just use the setw() and setfill() manipulators? You can even use the setw() to do the padding for the title.

Re: Trying to convert int to string

Posted 19 September 2012 - 09:08 AM

Quote

The answer to the comment question can be seen here:

But the code you posted in post 10 doesn't have anything to do with the function you posted in post 8. The function in post #8, as presented, is not doing anything and should produce warnings when compiled, because you told the compiler you would be returning something when you don't actually return anything.

Quote

If you would care for it I could send you what the total before code and my total after code looks like (all functions included).

If your program is producing the output you desire and you don't have any further questions posting your code is not necessary. But if your program is not producing the desired output and you have further questions then I suggest that you post the complete relevant code and ask specific questions.

Jim

This post has been edited by jimblumberg: 19 September 2012 - 09:09 AM

Now my after code doesn't do jack squat. My questions would be as follows:
1. Have I done WAY more manipulating than necessary?
2. I understand that in after code, I must "call" the above functions to it to make anything happen. I just have no idea how to do that.
3. Am I right in these thoughts (regarding before code to be manipulated):
I. lines 9-22 remain untouched in main().
II. lines 24-30 are the ones that need to be changed for the normalize function.
III. lines 32-54 should be the twoDigits function
IV. lines 56-68 are for function add.
V. lines 69-77 are for function printTime
4. End goal is indeed output in the format H:MM:SS Song Title, and after user types ctrl-z....Total H:MM:SS