Hello
I have a problem with the gets() and cin.get() functions when the same program uses also the "cin>>" one. When I compile and run, the "gets()" function skips and the program continues, not leaving me to input data.
If I replace the gets and use the cin>> the program runs correctly but I cannot input a string with a space bar. I use wxDev-C++ but in Turbo C++ the code runs correctly.
Here's the code of the program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <stdio.h>
#include <conio2.h>
#include <string>
usingnamespace std;
int main(){
int age;
char name_surname[20];
cout<<"Age: ";
cin>>age;
cout<<"Name and surname: ";
gets(name_surname);
cout<<"Your name is "<<name_surname<<"and your age is "<<age;
getch();
return 0;
}
Sigh, perhaps we should make a sticky or pinned thread on the solution to this common problem.
After line 11 add this cin.get();;
Problem solved -- it was just a trailing new line which functions like gets() use to determine the end of input.
Also, use the C++ getline() -- it is far more safer than the C gets().
Try not to mix C and C++ functions.
About the getch(), don't use it. It is non-standard. You are using <conio2.h>, which is also non-standard.
If you really must use getch and you are on Windows, use this instead: http://www.cplusplus.com/forum/articles/19975/
Another thing, in C++ use #include <cstdio> instead of what you're doing on line 2.
Many thanks but the preferred solution gives me the following error no matching function for call to `getline(std::string&)'
and the alternate solution gives me these:
no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(char[20])'
note D:\wxDevcppPortable7\App\devcpp\include\c++\3.4.5\bits\istream.tcc:582 candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
Oh and the program is only a test one to check the gets() fault that I got from a other huge program so don't worry for the 20 character length of the name and surname :-)
Many thanks and sorry for my inexperience :-(
It sounds like your STL is broken or you are not #including the proper headers. MinGW 3.4.5 sufficiently conforms to the C++ standard that both functions are properly available.
Please don't use the C library gets(). It is a security flaw and should never have been included in the language library. If you must use the C FILE* methods, use fgets(), then just strip the newline from the end of the string. Here's a little function to do it:
char* safe_gets( char* result, int maxchars )
{
char c;
char* nl;
// Get the user's input (hopefully to EOL)
fgets( result, maxchars, stdin );
// Strip the newline from the end of the string
nl = strchr( result, '\n' );
if (nl) *nl = '\0';
else
{
// Get rid of all remaining characters to newline
do c = getchar();
while ((c != EOF) && (c != '\n'));
}
return result;
}
That's the same as gets() but without the possibility of buffer overruns: safe_gets( name_surname, 20 );
If you are using C++, though, you are still better off using the C++ methods.
Good luck!