简体   繁体   English

调试简单控制台程序的问题:: CLion

[英]Problems debugging simple console program :: CLion

I am trying to learn basic C++ after being a Java developer. 我是一名Java开发人员后,我正在努力学习基本的C ++。 So I decided to give CLion a try. 所以我决定试试CLion。 I wrote this basic code just to familiarize myself with some C++ syntax. 我编写这个基本代码只是为了熟悉一些C ++语法。

#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. 我想逐步查看变量和不变量,并测试出CLion的调试器。

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. 我已经多次使用Eclipse的调试器进行Java开发而没有任何问题。 Any ideas could be helpful. 任何想法都可能有所帮助。

TL;DR How do I step through a C++ command line program with basic input and output using CLion? TL; DR如何使用CLion逐步执行带有基本输入和输出的C ++命令行程序?

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. 我已经复制了这个问题 - 看起来就像调试换行符被IDE吞没而没有传回程序一样。 I've submitted a bug to JetBrains . 我向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. 除了退出IDE并直接使用GDB或其他IDE进行调试之外,我没有办法解决这个问题。


UPDATE : This issue was fixed in the Clion EAP Build 140.1221.2. 更新 :此问题已在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. 调试器不再挂起'cin >>'运算符。

Looking at your code, if everything is correct, you need to add #include <string> . 查看代码,如果一切正确,则需要添加#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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