954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

why gets() is skip in this program?

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;
}
lara_
Newbie Poster
17 posts since Jul 2004
Reputation Points: 10
Solved Threads: 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
#include
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:";

// 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;
}

tlee
Newbie Poster
15 posts since May 2004
Reputation Points: 21
Solved Threads: 0
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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?

lara_
Newbie Poster
17 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 
fgets(), i've try but didn't work...

Usingfgets, 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 .

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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.

XianBin
Newbie Poster
24 posts since Aug 2004
Reputation Points: 15
Solved Threads: 0
 

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 :/

member19
Newbie Poster
1 post since Jul 2008
Reputation Points: 5
Solved Threads: 0
 
//....
else if(select==2) {
	cout << "\nSolve the puzzle:";
	//----
        cin.ignore();
        //----
        getline(cin,solve);
//....
}
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

Must you people dredge up four year old threads?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You