Trying to convert int to string

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2891 Views - Last Post: 23 September 2012 - 12:55 PM Rate Topic: -----

#1 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

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.

// Pad the title to 40 characters
int twoDigits (string minStr, int min, string secStr, int sec, char title)
      {
      title = title + string(40, ' ');
      title = title.substr(0, 40);

      cout << title;
      cout << hrs << ':';
      int min = minStr;
      string minStr = static_cast<ostringstream*> & (ostringstream) << minStr) )->str();)/>
      string secStr;
      k = sec / 10;
      c = '0' + k;
      secStr += c;
      k = sec % 10;
      c = '0' + k;
      secStr += c;
      cout << secStr << endl;
      }



Now I receive the error messages:

In function 'int twoDigits(std::string, int, std::string, int, char)':
error: cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'char' in assignment
error: request for member 'substr' in 'title', which is of non-class type 'char'
error: 'hrs' was not declared in this scope
error: declaration of 'int min' shadows a parameter
error: cannot convert 'std::string' to 'int' in initialization
error: declaration of 'std::string minStr' shadows a parameter
error: expected '(' before '&' token
error: expected primary-expression before ')' token
error: expected ',' or ';' before ')' token
error: expected primary-expression before ')' token
error: expected ';' before ')' token
error: 'k' was not declared in this scope
error: 'c' was not declared in this scope
At global scope:
error: expected unqualified-id before 'return'
error: expected declaration before '}' token
Process terminated with status 1 (0 minutes, 0 seconds)
15 errors, 0 warnings

A little daunting for me lol. Any and all help would be appreciated. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to convert int to string

#2 no2pencil  Icon User is offline

  • Dic Head
  • member icon

Reputation: 5167
  • View blog
  • Posts: 26,850
  • Joined: 10-May 07

Re: Trying to convert int to string

Posted 19 September 2012 - 07:56 AM

I always cheat & use sprintf, putting the integers into a string :

char buffer[256]={0};
sprintf(buffer,"%i",int_variable);


Was This Post Helpful? 0
  • +
  • -

#3 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

Re: Trying to convert int to string

Posted 19 September 2012 - 08:00 AM

So in this case I would set it up like

char buffer [99]={0} //Because that is my range?
sprintf(buffer, "%i", int sec); //Then sub out sec for min and hrs


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: Trying to convert int to string

Posted 19 September 2012 - 08:04 AM

View Postno2pencil, on 19 September 2012 - 09:56 AM, said:

I always cheat & use sprintf, putting the integers into a string :

char buffer[256]={0};
sprintf(buffer,"%i",int_variable);



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.
Was This Post Helpful? 1
  • +
  • -

#5 no2pencil  Icon User is offline

  • Dic Head
  • member icon

Reputation: 5167
  • View blog
  • Posts: 26,850
  • Joined: 10-May 07

Re: Trying to convert int to string

Posted 19 September 2012 - 08:05 AM

View PostR2B Boondocks, on 19 September 2012 - 11:00 AM, said:

char buffer [99]={0} //Because that is my range?
sprintf(buffer, "%i", int sec); //Then sub out sec for min and hrs


You don't need the int infront of sec. The %i indicates it's an integer.

View Postjimblumberg, on 19 September 2012 - 11:04 AM, said:

While you can convert to and from a std::string it is considered wasteful in a C++ program.

I did indicate it was cheating.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3457
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: Trying to convert int to string

Posted 19 September 2012 - 08:07 AM

Typically, the way you fix C/C++ errors is start from the first error, fix it, recompile and then fix the next error that becomes the first error.

Your first error:

Quote

In function 'int twoDigits(std::string, int, std::string, int, char)':
error: cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'char' in assignment
error: request for member 'substr' in 'title', which is of non-class type 'char'

Is due to you treating the char parameter title as a string on lines 4-5.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: Trying to convert int to string

Posted 19 September 2012 - 08:11 AM

View PostR2B Boondocks, on 19 September 2012 - 09:53 AM, said:

Hi,

I am trying to work on a section of code that will convert an integers (0-99) into a string.


Please explain the following line:
      string minStr = static_cast<ostringstream*> & (ostringstream) << minStr) )->str();)/>

What exactly are you trying to do in this line?

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.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

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:

// Pad the title to 40 characters
int twoDigits (char sec, char min, char hrs)
      {
          char buffer [99] = {0};
          sprintf (buffer, "%i", sec);
          sprintf (buffer, "%i", min);
          sprintf (buffer, "%i", hrs);
      }


Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: Trying to convert int to string

Posted 19 September 2012 - 08:23 AM

Quote

As for the "twoDigits" code, I just ran this through the compiler and it made it without error:

Just because it compiles without errors doesn't mean it works.

I have a couple of questions. One how does this match up with the comment?

Quote

// Pad the title to 40 characters

Two where are you actually using this buffer outside this function? This variable (buffer) is local to this function and it only exists inside this function.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

