-2

In my program it should be an option to ask a user for input, and then save input string into the file. My problem is, - when I put cin in any of it forms, inside Switch, program will stuck circling indefinitely, right after i press enter after finish typing new text. What could cause the problem?


#include <iostream>
#include <fstream>
#include <iterator>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <algorithm>
#include <chrono>
#include <random>
#include <vector>

using namespace std;

void changePlainText()
{
    ofstream nFile("plaintext.txt");
    string newText;
    cout << "Enter new plain text" << endl;
    getline(cin, newText);
    nFile << newText;
    nFile.close();
}

int main()
{
    int uInput = 0;
    do
    {
        printf("2.Change content of the plain text file: \n");
        cin >> uInput;
        switch (uInput)
        {
        case 1:
             break;
        case 2:
            changePlainText();
            break;
        }
    } while (uInput != 5);
    cout << "Closing program" << endl;
    system("pause");
}

After I type something in console and press enter, the program enters never ending circle. It still stuck even if I just write simple cin >> i, in switch case.

Stiven
  • 31
  • 3
  • 3
    That indentation is all over the place. – sweenish Aug 18 '22 at 11:49
  • The code provided is not considered an [mre] since it is not self-contained. It's missing includes, makes references to functions that you have not provided, etc. Consider editing it into an acceptable form. – sweenish Aug 18 '22 at 11:56
  • [Not reproduced](https://godbolt.org/z/55rEW5snn) – pptaszni Aug 18 '22 at 12:11
  • I removed everything which isn't part of the problem from the code, godbolt.org complied it, but it still gave me save result. – Stiven Aug 18 '22 at 12:20
  • 2
    Mixing formatted input (`std::cin >> i`) with unformatted input (`std::getline(std::cin, newText)` can lead to complications. The stream extractor leaves whitespace in the input stream; if that whitespace is a newline, getline reads that empty line. – Pete Becker Aug 18 '22 at 12:53
  • The code doesn't need to call `nFile.close();`. The destructor will do that. – Pete Becker Aug 18 '22 at 12:53
  • @ Pete Becker Im trying to do sort of menu inside my console program, does there no way around this problem? I tired this solution https://stackoverflow.com/a/20485924/17431663 but godbolt.org produces same errors with the code inside the solution. – Stiven Aug 18 '22 at 13:15
  • `std::cin.ignore()` is the naive solution. The better solution is to ensure the stream is empty by passing non-default values to that function. This should have been covered by your professor or learning resource. – sweenish Aug 18 '22 at 13:17
  • Does this answer your question? [c++ getline() isn't waiting for input from console when called multiple times](https://stackoverflow.com/questions/7786994/c-getline-isnt-waiting-for-input-from-console-when-called-multiple-times) – Hackjaku Aug 19 '22 at 07:55

1 Answers1

-1

Take a look at this question. I debugged your code and I experienced that exact behavior. The accepted answer explains quite well what's happening and also provides a solution.

I tried with boost, I changed

cin >> uInput;

to

string inputString; // so you know what inputString is
    
getline(cin, inputString);
uInput = boost::lexical_cast<int>(line);

and it's working fine now.

Hackjaku
  • 84
  • 1
  • 10
  • If there is a duplicate, you flag the question as such and move on. You don't use the dupe in your own answer. – sweenish Aug 18 '22 at 16:12
  • I see your point, and I may agree with you, however I wanted to provide some more info. the [rules](https://stackoverflow.com/help/privileges/comment) state that a comment would have been more appropriate, but I am not allowed to. So I followed [this](https://stackoverflow.blog/2010/11/16/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/?_ga=2.22434809.391673736.1660633583-502282724.1648558265) principle trying to be of some help. – Hackjaku Aug 19 '22 at 08:09
  • No, in the case of a duplicate, you flag it as such and move on. The inability to comment does not allow you to do so in other ways. Flagging as a duplicate also leaves a comment on the question. – sweenish Aug 19 '22 at 12:00
  • I didn't knew that and I'll keep in mind. As you can see, I did also that after your advice. However, let me suggest you reading [this](https://stackoverflow.blog/2010/11/16/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/?_ga=2.82254932.391673736.1660633583-502282724.1648558265). Someone "new" like me could be mislead. – Hackjaku Aug 19 '22 at 14:24
  • This answer doesn't fit the mold of what they're talking about. – sweenish Aug 19 '22 at 14:25