Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 26th, 2005, 11:10 AM   #1
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
problem w/ cin.getline?

Hi,

I am having a problem with cin.getline in function "getattrib()" in the program below.

I would have only posted the function getattrib() but I have noticed the function works fine when copied and compiled in another program, so I'm not sure what part of my code makes the function not work as intended.
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

struct movie_info 
	   {
	   char title[70];
	   char runtime[70];
	   char quality[70];
	   char codec[70];
	   } movie;

string printmain (void);
string printcategorie (void);
movie_info getattrib (void);
string existingchoice (void);   
 
int main(int argc, char *argv[])
{
	while (1) {
		  string answer1return;
		  answer1return = printmain();
		  
		  if (answer1return == "1") 
		  {
			 string answer2return;
			 answer2return = printcategorie();
						 
			 if (answer2return == "1") 
			 {
				while (1){
				movie = getattrib();
											 
				vector<string> readfile;
				string putfile;
	
			    ifstream Actionstreamin("Action.mdb");
			    while (getline(Actionstreamin, putfile))
				{
					 readfile.push_back(putfile + "\n");
				} 
				
				Actionstreamin.close();
	
				readfile.push_back("\nTitle: ");
				readfile.push_back(movie.title);
				readfile.push_back("\nRuntime: ");
				readfile.push_back(movie.runtime);
				readfile.push_back("\nQuality: ");
				readfile.push_back(movie.quality);
				readfile.push_back("\nCodec: ");
				readfile.push_back(movie.codec);
			 readfile.push_back("\n\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\n");
		 								 
				ofstream Actionstream("Action.mdb");
				if (Actionstream.is_open()) 
				{
				 for (int i=0; i < readfile.size(); i++)
				 Actionstream << readfile[i];
				   Actionstream.close();
				 cout << "\n" << "New Entry Successfully Added" << endl;
				   system("PAUSE");
				   break;
				} else if (! Actionstream.is_open()) 
				{
					 cout << "\n\nError opening file" << endl;
					 system("PAUSE");
					 exit(1);
				}
					   } 
			  } else if (answer2return == "2") 
			  {
					 while (1){
					 movie = getattrib();
											 
					 vector<string> readfile;
					 string putfile;
	
					 ifstream Adventure("Adventure.mdb");
					 while (getline(Adventure, putfile))
					 {
						 readfile.push_back(putfile);
					 } Adventure.close();
	
					 
					 readfile.push_back("\nTitle: ");
					 readfile.push_back(movie.title);
					 readfile.push_back("\nRuntime: ");
					 readfile.push_back(movie.runtime);
					 readfile.push_back("\nQuality: ");
					 readfile.push_back(movie.quality);
					 readfile.push_back("\nCodec: ");
					 readfile.push_back(movie.codec);
					 readfile.push_back("\n\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\n");
		 							 
					 ofstream Adventurestream ("Adventure.mdb");
					 if (Adventurestream.is_open()) 
					 {
					 for (int i=0; i < readfile.size(); i++)
					 Adventurestream << readfile[i];
					 Adventurestream.close();
					 cout << "\n" << "New Entry Successfully Added" << endl;
					 system("PAUSE");
					 break;
					 } else if (! Adventurestream.is_open()) 
							 {
								 cout << "\n\nError opening file" << endl;
								 system("PAUSE");
								 exit(1);
							 }
							 }
			  } else if (answer2return == "3") 
			  {  
				 while (1)
				 {
					 movie = getattrib();
											 
					 vector<string> readfile;
					 string putfile;
	
					 ifstream Comedystreamin("Comedy.mdb");
					 while (getline(Comedystreamin, putfile))
					   {
							 readfile.push_back(putfile);
					 } Comedystreamin.close();
	
											 
					 readfile.push_back("\nTitle: ");
					 readfile.push_back(movie.title);
					 readfile.push_back("\nRuntime: ");
					 readfile.push_back(movie.runtime);
					 readfile.push_back("\nQuality: ");
					 readfile.push_back(movie.quality);
					 readfile.push_back("\nCodec: ");
					 readfile.push_back(movie.codec);
					 readfile.push_back("\n\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\n");
		 							 
					 ofstream Comedystream ("Comedy.mdb");
					 if (Comedystream.is_open()) 
					   {
						 for (int i=0; i < readfile.size(); i++)
						 Comedystream << readfile[i];
						 Comedystream.close();
						 cout << "\n" << "New Entry Successfully Added" << endl;
						 system("PAUSE");
						 break;
					 } else if (! Comedystream.is_open()) 
								 {
								 cout << "\n\nError opening file" << endl;
								 system("PAUSE");
								 exit(1);
								 }
				 } 
			  } else if (answer2return == "4") 
			  {
				while (1)
				{
					 movie = getattrib();
											 
					 vector<string> readfile;
					 string putfile;
	
					 ifstream Kidsstreamin("Kids.mdb");
					 while (getline(Kidsstreamin, putfile))
					  {
						 readfile.push_back(putfile);
					 } Kidsstreamin.close();
	
					  
					 readfile.push_back("\nTitle: ");
					 readfile.push_back(movie.title);
					 readfile.push_back("\nRuntime: ");
					 readfile.push_back(movie.runtime);
					 readfile.push_back("\nQuality: ");
					 readfile.push_back(movie.quality);
					 readfile.push_back("\nCodec: ");
					 readfile.push_back(movie.codec);
					 readfile.push_back("\n\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\n");
		 							 
					 ofstream Kidsstream ("Kids.mdb");
					 if (Kidsstream.is_open()) 
					  {
						 for (int i=0; i < readfile.size(); i++)
						 Kidsstream << readfile[i];
						 Kidsstream.close();
						 cout << "\n" << "New Entry Successfully Added" << endl;
						 system("PAUSE");
						 break;
					 } else if (! Kidsstream.is_open()) 
					  {
							 cout << "\n\nError opening file" << endl;
							 system("PAUSE");
							 exit(1);
					  }
				}	 
			 }
		   } else if (answer1return == "2") 
		   {
				string answer3return;
				answer3return = printcategorie();
				
				if (answer3return == "1") 
				{
				   while (1) 
				   {
						 vector<string> readfile;
						 string existingmovie;
		 												 
						 ifstream premovie ("Action.mdb");
						 while (getline(premovie, existingmovie)) 
						 {
							 readfile.push_back(existingmovie + "\n");
						 } 
						 
						 premovie.close();
						 
						 cout << "\nAction Movies:" << "\n" << "\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4" << endl;
						 for (int i=0; i < readfile.size(); i++)
						 cout << readfile[i];
						 system("PAUSE");
						 break;
				   }
				} else if (answer3return == "2")
				{
				  while (1) 
				  {
					 vector<string> readfile;
					 string existingmovie;
		 												 
					 ifstream premovie ("Adventure.mdb");
					 while (getline(premovie, existingmovie)) 
					    {
					 readfile.push_back(existingmovie + "\n");
					    } 
					    
					 premovie.close();
		 	 														 
					 cout << "\nAdventure Movies:" << "\n" << "\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4" << endl;
					 for (int i=0; i < readfile.size(); i++)
					 cout << readfile[i];
					 system("PAUSE");
					 break;
				  }
															 
				} else if (answer3return == "3")
				{
				  while (1) 
				  {
					 vector<string> readfile;
					 string existingmovie;
		 												 
					 ifstream premovie ("Comedy.mdb");
					 while (getline(premovie, existingmovie)) 
					    {
							 readfile.push_back(existingmovie + "\n");
					    } 
					    
					 premovie.close();
		 	 														 
					 cout << "\nComedy Movies:" << "\n" << "\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4" << endl;
					 for (int i=0; i < readfile.size(); i++)
					 cout << readfile[i];
					 system("PAUSE");
					 break;
				  }
				} else if (answer3return == "4")
				{
				  while (1) 
				  {
					 vector<string> readfile;
					 string existingmovie;
		 												 
					 ifstream premovie ("Kids.mdb");
					 while (getline(premovie, existingmovie)) 
					    {
							 readfile.push_back(existingmovie + "\n");
					    } 
					    
					 premovie.close();
		 	 														 
					 cout << "\nKids Movies:" << "\n" << "\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4\xC4" << endl;
					 for (int i=0; i < readfile.size(); i++)
					 cout << readfile[i];
					 system("PAUSE");
					 break;
				  }
															 
				} else {
						 cout << "\n" << "ERROR: " << answer3return << " is not an option." << endl;
						 system("PAUSE");
					   } 
		   } else if (answer1return == "3") 
		   { 
			 break; 
		   } else {
					 cout << "\n" << "ERROR: " << answer1return << " is not an option." << endl;
					 system("PAUSE");
				  }
		  }
	return EXIT_SUCCESS;
}


