Problem with "gets()" or cin.get()

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>
using namespace 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;
}


Many thanks for your help!
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.

-unoriginal
Last edited on
Many thanks unoriginal, now it works OK after the adding of cin.get();

However when I try to use the getline() I get a error:

no matching function for call to `getline(char[20])'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdio>
//#include <conio2.h>
#include <stdio.h>
#include <windows.h>
CHAR GetCh (VOID)
{
  HANDLE hStdin = GetStdHandle (STD_INPUT_HANDLE);
  INPUT_RECORD irInputRecord;
  DWORD dwEventsRead;
  CHAR cChar;

  while(ReadConsoleInputA (hStdin, &irInputRecord, 1, &dwEventsRead)) /* Read key press */
    if (irInputRecord.EventType == KEY_EVENT
	&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_SHIFT
	&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_MENU
	&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_CONTROL)
    {
      cChar = irInputRecord.Event.KeyEvent.uChar.AsciiChar;
	ReadConsoleInputA (hStdin, &irInputRecord , 1, &dwEventsRead); /* Read key release */
	return cChar;
    }
  return EOF;
}
using namespace std;

int main(){
    int age;
    char name_surname[20];
    cout<<"Age: ";
    cin>>age;
    cin.get();
    cout<<"Name and surname: ";
    getline(name_surname);
    cout<<"Your name is "<<name_surname<<"and your age is "<<age;
    GetCh();
    return 0;
}


Many thanks
Last edited on
Preferred solution:
1
2
    string name_surname;
    getline( cin, name_surname );

Alternate solution:
1
2
    char name_surname[20];
    cin.getline( name_surname, 20 );

Twenty characters isn't very much space for a complete name. What if my name were "Viacheslav Kalishnikov"? Or "Maria Sanchez Hernandez"? Etc.

Hope this helps.

[edit] Fixed typo. (Thanks mcleano.) [/edit]
Last edited on
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 :-(
Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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!
Many thanks Duoas! (and sorry for the late reply :-( )
I've managed to solve it;
Thanks again :-)
Your error is because you are using getline improrperly. In your case, you would use it like this: getline(cin, name_surname);
Topic archived. No new replies allowed.