How to read whole line including white space?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JWest46088
    New Member
    • Sep 2006
    • 74

    How to read whole line including white space?

    I am making a program that asks the user to enter some information and then enters it into a text file, but I can't figure out how to read a whole line with spaces in it. When the user enters a line with spaces in it, only the first word appears in the text file. Can anyone help me figure this out? Thanks in advance.

    Here is my program:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
        
    int main ()
    {
      string line;
      ofstream myfile("playlist.txt");
      if(myfile.is_open())
      {
        cout << "Enter artist: ";
        cin >> line;
        myfile << line << endl;
        cout << "Enter album: ";
        cin >> line;
        myfile << line << endl;
        cout << "Enter track: ";
        cin >> line;
        myfile << line << endl;
        cout << "Enter song: ";
        cin >> line;
        myfile << line << endl;
        cout << "Enter genre: ";
        cin >> line;
        myfile << line << endl;
        myfile.close();
      }
      else
        cout << "Unable to open file.";
    
      return 0;
    }


    When I enter 2pac for artist and Greatest Hits for album, it skips the part where the user is supposed to enter the track and goes to the song so I enter the song I Get Around, and it skips the genre part.

    Here is what it does:

    Enter artist: 2pac
    Enter album: Greatest Hits
    Enter track: Enter song: I Get Around
    Enter genre:

    Here is the text file it outputs it to:

    2pac
    Greatest
    Hits
    I
    Get

    Can somebody please help me?
    Last edited by AdrianH; May 4 '07, 05:32 PM. Reason: Please use [code][/code] tags.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by JWest46088
    I am making a program that asks the user to enter some information and then enters it into a text file, but I can't figure out how to read a whole line with spaces in it. When the user enters a line with spaces in it, only the first word appears in the text file. Can anyone help me figure this out? Thanks in advance.

    Here is my program:

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    int main ()
    {
    string line;
    ofstream myfile("playlis t.txt");
    if(myfile.is_op en())
    {
    cout << "Enter artist: ";
    cin >> line;
    myfile << line << endl;
    cout << "Enter album: ";
    cin >> line;
    myfile << line << endl;
    cout << "Enter track: ";
    cin >> line;
    myfile << line << endl;
    cout << "Enter song: ";
    cin >> line;
    myfile << line << endl;
    cout << "Enter genre: ";
    cin >> line;
    myfile << line << endl;
    myfile.close();
    }
    else
    cout << "Unable to open file.";

    return 0;
    }



    When I enter 2pac for artist and Greatest Hits for album, it skips the part where the user is supposed to enter the track and goes to the song so I enter the song I Get Around, and it skips the genre part.

    Here is what it does:

    Enter artist: 2pac
    Enter album: Greatest Hits
    Enter track: Enter song: I Get Around
    Enter genre:

    Here is the text file it outputs it to:

    2pac
    Greatest
    Hits
    I
    Get

    Can somebody please help me?
    You should use the getline() function. That takes a whole line from an input stream:
    Code:
    cin.getline(buffer, size_of_line);

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      I've also used the getline function like this:

      Code:
      getline(cin, myStringVar);
      Keep in mind that, after you call getline, there is still a newline character \n in the stream. You should use a cin.get() call or a cin.ignore() call to get rid of this.

      Comment

      • JWest46088
        New Member
        • Sep 2006
        • 74

        #4
        Originally posted by ilikepython
        You should use the getline() function. That takes a whole line from an input stream:
        Code:
        cin.getline(buffer, size_of_line);
        What is buffer? What would I put in for it?

        cin.getline(buf fer, 10);

        Would that be right?

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          In this case, buffer is a character array, which is the convention for getting strings without using the string class. I'm not sure if cin.getline() will work with a string argument.

          Comment

          • JWest46088
            New Member
            • Sep 2006
            • 74

            #6
            Originally posted by Ganon11
            In this case, buffer is a character array, which is the convention for getting strings without using the string class. I'm not sure if cin.getline() will work with a string argument.
            I got it! Thanks! Don't leave though. I'll probably need more help. ;P Thanks again.

            Comment

            • JWest46088
              New Member
              • Sep 2006
              • 74

              #7
              Originally posted by JWest46088
              I got it! Thanks! Don't leave though. I'll probably need more help. ;P Thanks again.
              I want the user to enter information, such as names, and then I want to ask them after they entered the first name if they want to enter another name. How would I do that?

              This is what I have so far:

              Code:
               cout << "Would you like to enter an artist's information?  ";
                  cin >> answer;
              
                  if(answer = 'Y' || 'y')
                  {
                    cout << "Enter artist: ";
                    cin.getline(line, 50);// >> line;
                    myfile << line << endl;
                    cout << "Enter album: ";
                    cin.getline(line, 50);// >> line;
                    myfile << line << endl;
                    cout << "Enter track: ";
                    cin.getline(line, 50);// >> line;
                    myfile << line << endl;
                    cout << "Enter song: ";
                    cin.getline(line, 50);// >> line;
                                   .
                                   .
                                   .
              }
              Here's what happens:

              Would you like to enter an artist's information? y
              Enter artist: Enter album: 2pac
              Enter track: 2
              Enter song: f
              Enter genre: a

              It skips where you enter the artist name. Also, it doesn't matter whether you put y or a;osidhfasd in after it asks to enter more info. It goes on no matter what.
              Last edited by AdrianH; May 4 '07, 05:34 PM. Reason: Please use [code][/code] tags

              Comment

              • ilikepython
                Recognized Expert Contributor
                • Feb 2007
                • 844

                #8
                Originally posted by JWest46088
                I want the user to enter information, such as names, and then I want to ask them after they entered the first name if they want to enter another name. How would I do that?

                This is what I have so far:

                cout << "Would you like to enter an artist's information? ";
                cin >> answer;

                if(answer = 'Y' || 'y')
                {
                cout << "Enter artist: ";
                cin.getline(lin e, 50);// >> line;
                myfile << line << endl;
                cout << "Enter album: ";
                cin.getline(lin e, 50);// >> line;
                myfile << line << endl;
                cout << "Enter track: ";
                cin.getline(lin e, 50);// >> line;
                myfile << line << endl;
                cout << "Enter song: ";
                cin.getline(lin e, 50);// >> line;
                .
                .
                .
                }

                Here's what happens:

                Would you like to enter an artist's information? y
                Enter artist: Enter album: 2pac
                Enter track: 2
                Enter song: f
                Enter genre: a

                It skips where you enter the artist name. Also, it doesn't matter whether you put y or a;osidhfasd in after it asks to enter more info. It goes on no matter what.
                Try adding cin.ignore() after you ask for the input. Also, you have "if (answer = 'y'|'Y')"
                You want "if (answer == 'Y'|'y')".
                If you want to ask for more than one artist than you can use a while loop that runs if the user said 'y' or 'Y'.

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  Even this if condition will not work. I can see what you're trying to say - execute if the user enters 'y' or 'Y' - but that's not how the compiler will read it. You should instead type if (answer == 'Y' || answer == 'y').

                  Comment

                  • ilikepython
                    Recognized Expert Contributor
                    • Feb 2007
                    • 844

                    #10
                    Originally posted by Ganon11
                    Even this if condition will not work. I can see what you're trying to say - execute if the user enters 'y' or 'Y' - but that's not how the compiler will read it. You should instead type if (answer == 'Y' || answer == 'y').
                    Oh yes, that's what I meant, stupid mistake. I just wanted to point out the double equal sign.

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      No problem - it just goes to show you that we all make mistakes :D

                      Comment

                      • JWest46088
                        New Member
                        • Sep 2006
                        • 74

                        #12
                        I revised my program. It does the same thing. The input( ) function is supposed to be called, which gets the input from the user. Then, the moreInput( ) function is supposed to be called to ask the user if they want to enter more information, and if they type Y or y, it calls the input( ) function again, but I am getting errors that I have no clue what they mean.

                        Here is my program:

                        Code:
                        #include <iostream>
                        #include <fstream>
                        #include <string>
                        using namespace std;
                         
                        int main ()
                        {   
                          input();
                          moreInput();
                           
                          return 0;
                        }
                            
                        void input()
                        {
                          char line[100];
                            
                          ofstream myfile("playlist.txt");
                          if(myfile.is_open())
                          {
                            cout << "Enter artist: ";
                            cin.getline(line, 50); 
                            myfile << line << endl; 
                            cout << "Enter album: ";
                            cin.getline(line, 50); 
                            myfile << line << endl;
                            cout << "Enter track: ";
                            cin.getline(line, 50);
                            myfile << line << endl;
                            cout << "Enter song: ";
                            cin.getline(line, 50);   
                            myfile << line << endl;
                            cout << "Enter genre: ";
                            cin.getline(line, 50);  
                            myfile << line << endl;
                            myfile.close();
                          }
                          else
                            cout << "Unable to open file.";
                        }
                            
                        void moreInput()
                        {
                          char answer;
                          cout << "Would you like to enter more information into your library? (Y/N)  ";
                          cin >> answer;
                           
                          if(answer == 'Y' || answer == 'y')
                            cin.ignore();
                            input();
                        }

                        Here are the errors:

                        playlist_v2.cpp : In function `int main()':
                        playlist_v2.cpp :8: error: `input' undeclared (first use this function)
                        playlist_v2.cpp :8: error: (Each undeclared identifier is reported only once for each function it appears in.)
                        playlist_v2.cpp :9: error: `moreInput' undeclared (first use this function)
                        playlist_v2.cpp : In function `void input()':
                        playlist_v2.cpp :15: error: `void input()' used prior to declaration
                        playlist_v2.cpp : In function `void moreInput()':
                        playlist_v2.cpp :43: error: `void moreInput()' used prior to declaration


                        Can someone tell me what these errors mean?

                        Comment

                        • ilikepython
                          Recognized Expert Contributor
                          • Feb 2007
                          • 844

                          #13
                          Originally posted by JWest46088
                          I revised my program. It does the same thing. The input( ) function is supposed to be called, which gets the input from the user. Then, the moreInput( ) function is supposed to be called to ask the user if they want to enter more information, and if they type Y or y, it calls the input( ) function again, but I am getting errors that I have no clue what they mean.

                          Here is my program:

                          Code:
                          #include <iostream>
                          #include <fstream>
                          #include <string>
                          using namespace std;
                           
                          int main ()
                          {   
                            input();
                            moreInput();
                             
                            return 0;
                          }
                              
                          void input()
                          {
                            char line[100];
                              
                            ofstream myfile("playlist.txt");
                            if(myfile.is_open())
                            {
                              cout << "Enter artist: ";
                              cin.getline(line, 50); 
                              myfile << line << endl; 
                              cout << "Enter album: ";
                              cin.getline(line, 50); 
                              myfile << line << endl;
                              cout << "Enter track: ";
                              cin.getline(line, 50);
                              myfile << line << endl;
                              cout << "Enter song: ";
                              cin.getline(line, 50);   
                              myfile << line << endl;
                              cout << "Enter genre: ";
                              cin.getline(line, 50);  
                              myfile << line << endl;
                              myfile.close();
                            }
                            else
                              cout << "Unable to open file.";
                          }
                              
                          void moreInput()
                          {
                            char answer;
                            cout << "Would you like to enter more information into your library? (Y/N)  ";
                            cin >> answer;
                             
                            if(answer == 'Y' || answer == 'y')
                              cin.ignore();
                              input();
                          }

                          Here are the errors:

                          playlist_v2.cpp : In function `int main()':
                          playlist_v2.cpp :8: error: `input' undeclared (first use this function)
                          playlist_v2.cpp :8: error: (Each undeclared identifier is reported only once for each function it appears in.)
                          playlist_v2.cpp :9: error: `moreInput' undeclared (first use this function)
                          playlist_v2.cpp : In function `void input()':
                          playlist_v2.cpp :15: error: `void input()' used prior to declaration
                          playlist_v2.cpp : In function `void moreInput()':
                          playlist_v2.cpp :43: error: `void moreInput()' used prior to declaration


                          Can someone tell me what these errors mean?
                          Try putting prototypes at the top of your file. You need those because when main runs it hasn't yet seen those functions:
                          Code:
                          void input();  // notice the semicolons
                          void moreInput();
                          //rest of program
                          Also, your last if statement in moreInput() should have braces otherwise it will call input no matter what the user enters.

                          Comment

                          • JWest46088
                            New Member
                            • Sep 2006
                            • 74

                            #14
                            Yes, I just realized that. Doh...

                            New problem. I ask the user if they want to enter more information and they enter y or Y. Then, after they entered new information, it overwrites the existing information in the text file. How would I make it so it wouldn't do this?

                            Comment

                            • JWest46088
                              New Member
                              • Sep 2006
                              • 74

                              #15
                              Originally posted by JWest46088
                              Yes, I just realized that. Doh...

                              New problem. I ask the user if they want to enter more information and they enter y or Y. Then, after they entered new information, it overwrites the existing information in the text file. How would I make it so it wouldn't do this?
                              I probably should use append, huh?

                              Comment

                              Working...