why gets() is skip in this program?

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Jul 2004
Posts: 17
Reputation: lara_ is an unknown quantity at this point 
Solved Threads: 0
lara_'s Avatar
lara_ lara_ is offline Offline
Newbie Poster

why gets() is skip in this program?

 
0
  #1
Sep 10th, 2004
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;
}
Quick reply to this message  
Join Date: May 2004
Posts: 15
Reputation: tlee is an unknown quantity at this point 
Solved Threads: 0
tlee tlee is offline Offline
Newbie Poster

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

 
1
  #2
Sep 10th, 2004
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;
}
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #3
Sep 10th, 2004
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Quick reply to this message  
Join Date: Jul 2004
Posts: 17
Reputation: lara_ is an unknown quantity at this point 
Solved Threads: 0
lara_'s Avatar
lara_ lara_ is offline Offline
Newbie Poster

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

 
0
  #4
Sep 11th, 2004
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?
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
1
  #5
Sep 11th, 2004
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.
  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. }
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Quick reply to this message  
Join Date: Aug 2004
Posts: 24
Reputation: XianBin is an unknown quantity at this point 
Solved Threads: 0
XianBin XianBin is offline Offline
Newbie Poster

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

 
0
  #6
Sep 12th, 2004
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.
Quick reply to this message  
Join Date: Jul 2008
Posts: 1
Reputation: member19 has a little shameless behaviour in the past 
Solved Threads: 0
member19 member19 is offline Offline
Newbie Poster

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

 
-1
  #7
Dec 18th, 2008
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.
Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

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

 
0
  #8
Dec 18th, 2008
  1. //....
  2. else if(select==2) {
  3. cout << "\nSolve the puzzle:";
  4. //----
  5. cin.ignore();
  6. //----
  7. getline(cin,solve);
  8. //....
  9. }
.:-cikara21-:.
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #9
Dec 18th, 2008
Must you people dredge up four year old threads?
I'm here to prove you wrong.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC