•
•
•
•
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
![]() |
•
•
Join Date: Jun 2006
Posts: 20
Reputation:
Rep Power: 3
Solved Threads: 0
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.
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.
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.
c Syntax (Toggle Plain Text)
do { t=1; cout << "\nEnter the encryption key (1-25): "; cin >> k; if ((k<1)||(k>25)) { cout << "\nERROR: Enter a valid number moron!\n"; t=0; } } 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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,562
Reputation:
Rep Power: 36
Solved Threads: 860
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,562
Reputation:
Rep Power: 36
Solved Threads: 860
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Jun 2006
Posts: 20
Reputation:
Rep Power: 3
Solved Threads: 0
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).
•
•
Join Date: Jul 2005
Posts: 1,095
Reputation:
Rep Power: 9
Solved Threads: 141
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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,562
Reputation:
Rep Power: 36
Solved Threads: 860
>> 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.
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- XP Startup Problem: Infinite Loop (Windows NT / 2000 / XP / 2003)
- infinite loop... (C++)
- infinite loop (C)
- infinite loop (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ A Few Errors
- Next Thread: Question : Pointers



Linear Mode