?
Solved

Converting int to string

Posted on 2003-03-25
24
Medium Priority
?
359 Views
Last Modified: 2012-06-27
Hi all,
I thought this would be simple but I'm having problems converting an interger to a class string. It's probably worth mentioning that I don't have the stringstrem header.
Cheers
0
Comment
Question by:chichibabin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
  • 6
  • +1
24 Comments
 
LVL 2

Expert Comment

by:xyzzer
ID: 8202004
What is a "class string"?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8202017
Probably something like:

class String
{
  char str[10] ;
public:
  String () // default constructor ()
  {
    str = "0" ;

  } // end of default constructor ()

  String ( int val ) // parameterized constructor ()
  {
    itoa ( val, str, 10 ) ; // include <stdlib.h> for that

  } // end of parameterized constructor ()

  void display ()
  {
    cout << str << endl ;

  } // end of display ()

} ; // class definition over


chichibabin, is that what you want????

Mayank.
0
 
LVL 12

Expert Comment

by:Salte
ID: 8202049
how come you don't have the stringstream header?

#include <iostream>
#include <sstream>

is the header for stringstream.

Or if you use the old obsolete streams:

#include <iostream.h>
#include <strstream.h>

stringstream should be present in all semi-modern compilers.

Alf
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:chichibabin
ID: 8202104
Regarding the class string I just ment a string ather than a char string. i.e. string str, not char str[4]
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8202126
string obj ;
char temp[10] ;

itoa ( intValue, temp, 10 ) ;
obj = string ( temp ) ;

Mayank.

0
 

Author Comment

by:chichibabin
ID: 8202154
I've just realised I do have the sstream header, I need to include some -LANG:std option when compiling. So if I can get it working what's the best way to convert int to string?
Cheers
0
 
LVL 12

Accepted Solution

by:
Salte earned 80 total points
ID: 8202174
string i2s(int x)
{
   stringstream ss;
   ss << x;
   string s;
   ss >> s;
   return s;
}

Alf
0
 

Author Comment

by:chichibabin
ID: 8202273
Salte
When I use the modern headers i.e 'iostream' rather than 'iostream.h', I have to add 'using namespace std' otherwise commands like 'cout' are not recognised.
Isn't this bad practise as I heard this leads to namespace pollution (whatever that is)?
Cheers
0
 
LVL 12

Expert Comment

by:Salte
ID: 8202411
namespace pollution occur when you do a lot of 'using' since you essentially import all the names defined in the given namespace into your present scope.

so:

using namespace foo;

will in one statement import EVERY name defined in foo into the current scope.

However, for namespace std this isn't so much of an issue. The std namespace is standard and is defined as part of the C++ standard and so if you know C++ you should be assumed to be familiar with the std namespace.

For any other namespace (not std) it might be an issue though.

However, for other namespaces that you might wnat to use 'using' though might be a company wide namespace. If all employees in the company FOO is supposed to be familar with the namespace 'foolib' then you might use 'using namespace foolib;' as well.

However, you should use caution when using 'using' as it might quickly lead to a tons of various names that you don't know about. The problem is that if you call a function or use a variable or a type named 'bar' where is that function, variable or type defined? If you have:

using namespace foo1;
using namespace foo2;

Then the name 'bar' might reference a name foo1::bar or it can be foo2::bar or it can be just 'bar' in your own local scope it isn't always easy to tell. In particular it might bring trouble since C++ has some 'odd' scoping rules:

if you write:

foo * ptr;

Then ptr is a variable of type 'pointer to foo'. Now foo is obviously a type, possibly defined by a typedef defined locally:

typedef int foo;

above the declaration of the variable.

However, further down in the code you may refer to:

struct foo * ptr2;

Now this refer to a 'struct foo' which isn't the same as the typedef foo. Now, in C++ usuallly when you have 'struct foo' it can be replaced with just 'foo' since structs are types in C++ (unlike C). However, due to our own typedef above you can't do that in this code since 'foo' does not refer to the same type as 'struct foo' but instead refer to the typedef so 'foo' is 'int' while 'struct foo' is a struct defined somewhere.

Now, this problem is of course possible without namespaces and 'using' but it isn't very likely to occur in those cases. However, if you use 'using' it is very easy to end up in such a situation since you often do 'using namespace foo;' without having full overview of what the namespace foo contains.

So, the bottom line is:

using namespace foo;

should in general be avoided and you should definitely use caution. It is a lot clearer if you write foo:bar() instead of just bar() when you call a function in namespace foo.

However, the specific namespace std and possibly also a company specific namespace are exceptions to this rule. They should be assumed to be familiar to the reader (in particular the std namespace is, the company specific is probably not familiar to anyone except those who has worked in the company a while).

Above all I will in general strongly advice against using compiler specific namespaces through 'using' statements. For example if you use 'Foo C++ compiler' delivered from company 'Foo inc.' they might provide their own namespace 'foo' where they have all compiler specific extensions. In this case it is probably a very good idea to NOT use 'using namespace foo;' but rather use foo::extension_function(..);

Bottom line:

using namespace std;

is ok and is even good since you otherwise would have a lot of references to std::.

using namespace company_specific;

is probably also ok, but partly depends on use. If you can manage without it, it is better :-)

using namespace anything_else;

should be used with caution if at all.

Alf
0
 

Author Comment