Re: Trying to convert int to string

Posted 19 September 2012 - 08:27 AM

The answer to the comment question can be seen here:
// Pad the title to 40 characters
      title = title + string(40, ' ');
      title = title.substr(0, 40);
      
      cout << title;
      cout << hrs << ':';
      string minStr;
      int k = min / 10;
      char c = '0' + k; 
      minStr += c;
      k = min % 10;
      c = '0' + k; 
      minStr += c; 
      cout << minStr;
      cout << ':';
      string secStr;
      k = sec / 10;
      c = '0' + k; 
      secStr += c; 
      k = sec % 10;
      c = '0' + k; 
      secStr += c; 
      cout << secStr << endl;



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).
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3457
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

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.

Look for "justification" and "padding" in these class notes: http://courses.cs.vt...otes/C04.IO.pdf
Was This Post Helpful? 0
  • +
  • -

#12 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

Re: Trying to convert int to string

Posted 19 September 2012 - 09:04 AM

View PostSkydiver, 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.

Look for "justification" and "padding" in these class notes: http://courses.cs.vt...otes/C04.IO.pdf


I will give those a look and see if they help thanks.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

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

Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,004
  • Joined: 08-August 08

Re: Trying to convert int to string

Posted 19 September 2012 - 09:26 AM

This function will return a C++ string from a double:
string DoubleToStr(double n)
{
	std::ostringstream result;
	result << n;
	return result.str();
}


I'll leave it to you to convert it to return a string from an int.
;)
Was This Post Helpful? 0
  • +
  • -

#15 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

Re: Trying to convert int to string

Posted 19 September 2012 - 09:29 AM

Total Before Code:
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;



int main (int argc, char** argv)
{
  int totalHrs = 0;
  int totalMins = 0;
  int totalSecs = 0;

  int hrs;
  while (cin >> hrs)
    {
      // Read a song
      int min, sec;
      string title;
      cin >> min >> sec >> ws;
      getline (cin, title);

      // Normalize the time and print the song (This should be the normalize function)
      int m = sec / 60;
      sec = sec % 60;
      min += m;
      int h = min / 60;
      hrs += h;
      min = min % 60;

      // Pad the title to 40 characters (I believe this should become the twoDigits function)
      title = title + string(40, ' ');
      title = title.substr(0, 40);
      
      cout << title;
      cout << hrs << ':';
      string minStr;
      int k = min / 10;
      char c = '0' + k; 
      minStr += c;
      k = min % 10;
      c = '0' + k; 
      minStr += c; 
      cout << minStr;
      cout << ':';
      string secStr;
      k = sec / 10;
      c = '0' + k; 
      secStr += c; 
      k = sec % 10;
      c = '0' + k; 
      secStr += c; 
      cout << secStr << endl;
      
      // Add this time to the sum. 
      // Keep the sum normalized
      //   (useful for debugging purposes), (I understand this should become the function add)
      totalSecs += sec;
      totalMins += min;
      totalHrs += hrs;
      m = totalSecs / 60;
      totalSecs = totalSecs % 60;
      totalMins += m;
      h = totalMins / 60;
      totalHrs += h;
      totalMins = totalMins % 60;
    }
  // Done with all the songs. Print the total time. (I understand this should become the function printTime)
  cout << "Total: ";
  
  cout << totalHrs << ':';
  if (totalMins < 10)
    cout << '0';
  cout << totalMins << ':';
  cout << setfill('0') << setw(2) << totalSecs;
  cout << endl;

  return 0;
}



Total after code:

#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>

using namespace std;

// Normalize the time and print the song
int normalize (int sec, int min, int hrs)
    {
	sec = sec % 60;
    min = sec / 60;
    hrs = min / 60;
    }

// Add this time to the sum.
// Keep the sum normalized
//(useful for debugging purposes)

int add (int sec, int min, int hrs, int totalHrs, int totalMins, int totalSecs)
      {
      totalSecs += sec;
      totalMins += min;
      totalHrs += hrs;
      totalMins = totalSecs % 60;
      sec = totalSecs %60;
      totalHrs = totalMins /60;
      }

// Done with all the songs. Print the total time.
int printTime (int totalHrs, int totalMins, int totalSecs)
  {
  cout << "Total: ";
  cout << setfill('0') << totalHrs << ':' << setw(2) << totalMins << ':' << setw(2) << totalSecs << endl;
  }

// Convert an integer in the range of 0..99 into a string containing exactly two characters
int twoDigits (char sec, char min, char hrs)
      {
          char buffer [99] = {0};
          sprintf (buffer, "%i", sec);
          sprintf (buffer, "%i", min);
          sprintf (buffer, "%i", hrs);
      }


int main (int argc, char** argv)
{
int normalize();
int twoDigits(char sec, char min, char hrs);
int printTime();

      return 0;

}



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
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2