| | |
why gets() is skip in this program?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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;
}•
•
Join Date: May 2004
Posts: 15
Reputation:
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<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;
}
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;
}
"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
•
•
•
•
Originally Posted by lara_
fgets(), i've try but didn't work...
C++ Syntax (Toggle Plain Text)
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 */ } }
•
•
•
•
Originally Posted by lara_
about fflush(stdin), if didn't use this function then is there any other function can replace?
"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
c++ Syntax (Toggle Plain Text)
//.... else if(select==2) { cout << "\nSolve the puzzle:"; //---- cin.ignore(); //---- getline(cin,solve); //.... }
![]() |
Similar Threads
- Convert char *argv to char (C++)
- Pascal Help (Pascal and Delphi)
- adding data into an char array (C++)
Other Threads in the C++ Forum
- Previous Thread: header - library integration
- Next Thread: backgroundimage
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





