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

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.
closed account (z05DSL3A)
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
Thanks alot mate , worked perfectly
Topic archived. No new replies allowed.