Like Tree2Likes
  • 2 Post By tabstop

Problem in getting "<< gets(string)" to workout

This is a discussion on Problem in getting "<< gets(string)" to workout within the C++ Programming forums, part of the General Programming Boards category; Hi Coders!!! I am developing a Bank Management System ..In one of its provided facility i.e.new account creation i am ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    158

    Post Problem in getting "<< gets(string)" to workout

    Hi Coders!!!
    I am developing a Bank Management System ..In one of its provided facility i.e.new account creation i am having problem with gets(name) .

    Code Snippet:
    Code:
     void account::create_account()
      {
          cout<<"\nEnter The account No.";
          cin>>acno;
          cout<<"\nEnter The Name of The account Holder :\n";
          gets(name);
          cout<<"Your name is :";
          puts(name);      //for check purpose but got it empty every time
          cout<<"\nEnter Type of The account (C/S) : ";
          cin>>type;
          type=toupper(type);
          cout<<"\nEnter The Initial amount(>=500 for Saving and>=1000       for current ) : ";
          cin>> dep;
          while(type=='C' && dep<1000)
            {
          cout<<"Please enter amount >=1000";
          cout<<"\n";
          cin>>dep;
            }
          while(type=='S'&& dep<500)
           {
           cout<<"Please enter amount >=500";
           cout<<"\n";
               cin>>dep;
            }
       cout<<"\n\n\nAccount Created..";
       cout<<"Good Luck :-)" ;
     }

    The Problem is that when i runs this program it asks for other inputs except "name" which it skips i.e. the cursor even doesn't bother to get to the line and so did i am unable to give name.
    Please suggest me the possible reason
    thanks in ADVANCE
    Last edited by jeedi khan; 01-27-2014 at 04:50 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    First, it's getline, not gets -- FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com. Second, mixing getline (which is line-oriented) with << (which is ... ... data-field-oriented, I guess) is going to cause you to have to work a little harder; << leaves the newline character behind (since it's not part of the account number), which means getline sees the newline character and stops, since it's read an entire line. If you just want to make what you have work, you can add an ignore() call before the getline to get rid of the extra newline.
    Elysia and Salem like this.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by tabstop View Post
    First, it's getline, not gets -- FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com. Second, mixing getline (which is line-oriented) with << (which is ... ... data-field-oriented, I guess) is going to cause you to have to work a little harder; << leaves the newline character behind (since it's not part of the account number), which means getline sees the newline character and stops, since it's read an entire line. If you just want to make what you have work, you can add an ignore() call before the getline to get rid of the extra newline.
    Thanks for Providing me an INSIGHT to this Problem but the issue i m is still facing...I am unable to get to the line gets(name) i.e when i enters account no.after being asked the Compiler drives me to enter account type...
    Code:
    void account::create_account()
     {
         cout<<"\nEnter The account No.";
         cin>>acno;
         cout<<"\nEnter The Name of The account Holder :\n";
         gets(name);
         cout<<"Your name is :";
         puts(name);      //for check purpose but got it empty every time
         cout<<"\nEnter Type of The account (C/S) : ";
         cin>>type;​

    If u dont mind and see this snapshotName:  Untitled.jpg
Views: 54
Size:  46.1 KB

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Do you understand tabstop's explanation?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by laserlight View Post
    Do you understand tabstop's explanation?
    tapstop tell me to call ignore before getline but which ignore?

    So i went for fgets
    Code:
    void account::create_account()
    {
    cout<<"\nEnter The account No.";
    cin>>acno;
    cout<<"\nEnter The Name of The account Holder :\n";
    if(fgets(name,sizeof(name),stdin)!=NULL)
      {
          char*p;
          if((p=strchr(name,'\n'))!=NULL)
              *p=0;
      }
    cout<<"Your name is :";
    puts(name);
    cout<<"\nEnter Type of The account (C/S) : ";
    cin>>type;
    Last edited by jeedi khan; 01-28-2014 at 01:35 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Quote Originally Posted by jeedi khan
    HERE is WHAT tapstop tell me to do and i m done with no improvement.
    No, tabstop did not tell you to do that. tabstop wrote: "If you just want to make what you have work, you can add an ignore() call before the getline to get rid of the extra newline."

    Unless you have special reasons to do so, do not mix C-style and C++-style standard I/O. I would expect something along these lines:
    Code:
    void account::create_account()
    {
        cout << "\nEnter The account No.";
        cin >> acno;
        cout << "\nEnter The Name of The account Holder :\n";
        cin.ignore();
        getline(cin, name);
        cout << "Your name is :" << name;
        cout << "\nEnter Type of The account (C/S) : ";
        cin >> type;
    I have used the version of getline for std::string, because name should be a std::string (and if it isn't, you should either have a good reason for that, or fix it). Of course, you should perform error checking in case the user enters invalid input, and perhaps call cin.ignore with specific arguments to cater for the possibility that there's more whitespace to discard.
    Last edited by laserlight; 01-28-2014 at 01:40 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    MoreOver FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com. arguments me to betterly use fgets() as its buffer safe and no issue of getline is discussed there.so i would anyone suggest me how to add ignore

  8. #8
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by laserlight View Post
    No, tabstop did not tell you to do that. tabstop wrote: "If you just want to make what you have work, you can add an ignore() call before the getline to get rid of the extra newline."

    Unless you have special reasons to do so, do not mix C-style and C++-style standard I/O. I would expect something along these lines:
    Code:
    void account::create_account()
    {
        cout << "\nEnter The account No.";
        cin >> acno;
        cout << "\nEnter The Name of The account Holder :\n";
        cin.ignore();
        getline(cin, name);
        cout << "Your name is :" << name;
        cout << "\nEnter Type of The account (C/S) : ";
        cin >> type;
    I have used the version of getline for std::string, because name should be a std::string (and if it isn't, you should either have a good reason for that, or fix it). Of course, you should perform error checking in case the user enters invalid input, and perhaps call cin.ignore with specific arguments to cater for the possibility that there's more whitespace to discard.


    i have a look at here but the writer have made things very much messy ...so what i got basically for flushing the input is to add
    Code:
    #include <ios>
        #include <istream>
        #include <limits>   
    void ignore_line ( std::istream& in ) {   
    in.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
     }

    Should i go for?

  9. #9
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Thanks i have done with..here is what i edited and am presenting here for assistance to anyone who faces the same problem in future.
    Code:
    cout<<"\nEnter The account No.";
    cin>>acno; 
    
    
    cout<<"\n Enter The Name of The account Holder :\n";
    std::cin.ignore ( numeric_limits<std::streamsize>::max(), '\n' );
    getline(cin,name);
    cout<<"Your name is :"<<name;
    The Helping material in this regard besides the respected forum members were from here.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by jeedi khan View Post
    MoreOver FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com. arguments me to betterly use fgets() as its buffer safe and no issue of getline is discussed there.so i would anyone suggest me how to add ignore
    fgets comes with its own problems: buffer management and correctly passing along the buffer size or you risk buffer overflows. std::getline has none of these issues.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 46
    Last Post: 08-24-2007, 05:52 PM
  2. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  3. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21