string printmain (void) {
	   system("CLS");
	 cout << "					 \xC9\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBB" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xBA		 Movie Database		 \xBA" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xCC\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xB9" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xBA Choose from the folowing options \xBA" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xBA 1: Add New Entry To Database	 \xBA" << endl;
	 cout << "					 \xBA 2: View Existing Database Entries\xBA" << endl;
	 cout << "					 \xBA 3: Exit Program				 \xBA" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xC8\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBC" << endl;
	   cout << "\n\nChoice:";
	   string answer1;
	   cin >> (answer1);
	   return (answer1);
	   }
	   
	 
string printcategorie (void) {
	   system("CLS");
	 cout << "					 \xC9\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBB" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xBA		 Movie Database		 \xBA" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xCC\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xB9" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xBA	 Please select a category	\xBA" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xBA 1: Action					 \xBA" << endl;
	 cout << "					 \xBA 2: Adventure					 \xBA" << endl;
	 cout << "					 \xBA 3: Comedy					 \xBA" << endl;
	 cout << "					 \xBA 4: Kids						 \xBA" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xC8\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBC" << endl;
	   cout << "\n\nChoice:";
	   string answer2;
	   cin >> (answer2);
	   return (answer2);
	   }
	   
movie_info getattrib (void) {
	   system("CLS");
	 cout << "					 \xC9\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBB" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xBA		 Movie Database		 \xBA" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xCC\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xB9" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xBA Next input the movies details \xBA" << endl;
	 cout << "					 \xBA								 \xBA" << endl;
	 cout << "					 \xC8\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBC" << endl;
	   cout << "\n\nTitle:"; 
	   cin.getline(movie.title,70); //This line seems to be skipped when the program executes
	   cout << "\nRuntime:"; 	
	   cin.getline(movie.runtime,70);
	   cout << "\nQuality:";
	   cin.getline(movie.quality,70);
	   cout << "\nRequired Codec:";
	   cin.getline(movie.codec,70);
	   return (movie);
	   }

