简体   繁体   中英

Problems debugging simple console program :: CLion

I am trying to learn basic C++ after being a Java developer. So I decided to give CLion a try. I wrote this basic code just to familiarize myself with some C++ syntax.

#include <iostream>
using namespace std;

int main() {
    string word;

    cout << "Enter a word to reverse characters: " << endl;
    getline(cin, word);

    for(int i = word.length(); i != -1; i--) {
        cout << word[i];
    }

    return 0;
}

The code is functional. It reverses whatever word you input. I wanted to step through it to see variables and what not, and to test out CLion's debugger.

My problem occurs when I get to

getline(cin, word);

When I step onto this line, I enter a word and hit enter. Then step over. After I do this nothing happens; all the step over, in, etc. buttons are disabled. I am unable to continue through the loop, or run the remainder of the code.

I have used Eclipse's debugger many times for Java development without any issues. Any ideas could be helpful.

TL;DR How do I step through a C++ command line program with basic input and output using CLion?

I've replicated the problem - looks to me like when debugging the newline is being swallowed by the IDE and not passed back to the program. I've submitted a bug to JetBrains . I don't see a way to work around this aside from getting out of the IDE and debugging directly with GDB or another IDE.


UPDATE : This issue was fixed in the Clion EAP Build 140.1221.2. It even made the first change listed in the release notes:

The most valuable changes are:

  • Debugger doesn't hang on 'cin >>' operator any more.

Looking at your code, if everything is correct, you need to add #include <string> .

When I run this, it compiles and completes the output.

#include <iostream>
#include <string>

int main() {

    std::string word;

    std::cout << "Enter a word to reverse chars: ";
    std::getline(std::cin, word); //Hello

    for (int i = word.length() - 1; i != -1; i--) {
        //Without - 1 " olleh"
        //With    - 1 "olleh"
        std::cout << word[i];
    }
    std::cout << std::endl;
    system("pause");
    return 0;
}

Use the following code. I have modified your code to make it workable for your purpose. :)

#include <iostream>
#include <string>
using namespace std;

int main() {
    string word;

    cout << "Enter a word to reverse characters: " << endl;
    getline(cin, word);

    for(int i = word.length() - 1; i != -1; i--) {
        cout << word[i];
    }

    printf("\n");

    system("pause");

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM