Problem with journal program

I'm trying to make a basic journal program.

The primary problem I'm having is with the do while loop. When I input yes/no the loop doesn't end.

Also if you don't mind:

If you can't change the amount of elements in an array, how do you create an additional string to accommodate additional user input?

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

int main () {
  int x = 1, y=1,z=1;
  ofstream myfile ("example.txt",ios::app);
  if (myfile.is_open())
  {


    while (x=1)
    {
    cout << "Type what you want to store\n";
    string entry1;
    getline(cin,entry1);
    myfile << entry1<< "\n\n";
    cout <<"\n\nSuccessfully stored\n\n";

    string response;

    cout <<"Would you like to input more?(type yes or no)\n\n";

    do
    {


        getline(cin,response);

        if (response=="yes")
         {
             y=2;

        }
        else if (response=="no")
        {
            y=2;
            x=2;
        }
        else
            cout << "Type yes or no exactly\n\n";

    }while (y=1);


  }
  }
    else cout << "Unable to open file";

  return 0;
  }



Thanks
Line 43 should be }while (y==1);

Might I suggest also that instead of using an int to keep track of something that is inherently true/false, you instead use the bool type, which takes values of true/false.

I also suggest that you don't use single letter variable names. y means nothing. You could have called it something meaningful like keep_looping
Last edited on
Topic archived. No new replies allowed.