GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Nov-2007, 13:50
Commochief Commochief is offline
New Member
 
Join Date: Nov 2007
Posts: 7
Commochief is on a distinguished road

Removing the pause after a cin.ignore


Hello all,

Programming student just starting out, and running into an issue when using cin.clear & cin.ignore.

My intent with this is to clear the buffer of extraneous characters and numbers, while still advancing on to the next cout. But it will add an extra pause in between the 2 sets of couts.

If I use just the ignore (example on the ISBN entry) for some reason it does not work, it will still either not drop the text and then jump past several cout/cin's or it will crash with an invalid data type entry after infinite looping.

(Hopefully this makes sense to ya'll, if not I can try and clarify.)

Here is a snippet of the code:

CPP / C++ / C Code:
void dataEntry(char title[],char isbn[],char author[],int pages,float bookValue,int recordComplete,ofstream &bookLibrary)
{
system("cls");
bookValue=0.0;
pages=0;
recordComplete=0;
//Get Title and notify user of how to escape the program.
cout<<"!!To close the Program and finalize the library file\n\n"
    <<"do not enter a title for the book and press 'Enter'!!\n"<<endl;
cout<<"What is the title of the book?";
cin.getline(title, 41,'\n');
while(strcmp(title,"") != 0)
    {
     
    //Get ISBN
    cout<<"What is the ISBN of the book?";
    cin.getline(isbn, 13, '\n');
    
    //Clear the buffer
    cin.clear();
    cin.ignore(10000, '\n');
    
    
    //Get Author
    cout<<"What is the Author of the book?";
    cin.getline(author, 21, '\n');
    
    //Clear the buffer
    cin.ignore(10000, '\n');
    
    //Get Number of Pages
    cout<<"What is the number of pages in the book?";
    cin>>pages;
    
    //Clear the buffer
    cin.clear();
    cin.ignore(10000, '\n'); 
    
    //Get Book Value from user.
    evalBookValue(bookValue);
    
    //Clear the buffer
    cin.clear();
    cin.ignore(10000, '\n'); 
    
    //Write Data to file if valid entry.
    if(recordComplete = (bookValue != -999.0))
        {
        cout<<"Writing data to file...please wait.\n\n";
        bookLibrary<<setw(41)<<setiosflags(ios::left)<<title<<endl;
        bookLibrary<<setw(13)<<setiosflags(ios::left)<<isbn<<endl;
        bookLibrary<<setw(21)<<setiosflags(ios::left)<<author<<endl;
        bookLibrary<<setw(6)<<setiosflags(ios::left)<<pages<<endl;
        bookLibrary<<"$"<<setw(9)<<setiosflags(ios::left)<<setiosflags(ios::fixed)<<setprecision(2)<<bookValue<<endl;
        }
        
    //Get Title and notify user of how to escape the program.
    cout<<"\n!!To close the Program and finalize the library file"<<endl;
    cout<<"do not enter a title for the book and press 'Enter'!!"<<endl;
    cout<<"\n\nWhat is the title of the book?";
    cin.getline(title, 41, '\n'); 
    }

cout<<"\n\nThank you for using the Small Library Entry System."<<endl;
cout<<"Have a nice day!"<<endl;
system("pause");
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Evaluate teh price of the book.
void evalBookValue(float &bookValue)
{
cout<<"\nTo exit the record, type in -999.0"<<endl;
cout<<"What is the value of the book? ";
cin>>bookValue;
while (bookValue > 10000 || bookValue < 0)
    {
    cout<<"\nPlease enter a valid amount between $0 and $10,000"<<endl;
    cout<<"\nTo exit the record, type in -999.0"<<endl;
    cout<<"What is the value of the book? ";
    cin>>bookValue;
    }
if (bookValue == -999.0)
cout<<"Skipping the Book Entry..."<<endl;
}
  #2  
Old 08-Nov-2007, 14:10
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: removign the pause after a cin.ignore


One thing I noticed is that you're calling cin.clear(). This does not clear the buffer. This clears the error flags.

Another thing that I am not sure of is whether when the user hits enter if it will get to a \n or if it gets to a \r. This might be something to play around with.

For more documentation on cin (istream), you can check:
http://www.cplusplus.com/reference/iostream/istream/
  #3  
Old 08-Nov-2007, 14:18
Commochief Commochief is offline
New Member
 
Join Date: Nov 2007
Posts: 7
Commochief is on a distinguished road

Re: removign the pause after a cin.ignore


So the cin.clear is unnecessary? When I comment out that line, and say I add a ton of numbers to the ISBN (which is declared as an int), here is the code:

CPP / C++ / C Code:
    //Get ISBN
    cout<<"What is the ISBN of the book?";
    cin.getline(isbn, 13,'\n');
    
    //Clear the buffer
    //cin.clear();
    cin.ignore(10000,'\n');

and my result looks like:

What is the title of the book?sdfdsfg
What is the ISBN of the book?6376537646573456347653745
What is the Author of the book?
What is the number of pages in the book?

It skipped right over the Author cin/cout and went on to the number of pages. My result file looks like...

sdfdsfg //this is the title line
637653764657 //this is the ISBN line
//this is the author line
789 //this is the number of pages line
$678.00 //this is the price line


If I add the cin.clear() back in I get the following

What is the title of the book?james
What is the ISBN of the book?5555555555555555555555555
What is the Author of the book?jamesjamejase
What is the number of pages in the book?4444444444444444
To exit the record, type in -999.0
What is the value of the book? 456

james
555555555555
jamesjamejase
4.44444e+15 //I noticed I need to adjust my format here...
$456.00
  #4  
Old 08-Nov-2007, 14:41
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: removign the pause after a cin.ignore


I don't know exactly. You might try to replace the clear/ignore statements with

cin.seekg (0, ios::end);

This should take you to the end of the stream. I'm not sure though, so you'll have to let us know if it works.
  #5  
Old 08-Nov-2007, 14:42
Commochief Commochief is offline
New Member
 
Join Date: Nov 2007
Posts: 7
Commochief is on a distinguished road

Re: removign the pause after a cin.ignore


I think I see what you mean about the error flag now that you say that.
( I am not trying to argue, btw, just trying to understand...)

I am still getting an error, but the .clear just removes the error and allows it to continue on with the bad data, clearing it out with the ignore? is that a correct way to look at it?
  #6  
Old 08-Nov-2007, 14:47
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: removign the pause after a cin.ignore


Are you, by chance, using Microsoft's Visual Studio? I'm not sure about with cout/cin, but when I used to use printf/scanf with it, it would do exactly what you're talking about and I had heard that it was either the studio or the shell (command prompt interface).
  #7  
Old 08-Nov-2007, 14:47
Commochief Commochief is offline
New Member
 
Join Date: Nov 2007
Posts: 7
Commochief is on a distinguished road

Re: removign the pause after a cin.ignore


Nope, Didn't work with the cin.seekg (0, ios::end);

When I replaced the clear/ignore with it, it did not kill the extra characters int he buffer, it just put them in the other statements and continued on.

It works with the clear/ignore, I just can't figure out why it ask for a second line feed to get to the next statement....
  #8  
Old 08-Nov-2007, 14:48
Commochief Commochief is offline
New Member
 
Join Date: Nov 2007
Posts: 7
Commochief is on a distinguished road

Re: removign the pause after a cin.ignore


nope, hand coding in notepad++ then using the Borland Compiler (BCC55) to compile and link...
  #9  
Old 08-Nov-2007, 14:50
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: removign the pause after a cin.ignore


Well, I wish I could be of more help. Maybe Dave or Davis will have better advice. I just don't use a command-line interface that much. I'll let you know if I think of anything else to try.
  #10  
Old 08-Nov-2007, 14:52
Commochief Commochief is offline
New Member
 
Join Date: Nov 2007
Posts: 7
Commochief is on a distinguished road

Re: Removing the pause after a cin.ignore


Ok, thanks a lot for the suggestion. Pretty new to programming so have a lot to learn. I am posting the entire cpp file as a text file in case anyone want the whole thing.

thanks again for the help and quick response...
Attached Files
File Type: txt project3.txt (4.6 KB, 9 views)
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
win32 CreateProcess(), STARTUPINFO not working well for 98 console Howard_L C Programming Language 0 09-Aug-2007 23:33
best way to pause program flow - cin.get? Jbonez CPP / C++ Forum 6 23-Oct-2005 21:57
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
want to add a pause in a game. Help jjj93421 CPP / C++ Forum 1 11-Feb-2004 09:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:16.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.