I've just wrote this program and when i compile, the gets() function is skip before i can enter the string. i have try cin.getline but the same problem occurs. Can anybody tells me what's the problem and to solve it?

#include<iostream>
#include<cctype>
using namespace std;

int main ()
{	char str[10]="ABCDE FG", ch, solve[10], guess[10];
	int select;	

	cout << "Word Puzzle\n"
		 << "===========\n\n\n";

	for(int i=0; str[i]!='\0'; i++) 
		if(i!=5)
			cout << "_ ";
		else
			cout << " ";
	
	cout << "\n\n\n\n";

	do {
		cout << "1) Enter a character\n2) Solve the puzzle\n3) Exit\n";
		cin >> select;

		if(select==1) {
			cout << "\nPlease enter a character:  ";
			cin >> ch;
			ch=toupper(ch);
	
			cout << "\n\n";
		
			for(i=0; str[i]!='\0'; i++)
				if(ch==str[i])
					guess[i]=ch;
				else if(str[i]==' ')
					guess[i]=str[i];
				
			for(i=0; str[i]!='\0'; i++) 
				if( guess[i]==str[i])
					cout << guess[i];
				else
					cout << "_ ";
			
				guess[i]='\0';
		}

		else if(select==2) {
			cout << "\nSolve the puzzle:";
	                  gets(solve);
		
			
			if( strcmp(solve, str)!=0 )
				cout << "Your answer is wrong...";
		}

		else
			return 0;
		
		cout << "\n\n";

	} while(strcmp(solve, str)!=0 && strcmp(guess, str)!=0);
	
	if( strcmp(solve, str)==0 || strcmp(guess, str)==0)
		cout << "Well done!!! You got the correct answer!!!\nThe answer is ABCDE FG.";

	cout << "\n\n\n";

	return 0;
}

Hi,
The problem is that we need to clear the input buffer before using gets() because gets() read anything in the input buffer until it encounters a new line or return character. Although it works correctly, it is unsafe to use gets() if the input is bigger than the array can be stored, it writes over the array as we are not expected. Good luck with your program.

Here is the modified code:

#include<iostream>
#include<cctype>
using namespace std;


int main ()
{   char str[10]="ABCDE FG", ch, solve[10], guess[10];
int select;


cout << "Word Puzzle\n"
<< "===========\n\n\n";


for(int i=0; str!='\0'; i++)
if(i!=5)
cout << "_ ";
else
cout << " ";


cout << "\n\n\n\n";


do {
cout << "1) Enter a character\n2) Solve the puzzle\n3) Exit\n";
cin >> select;


if(select==1) {
cout << "\nPlease enter a character:  ";
cin >> ch;
ch=toupper(ch);


cout << "\n\n";


for(i=0; str!='\0'; i++)
if(ch==str)
guess=ch;
else if(str==' ')
guess=str;


for(i=0; str!='\0'; i++)
if( guess==str)
cout << guess;
else
cout << "_ ";


guess='\0';
}


else if(select==2) {
cout << "\nSolve the puzzle:";


// To clear the input buffer
fflush(stdin);


gets( solve);


if( strcmp(solve, str)!=0 )
cout << "Your answer is wrong...";
}


else
return 0;


cout << "\n\n";


} while(strcmp(solve, str)!=0 && strcmp(guess, str)!=0);


if( strcmp(solve, str)==0 || strcmp(guess, str)==0)
cout << "Well done!!! You got the correct answer!!!\nThe answer is ABCDE FG.";


cout << "\n\n\n";


return 0;
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Comments
though i dont personally quite understand C++ your effort to help does not go unrewarded --KT

fgets(), i've try but didn't work...

about fflush(stdin), if didn't use this function then is there any other function can replace?

fgets(), i've try but didn't work...

Using fgets, the newline may be retained in the string -- it then becomes part of the comparison. If you don't want the newline at the end of the string, remove it.

if(fgets(solve, sizeof solve, stdin))
    {
       size_t end = strlen(solve) - 1; /* find position of last character */
       if(solve[end] == '\n') /* see if it is a newline */
       {
    	  solve[end] = '\0'; /* get rid of the newline */
       }
  }

about fflush(stdin), if didn't use this function then is there any other function can replace?

First, learn to use fgets correctly. Or else realize that it was the leftover character(s) that was causing problems. Reading leftover characters up to and including a newline can be done a variety of ways. Here are a few .

Comments
Very usefull block of code to those that need to find a way around using just gets() ver much appreciated --KT

THe C++ Standard suggest we ought to using cin to get char or strings. you can use vector to store the strings. vector is a object but like a array.

type it twice, for example :
char variable[100];
gets(variable);
gets(variable);
it'll work.
edit :
sorry, you have asked why. im sorry i dont know why, all i can do to help is above :/

Comments
Perhaps read previous posts, like never use gets(), and post dates, like 4 YEARS AGO
//....
else if(select==2) {
	cout << "\nSolve the puzzle:";
	//----
        cin.ignore();
        //----
        getline(cin,solve);
//....
}
This article has been dead for over six months. Start a new discussion instead.