943,523 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3941
  • C++ RSS
Sep 10th, 2004
0

why gets() is skip in this program?

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lara_ is offline Offline
17 posts
since Jul 2004
Sep 10th, 2004
1

Re: why gets() is skip in this program?

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[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;
}
Reputation Points: 21
Solved Threads: 0
Newbie Poster
tlee is offline Offline
15 posts
since May 2004
Sep 10th, 2004
0

Re: why gets() is skip in this program?

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 11th, 2004
0

Re: why gets() is skip in this program?

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lara_ is offline Offline
17 posts
since Jul 2004
Sep 11th, 2004
1

Re: why gets() is skip in this program?

Quote originally posted by lara_ ...
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.
C++ Syntax (Toggle Plain Text)
  1. if(fgets(solve, sizeof solve, stdin))
  2. {
  3. size_t end = strlen(solve) - 1; /* find position of last character */
  4. if(solve[end] == '\n') /* see if it is a newline */
  5. {
  6. solve[end] = '\0'; /* get rid of the newline */
  7. }
  8. }
Quote originally posted by lara_ ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 12th, 2004
0

Re: why gets() is skip in this program?

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.
Reputation Points: 15
Solved Threads: 0
Newbie Poster
XianBin is offline Offline
24 posts
since Aug 2004
Dec 18th, 2008
-1

Re: why gets() is skip in this program?

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 :/
Last edited by member19; Dec 18th, 2008 at 2:44 pm.
Reputation Points: 5
Solved Threads: 0
Newbie Poster
member19 is offline Offline
1 posts
since Jul 2008
Dec 18th, 2008
0

Re: why gets() is skip in this program?

c++ Syntax (Toggle Plain Text)
  1. //....
  2. else if(select==2) {
  3. cout << "\nSolve the puzzle:";
  4. //----
  5. cin.ignore();
  6. //----
  7. getline(cin,solve);
  8. //....
  9. }
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 18th, 2008
1

Re: why gets() is skip in this program?

Must you people dredge up four year old threads?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: header - library integration
Next Thread in C++ Forum Timeline: backgroundimage





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC