#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <algorithm>

using namespace std;

//Types and Arrays
string word[16] = {"GOLDFISH", "COMPUTER", "ANONYMOUS", "JACKET", "SHARP", "SERVICE", "EFFORT", "CHARACTER", "CHANGE", "WITHOUT", "PRODUCT", "UNFOLDS", "MUSIC", "MOMENT", "LIFETIME", "PROVIDE"};
char guess[50];
static const char Y= 'Y';
static const char N= 'N';
char ans;

// Functions
int instructions();
int game();



int main()
{
	cout << "\tWelcome to Word Jumble By Khoanyneosr!\n";
	cout << "\nWould you like to see the instructions?"
		 << "\n\t1) Yes = Y"
		 << "\n\t2) No = N\n- ";
		 bool rep= true;

   while (rep)  {
      cin >> ans;
	  cin.ignore(1, ' '); // Figure this out
		
      switch (toupper(ans)) {
         case Y:
			 cin.clear();
			 cin.sync();
            instructions();
            rep= false;
            break;
         case N:
			 cin.clear();
			 cin.sync();
            game();
            rep= false;
            break;
         default:
			 cout << "\n\nInvalid input.\n\n- ";
			 cin.clear();
            rep= true;
            break;
       }
    }
	return 0;
}
int instructions()
{
	system("cls");
	cout << endl << endl << "\tINSTRUCTIONS" << endl;
	cout << endl << "1 - You will be presented with a word. Your job is to guess it." << endl;
	cout << endl << "2 - At the beginning of the game your given 10 points." << endl;
	cout << endl << "3 - You will be given 1 hint. Everytime you have to revisit\nthe hint 1 point will be deducted." << endl;
	cout << endl << "4 - For everytime you guess wrong 1 point will be deducted." << endl;
	cout << endl << "5 - When your point value reaches 0, the game is over." << endl;
	cout << endl << "You can exit at anytime by pressing Q." << endl;
	cout << endl << endl << "\tGood Luck!" << endl << endl << endl;

		system("pause");
		return game();

}
int game()
{
		cout << endl << "what is your guess?\n- ";
			cin.getline(guess, 50);
			
		cout << endl << guess << endl; 

		return 0;
}

That's my code. I need to know how, at the very beginning, how to fix the fact that for every character that is "invalid" it repeats...

Invalid input.
-

Go ahead and compile it if it doesn't make sense. Thanks in advance.

Use ignore with no arguments, which will default to skipping 1 '\n' (you don't want the space like you have it)

Use ignore with no arguments, which will default to skipping 1 '\n' (you don't want the space like you have it)

It still does it. When i enter just random gibberish it still displays multiple "invalid inputs"

Use a construct like this

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

(see the sticky thread on flushing the input stream for more details).

This will ignore a number of characters up until the maximum capacity of the buffer or until it hits a newline.

Since ans is a character, when you enter "adfsffef<enter>" the 'a' is written into ans and all the rest of it stays in the stream, so the loop executes once for each character and the newline.

Is there something about the sticky post at the top of the forum titled Read me: How do I flush the input stream? that you don't understand?

yeah i can't get it to work. It's not flushing the stream, i only need the stream to accept the first value the user inputs, and i don't know how to that.

Use a construct like this

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

(see the sticky thread on flushing the input stream for more details).

This will ignore a number of characters up until the maximum capacity of the buffer or until it hits a newline.

Since ans is a character, when you enter "adfsffef<enter>" the 'a' is written into ans and all the rest of it stays in the stream, so the loop executes once for each character and the newline.

Thanks for explaining it, i read the flushing the stream post and didn't get it.

yeah i can't get it to work. It's not flushing the stream,

This implies you tried changing your code. But since you didn't post the new code, we can't explain what you did wrong.

This implies you tried changing your code. But since you didn't post the new code, we can't explain what you did wrong.

Okay, i'll keep that in mind, but jonsca helped me figure it out. Thanks for the help. =)

This article has been dead for over six months. Start a new discussion instead.