-2

I am implementing the ALV tree, and I need to read input from the command line.

An example of the command is as follows:

insert “NAME_ANYTHING_IN_QUOTES_$” ID

where NAME_ANYTHING_IN_QUOTES_$ is the data being stored in the AVL tree, and ID is used to decide if the info will be stored in the left subtree or right subtree.

Code snipet is as follows:

if (comand == "insert")
{
    int ID = 0;
    string Name;
    cin >> Name;
    cin >> ID;
    root = insertInfo(root, Name, ID);
}

I can not figure out how to scan in the substring that is between two double-quotes.

Thanks.

Nejc Galof
  • 2,538
  • 3
  • 31
  • 70

4 Answers4

3

Use std::quoted:

std::string name;
std::cin >> std::quoted(name);
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • I tried that it does not work – Parth Yagnik Oct 07 '21 at 18:09
  • 2
    @ParthYagnik Expand on *does not work*. My brother-in-law does not work, but likely you mean something other than loafing around the house eating potato chips and soaking up people's taxes. Please describe what happened and how it differed from your expectations. Also know that you may have more than one bug. – user4581301 Oct 07 '21 at 18:10
  • quoted does not extract substring between two quotes. I am trying to fix this bug in a nonproduction environment where it does not interact with the larger codebase. I am not inserting anything until I can figure out why I cant extract the substring between the double quotes. – Parth Yagnik Oct 07 '21 at 18:14
  • 2
    Do you by any chance have different start and end quotes ? (Like if you use MSWord ) ? – kalyanswaroop Oct 07 '21 at 18:16
  • In other words, are you copy-pasting the input from somewhere? In your question you have some fancy quotes `“”` instead of the regular ones `"`. – HolyBlackCat Oct 07 '21 at 18:20
  • I think the quotes are your issue. First replace all chars of \x93 and \x94 with \x22 and then use HolyBlackCat's solution. That should work. I'll add that to the code below. – kalyanswaroop Oct 07 '21 at 18:39
1

I found the answer....

string name,ID,concat;

getline(cin, concat);

for(int i =0; i< concat.length();i++){

    if(!isdigit(concat[i])&& concat[i] != 34){
        name += concat[i];
    }
    if(isdigit(concat[i])){
        ID += concat[i];
    }
}

cout<<"name is ->"<<name<<endl;

cout<<"ID is ->"<<ID<<endl;
0

I'm just adding to HolyBlackCat's answer above.
Here's code that works:

#include <iostream>
#include <iomanip>

using namespace std;


int main(int argc, const char** argv)
{
    //if (comand == "insert")
    {
        int ID = 0;
        string Name;
        cin >> std::quoted(Name);
        cin >> ID;
        cout << "name is " << Name << " and ID is " << ID << endl;  
        //root = insertInfo(root, Name, ID);
    }

}

When input is "MyName_$" 34
I get
name is MyName_$ and ID is 34
So, HolyBlackCat's solution works. Show us your code and input.

kalyanswaroop
  • 403
  • 4
  • 5
  • Recommendation: Make things a bit harder on yourself by placing some whitespace in `"MyName_$"` to demonstrate the power of this fully armed and operational `std::quoted`. I use the Major-General's song from The Pirates of Penzance, but that might be overkill here. – user4581301 Oct 07 '21 at 18:41
  • when I tried your code with "Parth yagnik" 34 it gave me name is “Parth and ID is 0. Its not scanning "Parth Yagnik" as name it's only scanning "Parth as name. – Parth Yagnik Oct 07 '21 at 20:27
  • 1
    @ParthYagnik Something odd and unspecified is happening at your end then. [Here is what should (and does) happen](https://ideone.com/sRytni) with a good compiler and valid input. – user4581301 Oct 07 '21 at 21:10
0

On second thought, those strange open and close braces may depend on the font, etc. The std::quoted should have worked for you, but it you say it doesnt. Let us know what env and compiler you are using. If you're sure they're always there, you can just delete the first and last char in that string as in:

#include <iostream>
#include <string>

using namespace std;


int main(int argc, const char** argv)
{
    
    //if (comand == "insert")
    {
        int ID = 0;
        string Name;
        
        cin >> Name;

        Name.erase(0, 1);
        Name.erase(Name.length() - 1, 1);
                
        cin >> ID;

        cout << "name is " << Name << " and ID is " << ID << endl;
        
        //root = insertInfo(root, Name, ID);
    }

}
kalyanswaroop
  • 403
  • 4
  • 5