Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. using cin.getline() with string

using cin.getline() with string

Scheduled Pinned Locked Moved ATL / WTL / STL
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luis E Cuadrado
    wrote on last edited by
    #1

    Hello everybody: I was wondering if ther is a way to use the cin.getline() function when you use string objects. With the code snipet below I get the following error:

    error C2664: 'class std::basic_istream > &__thiscall std::basic_istream >::getline(char *,int,char)' : cann
    ot convert parameter 1 from 'class std::basic_string,class std::allocator >' to 'char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

    hwproject.exe - 1 error(s), 0 warning(s)

    I always encounter this problem and I don't know what to do to solve this. Is there a better way to do this with string objects? Can you tell me what I did wrong with the code below? Any answer is more than welcome. #include <string> #include <iostream> using namespace std; void main() { int itemNum; string Desc; cout << "Enter Item Number: "; cin >> itemNum; cout << "Enter Description: "; cin.getline(Desc, sizeof(Desc), (char)"\n"); } Luis E. Cuadrado :0)

    V 1 Reply Last reply
    0
    • L Luis E Cuadrado

      Hello everybody: I was wondering if ther is a way to use the cin.getline() function when you use string objects. With the code snipet below I get the following error:

      error C2664: 'class std::basic_istream > &__thiscall std::basic_istream >::getline(char *,int,char)' : cann
      ot convert parameter 1 from 'class std::basic_string,class std::allocator >' to 'char *'
      No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
      Error executing cl.exe.

      hwproject.exe - 1 error(s), 0 warning(s)

      I always encounter this problem and I don't know what to do to solve this. Is there a better way to do this with string objects? Can you tell me what I did wrong with the code below? Any answer is more than welcome. #include <string> #include <iostream> using namespace std; void main() { int itemNum; string Desc; cout << "Enter Item Number: "; cin >> itemNum; cout << "Enter Description: "; cin.getline(Desc, sizeof(Desc), (char)"\n"); } Luis E. Cuadrado :0)

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      One solution is std::getline(). string sInput; std::getline(cin, sInput); Kuphryn

      L 1 Reply Last reply
      0
      • V valikac

        One solution is std::getline(). string sInput; std::getline(cin, sInput); Kuphryn

        L Offline
        L Offline
        Luis E Cuadrado
        wrote on last edited by
        #3

        Thanks for replying Kuphryn. I gave it a try and the program complies but when I run it, the variable where I store the value using the std::getline() function doesn't show up when I use cout.

        #include <iostream>
        #include <string>
        using namespace std;
         
        void main()
        {
        string itemID;
        string Desc;
        cout << "Enter ID: ";
        cin >> itemID;
        cout << "Enter Description: ";
        getline(cin, Desc);
        
        cout << "itemID: " << itemID << endl;
        cout << "Desc: " << Desck << endl;
        
        } 
        

        This is the output that I got.

        Enter ID: 222
        Enter Description: matches
        itemID: 222
        Desc:

        I'm asuming that there are some kind of garbage collecting thing that I have to do here, is it? Let me know what you think. Thanks for your help, Luis E. Luis E. Cuadrado :0)

        J 1 Reply Last reply
        0
        • L Luis E Cuadrado

          Thanks for replying Kuphryn. I gave it a try and the program complies but when I run it, the variable where I store the value using the std::getline() function doesn't show up when I use cout.

          #include <iostream>
          #include <string>
          using namespace std;
           
          void main()
          {
          string itemID;
          string Desc;
          cout << "Enter ID: ";
          cin >> itemID;
          cout << "Enter Description: ";
          getline(cin, Desc);
          
          cout << "itemID: " << itemID << endl;
          cout << "Desc: " << Desck << endl;
          
          } 
          

          This is the output that I got.

          Enter ID: 222
          Enter Description: matches
          itemID: 222
          Desc:

          I'm asuming that there are some kind of garbage collecting thing that I have to do here, is it? Let me know what you think. Thanks for your help, Luis E. Luis E. Cuadrado :0)

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          Hola Luis, There are two problems, one of them is not your fault:

          • std::getline is broken in MSVC++ 6.0. See here[^] for a fix.

          • (this assumes you've installed the previous fix) After reading the int, there are extra characters pending to be read in the first line (at least, the terminating '\n'): eat this before going for the description:

            #include <iostream>
            #include <string>

            using namespace std;
            void main()
            {
            string itemID;
            string Desc;
            cout << "Enter ID: ";
            cin>>itemID;
            cout << "Enter Description: ";
            getline(cin, Desc); // eats what remains of the previous line
            getline(cin, Desc);
            cout << "itemID: " << itemID << endl;
            cout << "Desc: " << Desc << endl;
            }

          Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          L 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            Hola Luis, There are two problems, one of them is not your fault:

            • std::getline is broken in MSVC++ 6.0. See here[^] for a fix.

            • (this assumes you've installed the previous fix) After reading the int, there are extra characters pending to be read in the first line (at least, the terminating '\n'): eat this before going for the description:

              #include <iostream>
              #include <string>

              using namespace std;
              void main()
              {
              string itemID;
              string Desc;
              cout << "Enter ID: ";
              cin>>itemID;
              cout << "Enter Description: ";
              getline(cin, Desc); // eats what remains of the previous line
              getline(cin, Desc);
              cout << "itemID: " << itemID << endl;
              cout << "Desc: " << Desc << endl;
              }

            Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            L Offline
            L Offline
            Luis E Cuadrado
            wrote on last edited by
            #5

            Hola Joaquín, Thank you for your response. I did exactly what you told me and it works. Thank you for pointing me out to that Microsoft site and for your help. Another question that I have is, if I want to do that in a loop. Do I need to flush or wipe-out the stream that I'm using? The reason that I'm asking is that I put everything in a loop except the cout statements and the loops keeps going and it doesn't stop. I step over the code and I noticed that there still data, aparently in the stream. Have you experience this before? Thanks, Luis E. Luis E. Cuadrado :0)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups