while loop crashes after the 4th entry

I have created this code in which I am proud of to have gotten working on my own after expanding it from a tutorial, still I find that the console crashes after the 4th entry any reason why? (I also probably have to get it to save each new input on a new line in the text file as it only save the last one)

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



int main(){

    ofstream theFile("players.txt");

    cout << "Enter your players ID, Name, and funds" << endl;
    cout << "press Ctrl+Z to quit\n" << endl;

    int idNumber;
    string name;
    double money;
    char repeat;

            do {
            cout << "Enter your Player ID" << endl;
            cin >> idNumber;
            cout << "Enter your Players Name" << endl;
            cin >> name;
            cout << "Enter your Players Funds" << endl;
            cin >> money;
            cout << "do you have any other Players, type yes or no" << endl;
            cin >> repeat;


            }while(repeat=='y');

            if(repeat=='n'){



             theFile << idNumber << ' ' << name << ' ' << money << endl;



            }else{theFile << idNumber << ' ' << name << ' ' << money << endl;}

}

Last edited on
It is 100% impossible for a sigsev (segfault) to be called. But it is possible for the code to go into an infinite loop, which cannot give a "not responding error" so that confuses me. Ctrl-Z will not exit your loop since you don't error check.

Overall you aren't checking for errors, which will cause an infinite loop, so do that first. everything going out of std::cin can fail and you need to check it by doing if(cin >> value) (even strings and char can fail if you enter ^Z).

By 4th entry I assume you mean "yes" and when you do cin >> repeat; it only takes a single letter from the input since you are only using a char, so next time you get cin >> idNumber; your stream is now corrupted since "es" cannot turn into a number, and that is why you need to error check.

Also if you want names that have spaces in them, you need to use getline, but you need to google how to fix the problem of cin+getline not working together.
Doesn't crash after the 4th entry for me, and I haven't seen a reason for it to in my skim over.
Better formatting helps.
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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ofstream theFile("players.txt");

  cout << "Enter your players ID, Name, and funds" << endl;
  cout << "press Ctrl+Z to quit\n" << endl;

  int idNumber;
  string name;
  double money;
  char repeat;

  do {
    cout << "Enter your Player ID" << endl;
    cin >> idNumber;
    cout << "Enter your Players Name" << endl;
    cin >> name;
    cout << "Enter your Players Funds" << endl;
    cin >> money;
    cout << "do you have any other Players, type yes or no" << endl;
    cin >> repeat;
  } while (repeat == 'y');

  if (repeat == 'n') {
    theFile << idNumber << ' ' << name << ' ' << money << endl;
  } else {
    theFile << idNumber << ' ' << name << ' ' << money << endl;
  }
}


> cout << "press Ctrl+Z to quit\n" << endl;
If you press ctrl-z into code such as yours that has no handling for ctrl-z, then you're going to see endless scrolling of your prompts. Because each of your cin will return immediately with a eof status (which you're not checking).

yeah the cout << press ctrl+z I forgot to remove as one can just quit with n, and weird @zapshe, wondering why it crashes on me? could it be RAM as one of my cards just fried the other day?

also @potato I use y/n but forgot to change the text in cout from yes/no to y/n.

Wondering why it crashes on my end then.
Topic archived. No new replies allowed.