Searching in a string for ints?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • The Midnighter
    New Member
    • Feb 2007
    • 15

    Searching in a string for ints?

    I'm trying to search a string for numbers, to ensure that it only contains numbers and no characters - what's the easiest way to do this?

    I thought it might be something likkkeee...
    size_t x;
    string y;

    x = y.find('0-9');
    if (x!=npos::strin g)
    {
    // if it contains anything but 0-9, exec code
    }
    else
    {
    // yeah
    }

    but, it doesn't seem to be working at all, yeeeah I'm sure that's a foolish way I just c an't think of any other way =(

    Alright, new idea.. What if I convert it to a c string, and then convert it back later? that should get rid of any alphanumeric characters...
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Just call isdigit() using each character of the string or code:
    [code=c]
    if (str[i] >= 0 && str[i] <= 9)
    {
    //it's a digit
    }
    else
    {
    //no it's not
    }
    [/code]

    Comment

    • The Midnighter
      New Member
      • Feb 2007
      • 15

      #3
      Originally posted by weaknessforcats
      Just call isdigit() using each character of the string or code:
      [code=c]
      if (str[i] >= 0 && str[i] <= 9)
      {
      //it's a digit
      }
      else
      {
      //no it's not
      }
      [/code]
      Alright.. What's [i] supposed to be? str would be my string of course..

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        The element in the string:

        string str("Hello");

        str[1] is the e of Hello.

        str[i] is the same thing where i is your loop counter.

        Comment

        • The Midnighter
          New Member
          • Feb 2007
          • 15

          #5
          Originally posted by weaknessforcats
          The element in the string:

          string str("Hello");

          str[1] is the e of Hello.

          str[i] is the same thing where i is your loop counter.
          ....
          What?

          so i is... a loop, that does what?

          Anyway, here's some code I'm working with now:

          [code=cpp]
          string str(n1);
          string str2(d1);
          istringstream strin2(str2);
          istringstream strin(str);

          strin >> c;
          strin2 >> d;
          cout << "c = " << c << "\nd = " << d << "\n";
          if (!((c<=0) || (c>9)) || (d<=0) || (d>9))
          {
          cout << "Error - Enter an int!\n";
          cout << "c = " << c << "\nd = " << d << "\n";
          move = true;
          }[/code]

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by The Midnighter
            What?

            so i is... a loop, that does what?
            A loop that examines each character in your string:
            [code=cpp]
            string str("Hello123") ;

            for (int i = 0; i < str.size(); ++i)
            {
            if (str[i] ....etc...
            }
            [/code]

            Comment

            • The Midnighter
              New Member
              • Feb 2007
              • 15

              #7
              :D Found a solution!
              [code=cpp]
              pos = second.find('/'); // Searches for the '/'
              d2 = second.substr(p os+1); // 1 space after '/'
              n2 = second.substr (0,pos); // from beginning to '/'
              space = second.find_fir st_of(" "); // Searches for a space


              string str(n2);
              string str2(d2);
              istringstream strin2(str2);
              istringstream strin(str);

              strin >> e;
              strin2 >> f;
              cout << "e = " << e << "\nf = " << f << "\n";
              if (((e>9999) || (e<=-9999)) || (f>9999) || (f<=-9999)) // Checks if its below -9999, or above 9999
              {
              cout << "Error - Enter an int!\n";
              cout << "e = " << e << "\nf = " << f << "\n";
              e = 0;
              f = 0;
              move = false;
              }[/code]

              Comment

              Working...