• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

Justaddwater

C++ Verify user inputs a number

18 posts in this topic

Hi all, I'm trying to make sure a user types in a number for a int varible. Ive tried useing isnum and isdigit but they don't seem to like working with Int's only Char's ... anyone else have any idea? here is a simple idea of what Im trying to do...
int Soc;

cout << "enter soc" << end;
cin >> Soc;
	if (isdigit(Soc))
		cout << "ok";
	else
		cout << "wrong";
}
 
[edited by - justaddwater on March 13, 2004 9:11:41 PM]
0

Share this post


Link to post
Share on other sites
the reason is, the >> operator ALREADY knows the type of your var ... and CAN ONLY WORK WITH input of the correct type ...

so using "char" you can get each character and check for digits ... but using "int" you cannot, because doing this:

int num;
cin >> num;

reads in ONLY AN INT already ... if the user types "65" it works, or "-1235433", or even "0x0fa3" ... and if the user types "45.23" then the "45" part is read, but the ".23" is still in the buffer ... and here''s the important part .. if they type "hello53" .. the system blows up!

the formatted stream readers do NOT guarantee the formats are correct, and do NOT handle errors well ... they leave it to you to correctly tell them what to expect ... which oftens sucks ... as you''ve found out.

What you must do to perform input error / format checking is read in UNFORMATED (or less directly formatted) input - for example if you tell the user to type number and press enter, then you read a line, and then validate the chars in that line, and THEN convert the validated input to an int. I know it sucks, but that''s just how it works ...

and if you want the user to type a points like:

34,45

then you also would read the whole line, and validate and probably parse and normalize the data ... meaning you will accept spaces around the numbers - and discard them ... and then feed each of the two strings "34" and "45" to be converted to ints ... and used. Of course in the case where the user types unexpected characters, or one one nubmer, or 2 commas or whatnot ... you should reject the input of course (and take whatever exception handling actions you want ... like prompting again) ...
0

Share this post


Link to post
Share on other sites
Justaddwater,

quote:
int num;
cin >> num;

reads in ONLY AN INT already ... if the user types "65" it works, or "-1235433", or even "0x0fa3" ... and if the user types "45.23" then the "45" part is read, but the ".23" is still in the buffer ...


If you do not care about the above case there is an easier way,

When the user enters something that causes cin to "blow up", this can be detected:

cin >> num;

if(cin.good())
{
//everything ok
}
else
{
//Everything not ok
cin.clear(); //reset status bits (including the fail bit)
}
0

Share this post


Link to post
Share on other sites

int num;
while (cout << "Enter a number: " && !(cin >> num))
{
cout << "That''s not valid input. Try again" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ''\n'');
}
0

Share this post


Link to post
Share on other sites
fallenang-

Your code works fine with one problem, if I enter a mixed number, say HHJ876 it accepts it... any ideas?

Thanks everyone for your ideas, I appreciate the quick feedback!!
0

Share this post


Link to post
Share on other sites
...one more thing I can't remeber what the command is to clear the input buffer? i remember it is "/something" any ideas on the something?

[edited by - justaddwater on March 13, 2004 11:17:15 PM]
0

Share this post


Link to post
Share on other sites
quote:
Original post by Justaddwater
fallenang-

Your code works fine with one problem, if I enter a mixed number, say HHJ876 it accepts it... any ideas?

Thanks everyone for your ideas, I appreciate the quick feedback!!


I compiled it just in case, and keying ''HHJ876'' causes it to print my error message. I have no clue why it doesn''t work for you, but it most definitely should. What compiler are you using and what headers are you including?
0

Share this post


Link to post
Share on other sites
Visual studio dot net

and your right HHJ876 gets caught, the problem is with 876HHJ (and me typeing dyslesicly..uhhh not to mention my spelling)

EDIT added headers

#include <ctime>
#include <cctype>
#include <limits>
#include <iomanip>
#include <iopstream>

[edited by - justaddwater on March 13, 2004 11:57:42 PM]
0

Share this post


Link to post
Share on other sites
the key is in the idea of the reader ... the reader stops reading upon encountering an input it isn''t wanting to process ... so, like i said about the float situation (where the user types "45.23" and what is left in the buffer (waiting to be read by the next read call - is ".23"

In your case, the first call reads the numbers, but just leaves the text for later.

this is no different than when you read characters, and the user types "hello[ENTER]" ... the first call gets "h" and leaves the "ello" in the queue ... so a loop would build the string ... but if you only wanted the user to type 1 character, and they typed "hello", if your next call innocently asked them to enter a number, it would error out, because the "ello" are the first things in the queue

... wish I could remember what you can do to throw those characters away safely ...
0

Share this post


Link to post
Share on other sites
I think this should work nicely.

int num;
while (cout << "Enter a number: " && !(cin >> num) || cin.peek() != ''\n'')
{
cout << "That''s not valid input. Try again" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ''\n'');
}
0

Share this post


Link to post
Share on other sites
with your code how would i modify it to do a check? I cant tell where to put in the line

if (num < 1) output error and redo

[edited by - justaddwater on March 14, 2004 1:29:09 AM]
0

Share this post


Link to post
Share on other sites
Ive never seen a statement written like that before with the cout in the while condison , so im not sure hoe you mean I could add it in there...
0

Share this post


Link to post
Share on other sites
The "cout << foo" part returns a true value, so it doesn''t affect breaking out of the loop - it''s used for its side effect, i.e. printing the text. The other guy is putting it there so that text will output before the input is checked, even off the first iteration.

All you need to do is add all of your filters to that line, chained with logical OR''s.

so at a high level it looks like

while (do output and report true) and ((input was not an integer) or (next character after the input isn''t a newline) or (anything else that would render the input invalid <-- YOU FILL IN THIS PART))
report error; // because "do output and report true" was of course true, and one of the filters failed, making that part true as well

clear the failed state of the input stream; // so we don''t report an error next time

skip ahead in the input stream to the next newline; // so we don''t re-parse the invalid data and get stuck in a loop


0

Share this post


Link to post
Share on other sites
I added this to my program and it worked great, but there is one problem now with it.

When the num < 1 triggers you have to hit CR twice to get back to prompt, when any other condishon triggers it loops right back to prompt


while (cout << "Enter Quantity: " && !(cin >> Quantity) || (Quantity < 1) || cin.peek() != '\n')
{
cout << "Quantity must be whole positive number!" << endl;
cin.clear();
cin.ignore();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}


[edited by - justaddwater on March 15, 2004 10:32:21 PM]

[edited by - justaddwater on March 15, 2004 10:33:39 PM]

[edited by - justaddwater on March 16, 2004 6:01:15 PM]
0

Share this post


Link to post
Share on other sites
Unpatched VC6?

Go here and here to fix it.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan
0

Share this post


Link to post
Share on other sites
quote:
Original post by Justaddwater
I added this to my program and it worked great, but there is one problem now with it.

When the num < 1 triggers you have to hit CR twice to get back to prompt, when any other condishon triggers it loops right back to prompt

*snip*




Err, what''s with the two ignore lines?

The program works perfectly for me if you comment out/delete the first line.
0

Share this post


Link to post
Share on other sites
dude, why is it always the stupid little things like that that trash it!?!?!

Thanks much for spottin that extra ignore!
0

Share this post


Link to post
Share on other sites