Endless Do-While loop

I'm currently having an issue with a do-while loop never ending. I'm not sure what I am doing wrong, though its probably something wrong. I'm pretty sure it has to do with the outer loop because it isnt giving me invalid and I'm pretty sure my i counter is going up. The issue is that whenever i type "New" to start the next paragraph, it just re-prompts the EXIT/New loop. Thanks!

This was coded in Code::Blocks on a Raspberry Pi Zero

EDIT: After adding a cout line to display i before the getline, I've discovered it has to do with the getline.

EDIT 2: After confirming this I have discovered the issue is mixing cin inputs, namely the cin inside the inner loop wasn't wiping the enter from the buffer and was causing the getline to receive an empty line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string name;
    string text[20];
    string op1;
    bool esc = false;
    bool reset = false;
    int i = 0;

    cout << "enter name for file (extension will be added):  ";

    getline(cin, name);

    name = "/home/pi/Documents/" + name + ".txt";

    ofstream output;

    output.open( name.c_str() , ofstream::app);

    cout << "\nBegin typing, strike return for new paragraph or to EXIT\n\n";

    do
    {
        getline(cin, text[i]);

        do
        {
        reset = false;

        cout << "\nNew paragraph or EXIT?(New/EXIT)" << endl;
        cin >> op1;

        if (op1 == "New")
        {
        esc = false;
        i++;
        }

        else if (op1 == "EXIT")
        {
        esc = true;
        }

        else
        {
        cout << "Invalid input!";
        reset = true;
        }

        }while(reset == true);

    }while(esc == false);

    for(int j = 0; j <= i; j++)
    {
        output << text[j] << "\n\n";
    }

    output.close();



    return 0;
}
Last edited on
Take a look at the inner do-while loop. When is reset ever false?

edit - on a side note, I'm having a hard time following your logic here. Can you explain the purpose of the loops?
Last edited on
The reset variable is initialized to false, and is set to false any time the loop starts, as it is the first thing the inner loop does. Mind you, a lot of this stuff probably isnt convention, as I'm just starting out in coding, so I'm doing what makes sense to me.

The outer loops purpose is to take in text using the getline, and put it in text[i]. after the text has been entered, the inner loop asks if you want to exit the program, do another paragraph, or spits out an error and resets the loop. If Exit is entered, it then exits the outerloop and goes into the for loop, which loads it into the file.
Last edited on
Does this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
    string name;
    string text[20];
    string op1;
    bool esc = false;
    bool reset = false;
    int i = 0;

    cout << "Enter name for file (extension will be added):  ";
    getline(cin, name);
    name = "/home/pi/Documents/" + name + ".txt";

	ofstream output;
    output.open(name.c_str() , ofstream::app);

    cout << "\n\nBegin typing, strike return for new paragraph or to EXIT\n";

    do
    {
        getline(cin, text[i]);

        do
        {
	        reset = false;
	
	        cout << "\nNew paragraph or EXIT?(New/EXIT)\n";
	        cin >> op1;
	
	        if (op1 == "New")
	        {
		        esc = false;
		        i++;
	        }
	        
	        else if (op1 == "EXIT")
	        {
	        	esc = true;
	        }
	        
	        else
	        {
		        cout << "Invalid input!\n";
		        reset = true;
	        }
	
	    } while (reset == true);

	    cin.ignore(); // Use this to continue to next input
	    i++;

	} while (esc == false);
	
    for(int j = 0; j <= i; j++)
    {
        output << text[j] << "\n\n";
    }

    output.close();

    return 0;
}
Topic archived. No new replies allowed.