User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,074 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,135 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 3198 | Replies: 26
Reply
Join Date: Jun 2006
Posts: 20
Reputation: rbinc is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
rbinc rbinc is offline Offline
Newbie Poster

Help Help Me Solve My Infinite Loop

  #1  
Nov 15th, 2006
Background:
I need the user to type in an int "k" key for an encryption program. For the program to work right, the user entered int needs to be between 1-25. It can't be higher. The problem is if the user enters something other than an int, the program either just continues, or the program goes into an infinite loop. The int "t" just acts as a true/false for the loop.

  1. do
  2. {
  3. t=1;
  4. cout << "\nEnter the encryption key (1-25): ";
  5. cin >> k;
  6. if ((k<1)||(k>25))
  7. {
  8. cout << "\nERROR: Enter a valid number moron!\n";
  9. t=0;
  10. }
  11. }
  12. while (!t);

I tried converting the value to a tmp char and checking the char to be between SOH-EM, but that results in the same problems.


Any help greatly appreciated :cheesy: Btw, its my first post, I think.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,562
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Help Me Solve My Infinite Loop

  #2  
Nov 15th, 2006
It is continuing like that because cin is trying to convert the alpha character that is in the keyboard buffer to numeric, but it fails and leaves the key in the keyboard buffer. The easiest way to correct this is to get the integer as a string the convert the string to int. Something like this will work:
do
{
  t=1;
   std::string input;
  cout << "\nEnter the encryption key (1-25): ";
  getline(cin,input);
  k = atol(input.c_str());
  if ((k<1)||(k>25))
  {
      cout << "\nERROR: Enter a valid number moron!\n";
      t=0;
  }
}
while (!t);
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jun 2006
Posts: 20
Reputation: rbinc is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
rbinc rbinc is offline Offline
Newbie Poster

Re: Help Me Solve My Infinite Loop

  #3  
Nov 15th, 2006
Thanx alot Ancient Dragon. Works great. Out of curiosity, what's the "std::" for?
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,562
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Help Me Solve My Infinite Loop

  #4  
Nov 15th, 2006
if you do not specify "using namespace std" then you have to identify the namespace if it is not in global namespace.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jun 2006
Posts: 20
Reputation: rbinc is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
rbinc rbinc is offline Offline
Newbie Poster

Re: Help Me Solve My Infinite Loop

  #5  
Nov 15th, 2006
Thanx again. I used "namespace std".
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 218
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Rep Power: 3
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: Help Me Solve My Infinite Loop

  #6  
Nov 15th, 2006
Noob question...
why not just define k as an int without resorting to the fancy lib stuff?
int k;
cin >> k;
won't that restrict the input sufficiently?
Reply With Quote  
Join Date: Jun 2006
Posts: 20
Reputation: rbinc is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
rbinc rbinc is offline Offline
Newbie Poster

Re: Help Me Solve My Infinite Loop

  #7  
Nov 15th, 2006
At first I did have the user enter "k" without any conditions, but if they entered something other than a number, my program got screwed up. I'm doing a Caesar's Cipher adaptation, and I have to add "k" to a user entered string, so "k" has to be between 1-25 for the program to work, as I limited the user's string to only letters (caps and small).
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Help Me Solve My Infinite Loop

  #8  
Nov 16th, 2006
Try it, JRM, enter 12A and see what happens. Be sure you put your cin in a loop.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Jul 2005
Posts: 1,095
Reputation: Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough 
Rep Power: 9
Solved Threads: 141
Lerner Lerner is offline Offline
Veteran Poster

Re: Help Me Solve My Infinite Loop

  #9  
Nov 16th, 2006
You can check the fail state of the stream to assist with type validation rather than accept input as a string, validate, and convert if necessary. It's no simpler than the other version, but it works.
1)declare variable
   int k = 0; 
2)attempt input 
   std::cin > k;
3)check the state variable of the stream
   if(std::cin.fail())
4)Do something if in failed state
   {
5)Reassign a default value to k---say user entered 5Q instead of 51. 
cin would assign 5 to the value of k, which would be desired.
cin would then try to put Q in k which would cause a failed state, but wouldn't remove the 5 from k (so k would have an incorrect input, 5 vs 51, eventhough it is a valid int) and would leave the invalid input in the input stream .
      k = 0;
6)clear the state bit (aka fail flag) of the stream
    std::cin.clear();
7)clear the input stream buffer of all unwanted information.  If you want to use this format and don't know what the following means, ask.  
    std::cin.ignore(std::<numeric_limits<streamsize>::max());
8)notify user of invalid input
    cout << "invalid input" << endl;
9)move on to whatever's next
    }
Last edited by Lerner : Nov 16th, 2006 at 1:19 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,562
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Help Me Solve My Infinite Loop

  #10  
Nov 16th, 2006
>> std::cin.ignore(std::<numeric_limits<streamsize>::max());
illegal syntax at '<' character. But otherwise interesting way to clear the keyboard buffer, just please post a corrected version. Too bad it doesn't work. In the example below, enter 123al;skfjlsdkf <Enter> The keyboard buffer is not cleared.

#include <iostream>
#include <limits>
using namespace std;

int main(int argc, char* argv[])
{
	int a;
	cout << "enter something ...";
	cin >> a;
   std::cin.ignore(std::numeric_limits<streamsize>::max());
   cout << "\nenter something else";
   cin >> a;
   cout << "a = " << a << endl;

	return 0;
}
Last edited by Ancient Dragon : Nov 16th, 2006 at 1:39 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

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

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:09 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC