cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : Help Please , Quite simple string conver...
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

post  Help Please , Quite simple string conversion , but can't find a way around it.

mosdapwn (3)
It really should be a simple conversion but there's no way I can think of to get this to work
I got a string with a number in it like " the number is 22"
And i want to take this 22 and store it in an integer,
the only thing i could do was use a character conversion for each one , and not only do i get some kind of ascii code for the numbers , but its pointless cause it can't be used for more than one character.
|
Grey Wolf (637)
Here a bit of code that you could probable neaten up a bit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <sstream>
using namespace std;

int main () {

  
  stringstream ss (stringstream::in | stringstream::out);

  ss << "the number is 22";
  
  // eat the alpha and spaces...
  char ch = 0;
  while (ss.get(ch) && !isdigit(ch));
  
  //put back the numeric;
  ss.putback(ch);
  
  // finally read the number into a variable
  int val =0;
  ss >> val;

  cout << val << endl;


  return 0;
}
| Last edited on
mosdapwn (3)
Thanks alot mate , worked perfectly
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us