What the function getattrib() should do is ask for and then store user input into a movie_info structure (movie), then return movie. The problem I'm gettins is during execution, when in the function getattrib() the line "cin.getline(movie.title,70);" seems to act as if data has already been passed to "movie.title" and so appears to get skipped.

Here is a picture of the problem when the program is run, just incase my description is no good:
Cache is offline   Reply With Quote
Old Oct 26th, 2005, 11:17 AM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
maybe there is something left in the stream.. try flushing it.
Polyphemus_ is offline   Reply With Quote
Old Oct 26th, 2005, 11:26 AM   #3
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by Polyphemus_
maybe there is something left in the stream.. try flushing it.
OK, can you tell me how to flush the stream please.

EDIT: I just tried "cout.flush()" and "cout << flush;" neither resolved the problem. Was that what you meant by "flushing it"?

Last edited by Cache; Oct 26th, 2005 at 11:41 AM.
Cache is offline   Reply With Quote
Old Oct 26th, 2005, 12:48 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Use cin.sync () before you ask for input. I haven't looked at your code, so if that doesn't work, post back. Be sure and check the streams for error after each operation. They don't promise to always work, they just promise to tell you if they don't.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 26th, 2005, 1:07 PM   #5
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by DaWei
Use cin.sync () before you ask for input.
cin.sync () worked, problem solved!

This is something I can note for future programs too. Thanks.
Cache is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC