Trouble with string

This is a discussion on Trouble with string within the C++ Programming forums, part of the General Programming Boards category; I've tried to make this small program that keeps track of my movie collection. However, I'm having trouble reading in ...

  1. #1
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80

    Trouble with string

    I've tried to make this small program that keeps track of my movie collection. However, I'm having trouble reading in movies with spaced names.

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    
    using namespace std;
    
    class movies
    {     
          public:
          void newMovie();
          void listMovies();
          void removeAllMovies();       
                
    };
    
    void movies::newMovie()
    {
         string movieName;
         int year;
         
         ofstream tempStream ("movies.txt", ios::app);
         
         cout << "\nInset movie name: ";
         cin  >> movieName; *Here is my problem* 
         cout << "Inset movie year: ";
         cin  >> year;
         
         tempStream << movieName
                    << " (" << year << ")\n";
         
    }
    
    void movies::listMovies()
    {
         string line; 
     
         ifstream tempStream ("movies.txt");
         if (tempStream.is_open())
         {
            while (!tempStream.eof() )
            {
             getline (tempStream,line);
             cout << line << endl;
            }
          tempStream.close();
          }
    
      else cout << "Unable to open file"; 
         
    }
    
    void movies::removeAllMovies()
    {
         char option;
         
         cout << "Sure you want to delete all your Movies?(y/n): ";
         cin  >> option;
         
         if (option = 'y')
         ofstream tempStream ("movies.txt", ios::trunc);   
         
    }
    
    int main ()
    {
    
        int option = 0;
        movies myMovies;
        
        while (option < 1 || option > 3)
        {
        cout << "Welcome to the movie Database v1.0\n\n"
             << "These are your options:\n\n"
             << "     (1)List my movies\n"
             << "     (2)Add new movie\n"
             << "     (3)Remove all my movies\n";
                      
        cin  >> option;
        
        if (option == 1)
        myMovies.listMovies();
        else if (option == 2)
        myMovies.newMovie();
        else if (option == 3)
        myMovies.removeAllMovies();
        
        option = 0;
        }
             
    }
    what fuction do I need to use to have it read in the whole line, and not interupt when it gets to a space?
    Last edited by stillwell; 03-21-2006 at 07:41 AM. Reason: spelling

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    getline(cin, movieName);

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Edit: Never Mind
    Last edited by indigo0086; 03-21-2006 at 08:09 AM.

  4. #4
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    OnionKnight, when I do that, the program simply jumps the movie title input. I don't understand why, but it does.

    Indigo, could you tell me what other errors?

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    runtime errors.

    I added
    Code:
    cout << "\nInset movie name: ";
         cin.get();
         getline(cin, movieName);
    because it was skipping the movie name input and getting the moviename data for year(int) and causing some endless loop.

    also if you don't enter a propper number in the menu, it goes into an endless loop.
    Last edited by indigo0086; 03-21-2006 at 08:27 AM.

  6. #6
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Thanks, the string input works now

    But I'm having trouble fixing the menu thing. Got any tips for me?

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Code:
    cin  >> option;
      if(!cin)
       break;
    Or you can create an inner loop that just re-displays the menu if the input is not correct.

    or

    Code:
        while (option < 1 || option > 3)
        {
        	cout << "Welcome to the movie Database v1.0\n\n"
             	<< "These are your options:\n\n"
             	<< "     (1)List my movies\n"
             	<< "     (2)Add new movie\n"
             	<< "     (3)Remove all my movies\n"
             	<< "     (4)Exit\n";
                      
        cin  >> option;
        if(option == 4 || cin.fail())
        	break;
    .......
    Last edited by indigo0086; 03-21-2006 at 08:51 AM.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    THanks stillwell...you code made me able to solve the problem with my Smarterchild programme.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Make sure you #include <string> since you are using the string class (<cstring> is completely different). Also, the cin.get() before the getline is necessary only when you use cin >> to read in a value before calling getline. (If your trouble is with the first call to getline and you use VC++ 6 you might have to fix a bug in the library code).

  10. #10
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I think it's something stored in cin from the option that's getting stored in movieName. That's why I used cin.get(). It didn't work without it.

  11. #11
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    You can also use a switch / case statement inside a do / while loop like this
    Code:
    char selection;
    do
    {
        cout<<"Menu"<<endl
               <<"1) Option 1"<<endl
               <<"2) Option 2"<<endl
               <<"3) Option 3"<<endl
               <<"x) Exit"<<endl
               <<"Selection : ";
        cin>>selection;
        switch (selection)
        {
            case '1' :
                // do option 1;
                break;
            case '2' :
                // do option 2;
                break;
            case '3' :
                // do option 3;
                break;
            case 'x' :
                break;
            default :
                cout<<"incorrect selection!";
                break;
        }
    }
    while (selection != 'x')
    This has always worked well for me in the past as it allows you to use numbers and characters for the menu options. And, with a little more code, you can also use other special keys like F1, F2.... FOr instance, you could use F3 to exit instead of 'x'...
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  12. #12
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Thanks to everyone who helped I have come upon a new problem, though. It deletes the list of movies even if I don't choose 'y' when the option comes up.

    This is how my code looks now:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    class movies
    {     
          public:
          void newMovie();
          void listMovies();
          void removeAllMovies();       
                
    };
    
    void movies::newMovie()
    {
         string movieName;
         int year;
         
         ofstream tempStream ("movies.txt", ios::app);
         
         cout << "\nInset movie name: ";
         cin.get();
         getline(cin, movieName);
         cout << "Inset movie year: ";
         cin  >> year;
         
         tempStream << movieName
                    << " (" << year << ")\n";
         
    }
    
    void movies::listMovies()
    {
         string line; 
     
         ifstream tempStream ("movies.txt");
         if (tempStream.is_open())
         {
            while (!tempStream.eof() )
            {
             getline (tempStream,line);
             cout << line << endl;
            }
          tempStream.close();
          }
    
      else cout << "Unable to open file"; 
         
    }
    
    void movies::removeAllMovies()
    {
         char option;
         
         cout << "\nSure you want to delete all your Movies?(y/n): ";
         cin  >> option;
         
         if (option = 'y')     Here is my problem
         ofstream tempStream ("movies.txt", ios::trunc);   
         else
         cout << "\nYour movies are safe !\n";
    }
    
    int main ()
    {
        char option;
        movies myMovies;
        
        while (option !='x')
        {
        cout << "Welcome to the movie Database v1.0\n\n"
             << "These are your options:\n\n"
             << "     (1)List my movies\n"
             << "     (2)Add new movie\n"
             << "     (3)Remove all my movies\n"
             << "     (x)Exit\n"
             << "\nChoose an option: ";
                           
        cin  >> option;
        
              switch (option)
              {
              case '1' :
              myMovies.listMovies();
              break;
        
              case '2' :
              myMovies.newMovie();
              break;
        
              case '3' :
              myMovies.removeAllMovies();
              break;
        
              case 'x' :
              break;
        
              default :
              cout << "Invalid choice !\n\n";
              break;
              }
        }
      return 0;       
    }

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    There is an important difference between = and ==.

  14. #14
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    hehe, I'm a noobie :P

    Works perfect now. Thanks for all your help guys.

  15. #15
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    One more thing to consider. All your input problems stem from using cin >> .... Change all your cin's to getline()'s and you won't have to fix the input problems by doing extra cin.get()'s.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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