program ends unexpectedly

This is my code. Everytime I want to run for the 2nd or 3rd time, before i even get a chance to type in a "y" or an "n" the program ends unexpectedly. The error says it is a normal program termination. Is there something wrong with my code?

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
  //test problem

#include <iostream>
#include <string>
#include <vector>
using namespace std;

//Asks the user to play again
bool playAgain(){
  char choice;
  cout<<"Again? y=Yes, n=No: ";
  cin>>choice;
  if (choice=='y'){
    return true;
  }
  else{
    return false;
  }
}

int main() {
  do{
    string text;
    cout<<"Enter text: ";
    getline(cin, text);
    cin.ignore();
  } while (playAgain());
}
Last edited on
It is because you're mixing cin and getline. Put a cin.ignore () right before your getline (cin, text); and that should take care of it.

Best,
max
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

bool playAgain(){
    char choice;
    cout << "Again (y/n)? ";
    cin >> choice;
    cin.ignore(1000, '\n');
    return choice == 'y' || choice == 'Y';
}

int main() {
    do
    {
        string text;
        cout << "Enter text: ";
        getline(cin, text);
        cout << "You entered: " << text << '\n';
    }
    while (playAgain());
}

I tried putting the cin.ignore() on line 43 but now it says exited, illegal instruction. I also made some expansions to my program but commented out the irrelevant parts to it.
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
#include <iostream>
using namespace std;
#include <string>
#include <random>
#include <vector>

/*
string split(string phrase){
  string left;
  string middle;
  string right;
  //int a=rand()%(phrase.length()-2+1)+1;
  //cout<<a;
  //int b=

  
} */


//Asks the user to play again
bool playAgain(){
  int choice;
  cout<<"Again? y=Yes, n=No: ";
  cin>>choice;
  //cin.ignore();
  if (choice==1){
    return true;
  }
  else if (choice==2){
    return false;
  }
}

int main() {
  int times;
  string phrase;
  string newPhrase;
  srand(0);
  do{
    cout<<"How many times would you like to shuffle the phrase? ";
    cin>>times;
    cout<<"Enter the phrase to scramble: ";
    cin.ignore();
    getline(cin, phrase);
    //cin.ignore();
    //for (int i=0; i<times; i++){
      //newPhrase=split(phrase);
      //cout<<"Permutation #"<<i<<": "<<newPhrase<<endl;
    //}
  }while (playAgain());
}
Last edited on
Perhaps something like this for a starter:

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

// Get an integer with prompt
int getInt(const std::string& prm)
{
	int i {};

	while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
		std::cout << "Not an integer\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}

// Asks the user to play again
bool playAgain() {
	int again {};

	do {
		again = getInt("Again? 1=Yes, 2=No: ");
	} while ((again < 1 || again > 2) && (cout << "Not a valid input\n"));

	return again == 1;
}

int main() {
	srand(static_cast<unsigned int>(time(nullptr)));

	do {
		const auto times {getInt("How many times would you like to shuffle the phrase? ")};

		string phrase;

		cout << "Enter the phrase to scramble: ";
		getline(cin, phrase);

	} while (playAgain());
}

Last edited on
Hello coder0101.

Did a little work on your code and came up with this. The comments should help:
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
//test problem

#include <iostream>
#include <limits>  // <--- Added.
#include <string>
#include <vector>
#include <cctype>  // <--- For "std::tolower() and std::toupper()" + others.

using namespace std;  // <--- Best not to use.

//Asks the user to play again
bool playAgain()
{
    char choice{};  // <--- Always good to initialize your variables.

    cout << "Again? y=Yes, n=No: ";
    cin >> choice;

    cin.ignore();  // <--- Only clears 1 character or to "Traits::eof()". Unless another deliniter is specifird. https://en.cppreference.com/w/cpp/io/basic_istream/ignore
    //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. A more portable way.

    if (std::tolower(choice) == 'y')  // <--- What would happen if the user entered 'Y'?
    {
        return true;
    }
    //else
    //{
        return false;
    //}
}

int main()
{
    do
    {
        string text;  // <--- Empty when defined. Does not need initialized. Unless you want to give it a starting value other than ("").

        cout << "Enter text: ";

        getline(cin, text);

    } while (playAgain());

    return 0;  // <--- Not required, but makes a good break point.
}

agent max makes a good point about mixing formatted input, (cin>>choice;, with unformatted input, the "getline". I tend to like following formatted input with the std::cin.ignore(...). statement to make sure that the buffer is clear before any unformatted input. But either way should work.

I will have to try your latest version, but for now I do not see anything wrong with what you did.

Also it helps if you mention what IDE/compiler yo are using and post the complete error message. Others may understand the error message better than you do right now.

Andy

In the function the "else" is not needed. Should the if statement be true you return true and leave the function. Should the if statement be false the if statement will be skipped and the only thing left to do is return false.
Registered users can post here. Sign in or register to post.