Getline Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compman9902
    New Member
    • Mar 2007
    • 105

    Getline Problem

    Thank you for reading this post, first of all.
    My problem is that whenever I try to use the following code:
    Code:
    std::getline(cin,original);
    Or, the getline method, it compiles fine, but when I run the program, it dosn't allow the user to enter anything. In other words, the length of the string original is set to zero automatically. When I use the following code:
    Code:
    cin  >> original
    it works perfectly, but with that method, it cannot have spaces in the input. Here is the code I'm trying to use in cotext:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <sstream>
    #include <string.h>
    #include <fstream>
    using namespace std;
    int main()
    {
    string original;
    cout << "Please enter in the string:" << endl;
    cout << "String: " << endl;
    std::getline(cin,original);
    cout << "Your String Is: \n\n\n" << endl;
    cout << original;
    return 0;
    }
    Thank you for your help and your time.

    --Parker Woods
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    the program works fine using g++ and Borland CBuilder. What compiler / operating systems are you using?

    Comment

    • compman9902
      New Member
      • Mar 2007
      • 105

      #3
      Originally posted by horace1
      the program works fine using g++ and Borland CBuilder. What compiler / operating systems are you using?
      Sorry, my mistake. I forgot to put my compiler and OS.
      Operating System: Windows XP Home Edition (Service Pach Two)
      Compiler: DEV-C++ 4.9.9.2

      This should tell yo enough. But thanks for telling we which systems it works on :).

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        I am using close to the same setup as you and it is working. The problem I think is that the output screen disappears before you see the output. you can insert a line like
        system("pause") ;
        befiore your return and you should see the output.

        Comment

        • compman9902
          New Member
          • Mar 2007
          • 105

          #5
          Originally posted by compman9902
          Sorry, my mistake. I forgot to put my compiler and OS.
          Operating System: Windows XP Home Edition (Service Pach Two)
          Compiler: DEV-C++ 4.9.9.2

          This should tell yo enough. But thanks for telling we which systems it works on :).
          Also, I'm sorry I didn't say this earlier, but i'm using this in quite a few loops and CASE statments. I heard on a different post (not sure which one) that this may effect it. Here is the code in my actual program (the other was just a dummy :))
          Code:
          #include <iostream>
          #include <stdlib.h>
          #include <string.h>
          #include <time.h>
          #include <sstream>
          #include <string.h>
          #include <fstream>
          using namespace std;
          int main()
          {
          int loopOne;
          int selectMode;
          int length;
          string original;
          string line;
          string fileOpen;
          loopOne = 1;
          while (loopOne !=0)
          {
          cout << "\n\n\nWhat Would You Like To Do:" << endl;
          cout << "Read A Text (.TXT) File - Option [1]" << endl;
          cout << "Write A Text (.TXT) File - Option [2]" << endl;
          cout << "\nPlease Select An Option Number: ";
          cin  >> selectMode;
          switch (selectMode)
          {
          case 1:
          cout << "You Have Chosen To Read A Text File" << endl;
          cout << "\nPlease Enter The Path For The File To Be Read:\n" << endl;
          cin  >> fileOpen;
          ofstream fileopen;
          fileopen.open(fileOpen.c_str());
          while (! fileopen.eof() )
          {
          getline (fileopen,line);
          line = line + "\n";
          original = original + line;
          }
          fileopen.close();
          }
          }
          return 0;
          }
          Once again, it is only the "getline (fileopen,line) ;" line that is bothering me. Thank you for you help, time, wisdom, and allknow power.

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            in
            Code:
            ofstream fileopen;
            fileopen.open(fileOpen.c_str());
            while (! fileopen.eof() )
            {
            getline (fileopen,line);
            fileopen is a ofstream(output stream) and then you attempt to read from it - change it to an input stream?

            Comment

            • compman9902
              New Member
              • Mar 2007
              • 105

              #7
              Originally posted by horace1
              in
              Code:
              ofstream fileopen;
              fileopen.open(fileOpen.c_str());
              while (! fileopen.eof() )
              {
              getline (fileopen,line);
              fileopen is a ofstream(output stream) and then you attempt to read from it - change it to an input stream?
              This may sound really dumb but..how?

              Comment

              • compman9902
                New Member
                • Mar 2007
                • 105

                #8
                Originally posted by compman9902
                This may sound really dumb but..how?
                Never mind..Just change the o to a i. Wow..talk about logic.
                Thanks you guys fixed it once again :)
                But, please read the first post's problem. It is strange..No?
                Well, I'll be posting all night..I have a feeling we'll get to know each other soon--up and comming programmer you know?
                Just look a my profile ;)
                Well, thanks again
                Bye

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  Originally posted by compman9902
                  Never mind..Just change the o to a i. Wow..talk about logic.
                  Thanks you guys fixed it once again :)
                  But, please read the first post's problem. It is strange..No?
                  Well, I'll be posting all night..I have a feeling we'll get to know each other soon--up and comming programmer you know?
                  Just look a my profile ;)
                  Well, thanks again
                  Bye
                  tried your first program using DEV-C++ and it works OK

                  Comment

                  • compman9902
                    New Member
                    • Mar 2007
                    • 105

                    #10
                    Originally posted by horace1
                    tried your first program using DEV-C++ and it works OK
                    I know..It was just a dummy as I said later.
                    But the problem that I stated in that post was what I wanted you to look at
                    But the issue is resolved and I no longer require your assistance...Fo r now
                    Thank you



                    --Parker Woods

                    Comment

                    Working...