0

I am building a meta assembler and am currently trying to get the input. The input consists of *OPCODE**SPACE**Max of 3 character Hex OPERAND*.

This is the code that I have so far:

string input;
while(input != "DONE"){
    if(input == "DONE"){
        break;
    }
    cout<<"Enter the opcode/operand to enter an instruction or 'DONE' to exit: ";
    cin>>input;


    // Seperate input
    bool opcode_bool = true;

    string opcode;
    string operand;
    for(int i = 0; i < input.length(); i++){
        if(input[i] == ' '){
            opcode_bool = false;
            continue;
        }

        if(opcode_bool == true){
            opcode = opcode + input[i];
        }else{
            operand = operand + input[i];
        }

    }

    cout<<opcode<<endl<<operand<<endl;
cout<<"END OF ITERATION"<<endl;
}

When I enter a combination of letters and numbers eg ADDI AB3, the following happens when the loop tries to separate the two parts into the opcode and operand.

This can be seen here

For some reason the input gets split up and run as two iterations of the while loop instead of as one, so that the for loop can split it up into two parts.

Help is appreciated!

wtreston
  • 1,051
  • 12
  • 28
  • 1
    Possible duplicate of [std::cin input with spaces?](https://stackoverflow.com/questions/5838711/stdcin-input-with-spaces) – UnholySheep Apr 28 '19 at 16:20
  • @UnholySheep yup that fixed it! didnt come across it when looking for solutions – wtreston Apr 28 '19 at 16:23
  • I would use `std::getline()`, to get the string, instead of `std::cin::operator>>()`, as the latter uses space as a separator between inputs – Kostas Apr 28 '19 at 16:30

0 Answers0