[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1253
  • Last Modified:

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.
0
day7
Asked:
day7
  • 6
  • 5
  • 5
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now