The while loop problem

Hey. I know you can be able to notice that am a new person in this area of C++ programming especially from how I have organized my program. Of late, I have got much interest in the fstream and after a lot of research on what it is and what it is cable of doing, I decided to do my first code that instructs the user to do an input of something, creates a new line for the input, and then does the printing of all the input contents in a file.
Help me to understand why the while loop is not printing.

Thanks all!

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
72
73
74
75
76
77
  #include <iostream>
  #include <fstream>
  #include <string>
  using namespace std;

int main()
{
    string const filePath("fstream landfill/Project8 (test fstream)/test.txt");
    ofstream testOutFlux(filePath.c_str()), testOutFluxMod(filePath.c_str(), 
    ios::app); 
    ifstream testInFlux(filePath.c_str()); 

    if(testOutFlux && testInFlux) //check that both streams have been 
    //successfully set up
    {
        string testLine, testInput;
        cout << "Succesfully opened " << filePath << " , what to add?" << 
        endl;

        getline(cin, testInput); 
        testOutFlux << testInput << endl; //deletes and replaces the file's data 
        //with testInput's content

        getline(testInFlux, testLine); //modifies testLine so as to be a copy of 
        //the file's data

        cout << endl << "successfully modified " << filePath << endl;
        cout << "it contains: " << endl << testLine << endl << endl;

        char Continue;
        do
        {
            cout << "add something?" << endl;
            cin >> Continue;

            if(Continue == 'y')
            {
                cout << endl << "what to add?" << endl;

                cin.ignore(); //ignores preceding uses of cin
                getline(cin, testInput); //modifies testInput to contain a line
                testOutFluxMod << testInput; //adds testInput at the end of  
                //the file rather than replacing it

                testInFlux.ignore(); //ignores preceesing uses of testInFlux
                getline(testInFlux, testLine); //modifies testLine to contain 
                //1 line of testInFlux's file

                cout << endl << "successfully modified " << filePath << endl;
                cout << "it now contains: ";

                testInFlux.ignore(); //ignores preceding uses of testInFlux
                
                string line; 
                while(getline(testInFlux, line)) 
                {
                    testOutFlux << line << endl;
                }
                cout << endl;
            }

            else
            {
                cout << endl << "closing program" << endl;
            }

        }while(Continue == 'y');


    }

    else
    {
        cout << "ERROR: could not open " << filePath << endl;
    }

    return 0;

https://www.theengineeringprojects.com/2019/11/introduction-to-enums-in-c-sharp.html
Last edited on
There are issues when you have the same file opened with two streams. Perhaps something like:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
	const string filePath("mytest.txt");
	fstream testOutFlux(filePath, ios::app | ios::in);

	if (!testOutFlux)
		return (cout << "Cannot open file\n"), 1;

	cout << "Successfully opened " << filePath << '\n';

	char Continue{};

	do {
		string testInput;

		cout << "what to add?\n";
		getline(cin, testInput);

		testOutFlux.seekp(0, ios::end);
		testOutFlux.clear();
		testOutFlux << testInput << '\n';

		cout << "\nsuccessfully modified " << filePath << "\nit now contains:\n";

		testOutFlux.clear();
		testOutFlux.seekg(0);

		for (string line; getline(testOutFlux, line); )
			cout << line << '\n';

		cout << "\nadd something (y/n)? ";
		cin >> Continue;
		cin.ignore();
	} while (Continue == 'y' || Continue == 'Y');

	cout << "\nclosing program\n";
}

Last edited on
Topic archived. No new replies allowed.