1

How can I parse a string that looks like "xxxx-xxxx" and get those xxxx parts as a number? For an example, the user will type in "9349-2341" and I will get those numbers as two different integers.

I need to do that for a random number generator, which chooses the number between these xxxx variables.

Thanks.

  • 2
    What have you tried? This is really very simple, but nobody should be writing the code for you. –  Aug 11 '18 at 12:54
  • 1
    I haven't tried anything yet, since I am a beginner. Also I know a real tiny bit of string manipulation, but this question is enough to take me out – Ferit Yiğit BALABAN Aug 11 '18 at 12:56
  • 2
    stackoverflow.com is not a substitute for [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You should find plenty of examples of working with strings in your C++ book, that will have far more detailed and useful information that can be fit into a brief one or two paragraph answer on stackoverflow.com. If this is your homework assignment, and you're having problems, you should ask your instructor for help. This is what they're paid to do. – Sam Varshavchik Aug 11 '18 at 12:58
  • 1
    I am not getting an course for this, I own a book with real simple examples, and I just need a simple method to solve this. – Ferit Yiğit BALABAN Aug 11 '18 at 13:03
  • Read up on string::find, string::substr and std::stoi. –  Aug 11 '18 at 13:04
  • https://en.cppreference.com/w/cpp/regex is one way. – Jesper Juhl Aug 11 '18 at 13:50

3 Answers3

4

You can use std::stringstream to extract numbers from string. It looks like that:

std::stringstream str_stream;
std::string str_to_parse = "1234-5678";
int num[2];

str_stream << str_to_parse;
str_stream >> num[0];
str_stream.ignore(1); // otherwise it will extract negative number (-5678)
str_stream >> num[1];

Also, there is C functions, like sscanf(). For example, your pattern can be extracted with this format: "%d-%d".

Shorrer
  • 104
  • 2
  • 3
2
std::string str = "1234-5678";

std::string str1 = str.substr (0,4); 
std::string str2 = str.substr(5, 4);

int n1 = std::stoi(str1);
int n2 = std::stoi(str2);

// do your random number generation between n1 and n2

JeJo
  • 30,635
  • 6
  • 49
  • 88
tofu head
  • 109
  • 1
  • 2
1

Using regular expression

If your input is assured to resemble "xxxx-xxxx" where 'x' represents a digit, you can simply ultilize the following function:

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main()
{
    string input = "9349-2341";

    // This pattern matches any string begining with 4 digits and ending with 4 digits, both parts seperated by a slash
    string pattern = "([0-9]{4})-[0-9]{4}";
    smatch matcher;

    regex prog (pattern);

    if (regex_search(input, matcher, prog))
    {
        auto x = matcher[1];
        cout << x << " " << endl;
        input = matcher.suffix().str();
    }
    else
    {
        cout << "Invalid input!" << endl;
    }

    return 0;
}

As for how to convert string to number, check out this article, from which the following segment is quoted:

string Text = "456";//string containing the number
int Result;//number which will contain the result

stringstream convert(Text); // stringstream used for the conversion initialized with the contents of Text

if ( !(convert >> Result) )//give the value to Result using the characters in the string
    Result = 0;//if that fails set Result to 0
//Result now equal to 456 

Or, simply as followed:

Using sscanf

#include <cstdio>

using namespace std;

int main(int argc, char ** argv)
{
    char input[] = "1234-5678";
    int result, suffix;

    sscanf(input, "%i-%i", &result, &suffix);

    printf("Output: '%i-%i'.\n", result, suffix);

    return 0;
}

You should check out C++ reference websites, such as CPlusPlus.

KaiserKatze
  • 1,521
  • 2
  • 20
  • 30