convert string to int - sscanf and ios

Can I convert a string to an int using an ios class like I can using sscanf()?
 Examples appreciated.
day7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pb_indiaCommented:
you can use:

atoi()

or you can use:

sscanf()

#include <stdio.h>

int main ()
{
  char sentence []="Test";
  char str [20];
  int i;

  sscanf (sentence,"%d",&i);
  printf ("%s -> %d\n",sentence,i);
 
  return 0;
}
0
pb_indiaCommented:
//atoi() example

#include <stdio.h>
#include <stdlib.h>

int main() {
  char str1[] = "124z3yu87";
  char str2[] = "-3.4";
  char *str3 = "e24";
  printf("str1: %d\n", atoi(str1));
  printf("str2: %d\n", atoi(str2));
  printf("str3: %d\n", atoi(str3));
  return 0;
}
Output:

str1: 124
str2: -3
str3: 0
0
pb_indiaCommented:
for sscanf example..

forgot to inititalize int i;

make it int i =0;
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

day7Author Commented:
Thanks pb_india but I was looking for something using an ios stream...
0
SirHandoCommented:
#include <strstream>
using namespace std;

void StrToInt( void )
{
  const char* str1 = "123";
  int              val;

  strstream ss1( str1 );

  ss1 >> val;

  cout << "Value is " << val << endl;
}

Will print

Value is 123

You can also

void StrToInt( void )
{
  const char* str1 = "123 456 789";
  int              val;

  strstream ss1( str1);

  while (ss1.good())
  {
    ss1 >> val;
    cout << "Value is " << val << endl;
  }
}

Will print

Value is 123
Value is 456
Value is 789

Regards,

SirHando


 
0
day7Author Commented:
SirHando,
  Thanks for your efforts...I get a no matching function call error on this line when I compile your code...:

strstream ss1( str1);
0
SirHandoCommented:
Here's the following, compiles and runs on VS 2003. Missing from my example above you need string lenght and open mode, although open mode has a default supplied.

#include <iostream>
#include <strstream>

using namespace std;

int _tmain(int argc, char* argv[])
{
    char*       str1 = "123 456 789";
    int         val;

    strstream ss1( str1, ::strlen( str1 ), ios_base::in );

    ss1 >> ::skipws;

    while( ss1.good() )
    {
        ss1 >> val;
        cout << "The value is " << val << endl;
    }

      return 0;
}

0
day7Author Commented:
Thanks again SirHando,
   This code compiles for me but I still encounter a problem in that the value for 'val' that is printed is an address and not the 'to integer' conversion of str1.
0
SirHandoCommented:
The address of what?

When  you run the above code, what is the output you get?

Also, what compiler are you using. Althought this example should be compiler independant, you might have some non standard setup.

0
pb_indiaCommented:
Try This -
//val needs to be initialized

#include <iostream>
#include <strstream>

using namespace std;

int _tmain(int argc, char* argv[])
{
    char*       str1 = "123 456 789";
    int         val=0;

    strstream ss1( str1, ::strlen( str1 ), ios_base::in );

    ss1 >> ::skipws;

    while( ss1.good() )
    {
        ss1 >> val;
        cout << "The value is " << val << endl;
    }

     return 0;
}
0
day7Author Commented:
Here's what I've compiled...I've also made it print the addresses of most of the other objects...I am using Borland's C++BuilderX Personal 1.0.0.1786...I get the same result(just different addresses) with Dev-C++ 4.9.9.0

#include <iostream>
#include <strstream>

using namespace std;

int main(int argc, char* argv[])
{
    char*       str1 = "123 456 789";
    int         val;

    strstream ss1( str1, ::strlen( str1 ), ios_base::in );

    ss1 >> ::skipws;

    while( ss1.good() )
    {
        ss1 >> val;
        cout << "The value is " << val << "  " << (int)&val << "  " << (int)&str1 << "  " << (int)&ss1 <<endl;
    }

     return 0;
}



Prints:
  The value is 1244948  1244800  1244804  1244844
0
pb_indiaCommented:
#include <iostream>
#include <strstream>
#include <sstream>
using namespace std;

int main(int argc, char* argv[])
{
     char*       str1 = "123 456 789";
     int b=0,c=0,d=0;
     stringstream ss(str1);
     ss >> b;
       ss>>c;
       ss>>d;
      
     std::cout << b<<c<<d;
     return 0;
}
0
pb_indiaCommented:
With current example this works..


#include <iostream>
#include <strstream>

using namespace std;

int main(int argc, char* argv[])
{
    char*       str1 = "123 456 789";
    int         val;

    strstream ss1( str1, ::strlen( str1 ), ios_base::in );

    ss1 >> skipws;
      cout << "The value is " <<endl;
    while( ss1.good() )
    {
        ss1 >> val;
        cout <<val;
    }

     return 0;
}
0
day7Author Commented:
thank you all for your efforts/

unfortunately, none of these answers works so unless I receive something further I'm going to request that the question be deleted
0
SirHandoCommented:
I will d/l dev-c++. The example I gave works corectly on VS 2003 and Borland 6.
0
SirHandoCommented:
Ok this works on Dev  c++ 4.9.9.0. The use of strstream is depreciated in this compiler, which you  should have observed from the warnings. Note that the only difference is replacing strstream with stringstream. The initial assignment of 0 to val is not required.

Regards,

SirHando


#include <iostream>
#include <sstream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
    char*       str1 = "123 456 789";
    int         val  = 0;
   
    stringstream ss1( str1, ios_base::in );

    ss1 >> ::skipws;

    while( ss1.good() )
    {
        ss1 >> val;
        cout << "The value is " << val << endl;
    }

     return 0;
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.