Link to home
Start Free TrialLog in
Avatar of day7
day7

asked on

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.
Avatar of pb_india
pb_india

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;
}
//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
for sscanf example..

forgot to inititalize int i;

make it int i =0;
Avatar of day7

ASKER

Thanks pb_india but I was looking for something using an ios stream...
#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


 
Avatar of day7

ASKER

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

strstream ss1( str1);
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;
}

Avatar of day7

ASKER

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.
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.

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;
}
Avatar of day7

ASKER

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
#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;
}
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;
}
Avatar of day7

ASKER

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
I will d/l dev-c++. The example I gave works corectly on VS 2003 and Borland 6.
ASKER CERTIFIED SOLUTION
Avatar of SirHando
SirHando

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial