getline function issue inputting string AND integer

Hello, I am trying to take in user input using getline(), but I am having an issue with the function.

I want users to input a name and an ID associated with their name where the name is inputted with double quotes and the ID has to be 8 digits long. For example:
"Bob Vance" 12345678
"Dwight Schrute" 12344321

Right now I have both the ID and name working separately, but I am confused on how to get this input from the user in one line and then separate the name and ID to its corresponding variable. I tried using getline() but that does not seem to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        string fakeNumber = "";
        int number = 0;
        string name = "";

        string userInput;
        cin >> userInput;

        if (userInput == "insert")
        {
            getline(cin, name);
            name.erase(remove(name.begin(), name.end(), '\"'), name.end());
            cin >> name >> fakeNumber;

            if (fakeNumber.length() != 8)
                cout << "unsuccessful" << endl;
            else
            {
                number = stoi(fakeNumber);

                cout << "Name: " << endl;
                cout << "ID: " << number << endl;
            }
        }
Using the regular expression library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <regex>

int main()
{
    std::cout << "enter the name in double quotes and the 8 digits ID: " ;
    std::string input ;
    if( std::getline( std::cin, input ) )
    {
        // optional white space, quote, name having one or more non-quote characters (capture 1), quote,
        //         optional white space, 8 decimal digits (captrure 2), optional white space
        const std::regex input_re( "\\s*\"([^\"]+)\"\\s*(\\d{8})\\s*" ) ;
        std::smatch match ;
        if( std::regex_match( input, match, input_re ) ) // if the input matches
        {
             const std::string name = match[1] ; // pick up name from capture 1
             const int id = std::stoi( match[2] ) ; // and id as an integer from capture 2
             std::cout << "name: " << name << "\nid: " << id << '\n' ;
        }

        else std::cout << "invalid input\n" ;
    }
}
Ohh wow, I did not know c++ had a regular expressions library! I will try this out, thank u
Without using regex, consider:

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
#include <sstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
	std::string line;

	std::cout << "Enter name in \" and 8 digit id: ";
	std::getline(std::cin, line);

	std::istringstream iss(line);
	std::string name, id;

	std::getline(iss, name, '\"');
	std::getline(iss, name, '\"');
	std::getline(iss >> std::ws, id);

	if (id.size() == 8 && std::all_of(id.begin(), id.end(), [](const auto ch) {return std::isdigit(ch); })) {
		std::cout << "Name is: " << name << '\n';
		std::cout << "Id is: " << id << '\n';
	} else
		std::cout << "Invalid input\n";
}



Enter name in " and 8 digit id:     "qwer asd ffg"     12345678
Name is: qwer asd ffg
Id is: 12345678

Last edited on
Topic archived. No new replies allowed.