Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-14-2004, 03:57 PM   #1 (permalink)
rarmknecht
Registered User
 
Join Date: May 2004
Location: US
Posts: 3
rarmknecht is on a distinguished road
Question Program is skipping cin.getline call

I'm not quite sure what is happening here, just that after much cutting of code I am still able to recreate the error and have narrowed it down to this section.

If I remove the part where it asks for encryption/decryption it allows user input. However, if it that portion is included, it skips over the cin.getline(input, MAXLN); call and displays all the cout's as if input was blank. Any ideas? I'm compiling under linux but you can feel free to compile it under windows. It really doesn't matter to me.

And the code:
Code:
#include <iostream>
using namespace std;

const int MAXLN = 128;
bool encrypted;

int main(int argc, char * argv[])
{

		char input[MAXLN];	//Holdind the string
		char temp;


		cout << "Press 1 to encrypt.  Press 2 to decrypt.\n";
		cin >> temp;

		if(temp == '2')	//Decryption needed
		{
			encrypted = true;
			cout << "Decryption it is then!" << endl;
		}
		else
		{
			if(temp == '1')
			{
				encrypted = false;
				cout << "Encryption is it then!" << endl;
			}
			else
			{
				cout << "You should have entered a number!" << endl;
				return 1;
			}
		}
	
		
		cout << "\nPlease enter in the text you would like to modify:" << endl;

		//grab the input string
		cin.getline(input, MAXLN);
		
		cout << "You entered: " << input << endl;
		
		return 0;
}
rarmknecht is offline   Reply With Quote
Old 05-14-2004, 04:33 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
you are not handling user input correctly. you ask the user to enter 1 or 2. really, the user presses 1, and presses the enter key. so now there are two bytes in the input buffer, "1\n". your cin >> temp eats the '1', but the '\n' is still sitting in the input buffer.
when you call cin.getline(input, MAXLN);, it gets the next line from the input buffer (ie, the "\n" that was waiting for it there), and it's done.
joe_bruin is offline   Reply With Quote
Old 05-14-2004, 05:58 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Hi, I noticed you wish to work in the standard namespace, yet you use cin.getline().
Use getline().
Here is a nice piece of code to validate correct input. You can change the int to anyting like a bool in your case.
Code:
  //uses <strstream>
  string sInLine;
  bool bValid = false;
  bool bNonDigit = false;
  int  nNumber;
  
  while( !bValid ) {
    cout << "Enter an integer : ";
    getline( cin, sInLine );
    
    bNonDigit = false;
    for( size_t i = 0; i < sInLine.size(); ++i ) {
      if( !isdigit( sInLine[i] ) )
        bNonDigit = true;
    }
    
    if( bNonDigit )
      cout << "The input contains non digits - Invalid\n";
    
    else {
      istrstream InStream( sInLine.c_str() );
      InStream>> nNumber;
      if( !InStream )
        cout << "Your number is invalid\n";
      else
        bValid = true;
    }
  }
  
  cout << "You entered " << nNumber << endl;
__________________
Valmont is offline   Reply With Quote
Old 05-15-2004, 08:11 AM   #4 (permalink)
rarmknecht
Registered User
 
Join Date: May 2004
Location: US
Posts: 3
rarmknecht is on a distinguished road
thanks for your replies!

Both were very helpful.
rarmknecht is offline   Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 06:14 AM
Help on starting new program B00tleg Standard C, C++ 21 10-17-2004 12:58 PM
Need help on program B00tleg Standard C, C++ 1 10-12-2004 12:02 AM


All times are GMT -8. The time now is 12:02 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting