gets() serves! cin.getline() evil! HELP!

Hello world!
The Epic starting program for every programming language ;) and hi frnds, this is my first post in this forum... nw coming to the matter, i read in many places gets() is EVIL and use cin.getline() instead of it... i actually like prefer cin.getline() over gets() as i know the problem with gets()... but this program gives the contrary...
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
#inlclude <iostream.h>
#include <conio.h>
#include <process.h>
int strlen(char*);              //Am trying to re-create strlen() on my own!
void main ()                               //Turbo-c++
{
  clrscr();
  for( ; ; )                     //For program repetition...
  {
  char s1[50];
  cout<<"Enter a string : ";
  cin.getline(s1,50);
  cout<<"No. of characters : "<<strlen(s1);
  repeat:
  cout<<endl<<"Do you want to continue? (y/n) : ";
  char ch;cin>>ch;
  if(ch=='N'||ch=='n')
  exit(0);
  else if(ch=='Y'||ch=='y')       //Actually not required... :P
  continue;
  else
  {
   cout<<"Invalid character...";
   goto repeat;
  }
  }
}
int strlen(char*)
{
  for(int i=0; ;i++)
  {
  if(x[i]=='\0')
  break;
  }
  return i;
}

Sorry for the big prog guys but its big only cause of the repetition... now the problem is, when i first execute program, it alright... but for the second time..
O/P:
Enter a string : asd
3
Do you want to continue? (y/n) : y
Enter a string : 0
Do you want to continue? (y/n) :

Actually i didnt enter any string!!! i didnt enter the 0! i just entered y and the comp entered the remaining things(please try urselves and u will see the o/p)...WHY the hell cin.getline failed to get the input string from the user and from where that 0 came from?!?! :O
Now the best part is, using gets(s1) instead of cion.getline(s1,50) yields the correct O/P!!!! Try and see guys! gets() serves its function here whereas cin.getline becomes evil! WHY?!?!?! PLEASE EXPLAIN! Am beginner, so please give your answers in detail... Actually am a 11th standard student studying c++ in skul :P please go easy on me guys!
Last edited on
Well, try using cin.ignore() and/or cin.clear(), search about these in the forums.
Last edited on
Hi...newbie--oh at least in this forum,

You must bring cin.ignore(); or cin.clear(); or cin.sync(); after declaring s1. What it does is to clear the buffer. This is needed because, getline() accepts characters until a newline character('\n') is "seen", and this newline character might have been left in the buffer when the user hit the enter(return) key.

------Other Tips-------
Always use code tags when posting a code, in order to receive more replies.
For info on how to use code tags, visit: http://www.cplusplus.com/forum/articles/42672/

Hope it helps,
Aceix.
Last edited on
thanx for ur replies guys... but how to get the work done with cin.ignore() or cin.clear()? just call tat function without any parameters?
OH! ya it worked! thanx samrux and Aceix :) now the next question is... Why cin.getline needs this extra function and why doesnt gets() needs it?
Last edited on
getline doesn't need this extra function. You do, because you're mixing line-oriented input with single character extraction. The only reason gets works is because you were also mixing C and C++ input methods and it only works by happenstance. If you were using all C methods, gets would have the same issue.
well i cant understand fully what u r saying actually :P
you're mixing line-oriented input with single character extraction

actually i was trying to get a string from the user.. whats that mixing line-oriented input with single character extraction means?? and from where the 0 came from?
mixing C and C++ input methods and it only works by happenstance

i understand tat i get the string thro' a C function... so... i dunno C language actually.. but then intermixing C and C++ methods wud b a good idea?
Topic archived. No new replies allowed.