Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,920 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,022 people online right now. Registration is fast and FREE... Join Now!




Help with character in integer variable

 
Reply to this topicStart new topic

Help with character in integer variable, if (variable != int)?

siylence
7 Jan, 2008 - 04:52 AM
Post #1

New D.I.C Head
*

Joined: 10 Jun, 2007
Posts: 21


My Contributions
This really shouldn't be that big of a problem... at least I don't think so. I'm having trouble with a validation loop. Here's the code:

CODE

int student = 20;
int num = 0;


while(num <= 0 || num > student+1 && num != 'Q' && num != 'q')
                       {
                              cout << "\nPick the number of the student you wish to edit or (Q)uit: ";
                              cin >> num;
                              cin.get();
                              
                       }




The number verification works fine. However, when you put a letter as the variable, it goes berserk. I've never really needed a verification that stops that from happening, but it'd be nice to finally figure out a way to have that stop.
Thank you,
Benjamin Atkerson
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Help With Character In Integer Variable
7 Jan, 2008 - 05:06 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
Two good options are

cin.good() (remember to clear the flags if an error is found).

home.att.net/~jackklein/c/code/strtol.html

User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 02:43PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month