by:chichibabin
ID: 8203191
I've just realised I do have the sstream header, I need to include some -LANG:std option when compiling. So if I can get it working what's the best way to convert int to string?
Cheers
0
 

Author Comment

by:chichibabin
ID: 8209355
Hi
I'm still having problems. My code originally was using obsolete headers iostream.h fstream.h stdlib.h. When I include sstream everything starts going horribly wrong and I have to change the headers and include a -LANG:std option. The result is a much slower executable. If I use strstream.h the speed is retained but when I declare:

stringstream ss

I get the comilation error - stringstream is undefined? Does strstream.h use different syntax?
Cheers
0
 

Author Comment

by:chichibabin
ID: 8209368
PS
What are the advantages of using the modern headers and is it a problem to mix them with obsolete ones?
Cheers
0
 
LVL 12

Expert Comment

by:Salte
ID: 8209465
strstream does not use the name 'stringstream'.

It uses the name 'strstream'.

so

strstream ss;

ss << intval;

ss >> str;

One problem is that it is possible that strstream doesn't mix well with std::string either so you should probably avoid using std::string together with strstream and so you might want do to this:

string i2s(int x)
{
   strstream ss;
   ss << x;
   char buf[50]; // should be enough
   ss >> buf;
   return string(buf);
}

Alf
0
 

Author Comment

by:chichibabin
ID: 8209637
Thanks
I really need it as a class string not a char string.
When I use:
strstream ss;
I get the error '"ss" has already been declared in the current scope'
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8209675
You have probably declared another variable or object in the same scope with the same name. The error says it itself.

Mayank.
0
 
LVL 12

Expert Comment

by:Salte
ID: 8209791
>> I get the error '"ss" has already been declared in the current scope'

This error message means what is says. You already have another variable named 'ss' in the same scope.

To me it seems like plain english and english is not my first language.

One good advice:

When you get an error message do NOT just think "Oh, error, I post it to EE and ask people what it means". try to actually READ the message first. There's a good chance you actually understand the message yourself. You'd be surprised as to how readable and understandable error messages from modern compilers are.

I agree that older compilers tended to have very cryptic error messages that some times didn't give the correct error message - if at all. But those days are long gone and most compilers today - including MSVC 6.0, Devstudio 7.0 and gcc 3.0 - have error messages which explains very well exactly what is wrong and you can usually figure out what is wrong by simply reading the message.

Alf
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8209815
>> When you get an error message do NOT just think "Oh, error, I post it to EE and ask people what it means". try to actually READ the message first.

>> There's a good chance you actually understand the message yourself.

>> You'd be surprised as to how readable and understandable error messages from modern compilers are.

I wish people understood it. Somebody'd been driving me mad over class templates.... and the error was just a missing <T>.... and there were already over 20 posts by him and myself on that page!

Mayank.
0
 

Author Comment

by:chichibabin
ID: 8209975
Thanks
I really need it as a class string not a char string.
When I use:
strstream ss;
I get the error '"ss" has already been declared in the current scope'
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8210010
Please don't click on "Refresh" but click on the "Reload this question" link at the left-hand side to reload this page.

Mayank.
0
 

Author Comment

by:chichibabin
ID: 8210042
I'm sorry Alf, but if you are suggesting that I already have a variable called 'ss' then you are very much mistaken. Of course I've tried changing the name of the variable!! I may be a C++ novice but I'm not a complete idiot! I could declare:
strstream fishfrommars;

and i get the message error '"fishfrommars" has already been declared in the current scope' (and I don't have another variable called fishfrommars)

0
 

Author Comment

by:chichibabin
ID: 8210104
PS
That goes to you too Mayank
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8210105
Please post your code, then.

Mayank.
0
 

Author Comment

by:chichibabin
ID: 8210149
I've finally sorted it out.
Thanks for the help
0
 
LVL 12

Expert Comment

by:Salte
ID: 8210173
I would suspect that the error is due to some obscure bug using some bogus #define's etc then, so that you get the variable ss declared twice.

The error message does complain that 'ss' is defined twice. It is possible you have declared it only once (or so you think) but the compiler complains that it is twice and then that is the error detected. It COULD be that the compiler is wrong and simply complain about an error and somehow got the name wrong but that is unlikely.

It is more likely to think that your code does have some problems.

For example mixing <iostream> and <iostream.h> is never a good idea, in particular mixing <iostream> and <strstream.h> is absolutely not a good idea.

However, as strstream.h is an obsolete include file it may include some #define's that aren't really good in modern code.

A problem with #define is that it knows nothing about C++ scoping rules and a #define somewhere far away might cause troubles for you.

I remember in one of the early days of C++ when I tried to write a program and making a class having a function named 'getc()' and it failed because one include file included <stdio.h> which had a #define for getc and so any reference to getc - even if it was clearly as member of a class object - was replaced by that macro and so I got error.

And actually, yes, trying to change the name of the variable to something else and recompile is a good way to attempt to find the problem. If you get the same error message  but with the new name (and presumably with a new name you are very sure you have never used before - aaa20030326bbb1431ccc might be a variable you are sure you have never declared before. If you still get the same error then it is something about the strstream declaration that causes the error.

It could simply be that the strstream.h is outdated and make use of features which are interpreted differently in modern compilers and which breaks. If so changing to stringstream and iostream everywhere is probably the best way to go.

Alf
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question