Error: Unrecognized command when I entered user input when debugging

"""

#include <iostream>

#include <cstring>

using namespace std;


// Function replaces spaces with hyphens

void StrSpaceToHyphen(char modString[]) {

int i; // Loop index


for (i = 0; i < strlen(modString); ++i) {

if (modString[i] == ' ') {

modString[i] = '-';

}

}

}


int main() {

const int INPUT_STR_SIZE = 50; // Input C string size

char userStr[INPUT_STR_SIZE]; // Input C string from user


// Prompt user for input

cout << "Enter string with spaces: " << endl;

cin.getline(userStr, INPUT_STR_SIZE);


// Call function to modify user defined C string

StrSpaceToHyphen(userStr);


cout << "String with hyphens: " << userStr << endl;


return 0;

}

"""


When I tried to step in cin.getline(userStr, INPUT_STR_SIZE), in debugger console , after "Enter string with spaces: ", I entered a string "Hello there everyone.".

Then there're two error.

error : 'Hello' is not a valid command.

error: Unrecognized command 'Hello'


Everytime I enter an user input, this error ocurred. I am using Xcode 10 with c++ , and my debugger is lldb.

How can I solve this problem? Thank you!

Replies

This is working for me. I put your code into a new project created from the C++ Command Line Tool template. I then ran it on my Mac (Xcode 10.0, macOS 10.14) and here’s what I saw in Xcode’s Console pane:

Enter string with spaces: 
Hello Cruel World!
String with hyphens: Hello-Cruel-World!
Program ended with exit code: 0

Some things to note:

  • Line 1 showed up first. I entered the text on line 2. It printed the output on line 3 and then terminated, causing Xcode to print line 4.

  • Line 4 actually showed up in the middle of the console output, which I’ve fixed here. This is kinda ugly, but it’s not a problem in practice because that line doesn’t show up until after the program has exited.

  • The first time I ran this I saw some weird behaviour that I don’t fully understand (Xcode was not accepting any console input). However, stopping the program and running it again cleared that issue and I’ve not been able to reproduce it.

Finally, a note on your code. In

StrSpaceToHyphen
the
i
variable should be
size_t
so that it matches the return result of
strlen
(-:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"