programming help! | xltronic messageboard
 
You are not logged in!

F.A.Q
Log in

Register
  
 
  
 
Now online (1)
recycle
...and 427 guests

Last 5 registered
Oplandisks
nothingstar
N_loop
yipe
foxtrotromeo

Browse members...
  
 
Members 8025
Messages 2614110
Today 7
Topics 127542
  
 
Messageboard index
programming help!
 

offline marlowe from Antarctica on 2006-10-05 07:11 [#01982463]
Points: 24589 Status: Lurker



Hi there, looking for a little help with C+

If I ask for an input which is supposed to be an integer,
and the user enters a string, how do I stop the program from
crashing.

Help greatly appreciated!


 

offline dog_belch from Netherlands, The on 2006-10-05 07:56 [#01982478]
Points: 15098 Status: Addict | Show recordbag



I don't know c+ but wouldn't you have to have a routine that
validated the user's input and checked it was in the right
format, and that if it contained any erroneous characters to
ABORT ABORT, before proceeding with the ... whatever your
program does.


 

offline aneurySm from Ypsilanti (United States) on 2006-10-05 08:08 [#01982485]
Points: 1701 Status: Lurker



in php there is a function to tell you what type a variable
is..
i don;t know what it's called


 

offline oxygenfad from www.oxygenfad.com (Canada) on 2006-10-05 10:40 [#01982576]
Points: 4442 Status: Regular



const char* str_int = "777";
const char* str_float = "333.3";
int i = atoi(str_int);
float f = atof(str_float);

Try that ...


 

offline oxygenfad from www.oxygenfad.com (Canada) on 2006-10-05 10:48 [#01982585]
Points: 4442 Status: Regular



(psuedocode)
If cin = ascii 41 to 49 then
Allow person to enter characters
else
""

Something like that maybe?



 

offline marlowe from Antarctica on 2006-10-05 16:47 [#01982797]
Points: 24589 Status: Lurker | Followup to oxygenfad: #01982585



I heard there was some kind of string to integer translator,
but I've no idea how THAT would work - it's a doozie!


 

offline ecnadniarb on 2006-10-05 17:18 [#01982803]
Points: 24805 Status: Lurker | Show recordbag



C or C++

Knowing you it will be C++ :)

I have to go to bed now but read the input in as a string.
You are best using the iostream header file and have a look
at passing the string to istringstream.

Off the top of my head (this may be wrong), something like

std::istringstream i(s);
int x;
if(!(i >> x)){
throw your error handling bit
}
return x;



 

offline big from lsg on 2006-10-05 17:21 [#01982804]
Points: 23728 Status: Lurker | Show recordbag



10 print "hi"
20 goto 10


 

offline 010101 from Vancouver (Canada) on 2006-10-05 17:21 [#01982805]
Points: 7669 Status: Regular



Try

10 print "010101 is the best!!"
20 goto 10


 

offline 010101 from Vancouver (Canada) on 2006-10-05 17:21 [#01982806]
Points: 7669 Status: Regular | Followup to big: #01982804



you beat me to it!!!!


 

offline oyvinto on 2006-10-05 17:24 [#01982807]
Points: 8197 Status: Lurker | Show recordbag



basic <3


 

offline marlowe from Antarctica on 2006-10-06 18:05 [#01983500]
Points: 24589 Status: Lurker



hehe I love those old 10 PRINT 20 GOTO 10 old school codes!
reminds me of my spectrum days long long ago


 


Messageboard index