11

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?

ryndshn
  • 692
  • 2
  • 13
  • 30
  • doesn't answer your question, but you've got undefined behavior: `string word; char wordReversedArray[word.length()];` - you have an array with 0 length that you try to write into... – John3136 Oct 02 '14 at 02:07
  • @John3136 You're correct. I changed the code to be a little more clear. The same problem still persists. – ryndshn Oct 02 '14 at 02:19
  • Could be as simple as buffering - `cout << "str";` write to the console/terminal. The console can buffer things up until it gets a newline. Try `cout << "str" << endl;` too see if that explains things... – John3136 Oct 02 '14 at 02:25
  • @John3136 That fixed one problem. It now prints when expected. However I'm still having the issue when inputting. – ryndshn Oct 02 '14 at 02:39
  • 2
    Add a `cout` before your loop to display the word. Add a `cout` in your loop to display `i`. See if eveything is as you expect. C++ uses 0 based indexes, so `word[word.length]` isn't valid. – John3136 Oct 02 '14 at 02:46
  • @John3136 Yeah that's an easy work around. I was hoping to find a fix for the debugger, because I will surely be writing more advanced code that will require some sort of debugger. – ryndshn Oct 02 '14 at 02:59
  • Not clear what problem you've actually got with the debugger? – John3136 Oct 02 '14 at 03:12
  • @John3136 The debugger options are disabled after entering a word. I am not able to step through the loop, or anything else following getline(cin, word); – ryndshn Oct 02 '14 at 03:22
  • @ryan4888 Have you compiled the application with debugging support enabled? (side note, change the loop to start with `i = static_cast(word.length()) - 1`). Go to [Run | Edit Configurations dialog and change the configuration type to Debug](http://blog.jetbrains.com/clion/2014/09/clion-answers-frequently-asked-questions/) – Mohit Jain Nov 10 '14 at 07:38

3 Answers3

11

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.
sfjac
  • 7,119
  • 5
  • 45
  • 69
1

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;
}
CodeMonkey
  • 1,136
  • 16
  • 31
  • `std::string` on some `C++` implementations may be available with `iostream` without including `string` explicitly. Moreover if `std::string` is unavailable, you would see a compile time error and not issue while debugging. – Mohit Jain Nov 14 '14 at 05:31
  • 1
    @MohitJain true, but with `iostream`, you call `std::cin.getline(char*, streamsize)`. With `string`, you call `std::getline(std::istream, std::string)`. – CodeMonkey Nov 14 '14 at 16:05
1

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;
}
Naseef Chowdhury
  • 2,357
  • 3
  • 28
  • 52
  • Why are you saying it's ok to be using system("pause")? Don't you think that the beginner would benefit from some look into how to actually reverse a string in c++? `string(s.rbegin(), s.rend())` implying s is a string? – user2913685 Nov 14 '14 at 22:10
  • Actually don't understand what you are talking about. System("pause") actually able us to see the output window after execution of program completed. It has nothing to do with string reverse. – Naseef Chowdhury Nov 17 '14 at 08:06