-
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?
-
-
-
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?
-
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.
-
Thanks, the string input works now :D
But I'm having trouble fixing the menu thing. Got any tips for me?
-
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;
.......
-
THanks stillwell...you code made me able to solve the problem with my Smarterchild programme.
-
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).
-
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.
-
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'...
-
Thanks to everyone who helped :D 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;
}
-
There is an important difference between = and ==.
-
hehe, I'm a noobie :P
Works perfect now. Thanks for all your help guys.
-
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.