This is my first time posting to a 4Chan text board so i apologize if i seem a bit selfish asking for help in my first post but this is the only board that im confident has frequent users.
Here is my question:
For a homework assignment in C/C++ i need to have some code that will terminate the program if the user inputs a value outside of a preset range. Ive looked through the book and all the class notes i have and cant seem to find any reference on how to make that happen. I know someone here should be able to give me an idea on how to do this.
get input
while(input > lower_range and input < higher_range)
{
do_stuff
get input
}
finished
Name:
Nobledragon2006-09-10 17:17
Thanks. The 'while' statement was going to be covered in the next class session (which is also when the assignment is going to be due) and I haven't read too much about it yet. Never thought it could be used that way.
Name:
Anonymous2006-09-10 17:26
>>2 for(get input; input > lower_range and input < higher_range; get input) do_stuff
fix'd
Name:
Anonymous2006-09-10 17:31
>>2
ooooooohhhhhh, you just got served by >>4's shorter code
and why the fuck would you sage when you are answering the OP?
Actually, this could easily be rewritten to only have one 'get input' (because duplicate code is bad), and depending on the situation the result could be more elegant as well.
Name:
Nobledragon2006-09-10 18:36
Also, i forgot to mention that it also needs to display an error message to the user before exiting the program. Also keep in mind that this is my first time programming anything.
Name:
Anonymous2006-09-10 19:03
>>7
Just try and write a program with what you know now, then get back to us with your code if you can't get it to work properly.
Name:
Anonymous2006-09-10 19:47
>>8
Fuck that, if he needs help with this then he's going to need our help with every single one of his assignments.
>>4
use for loops when you need to loop a known number of times. use while loops for when you need to loop an indefinite number of times
>>6
duplicate code? there isn't much duplication going on there only the get input sequence. I was using the Yourdon loop. Yourdon loop states:
get input
while(condition)
{
do_stuff
get input
}
The Yourdon loop is pretty useful such as its application in control break algorithms.
Alternatively, this should make you happy
do
{
get input
do_stuff
} while(input > lower_range and input < higher_range)
display error
Name:
Anonymous2006-09-10 20:22
>>10 use for loops when (...)
No, for loops are good whenever you have an initial statement, and a while loop with a related statement at the bottom. Also, a for loop keeps the two get_inputs together, which is nice if you ever need to change them.
Alternatively, this should make you happy
You need to check the input right after getting it, not after doing stuff.
This works:
while(true) {
i = get_input();
if(!inrange(i)) break;
do_stuff();
}
And can be written without the break:
while(inrange(i = get_input())) do_stuff();
However, that last version becomes less elegant when you have two checks in place of inrange.
Actually i just got it done. Just needed help figuring out how to get the program to terminate on an invalid input. Ended up using 'if' and 'else' statements to make it work. Also i was looking for a command word or something that can just be put into the code like a 'break' command with 'switch' statements. Anyways thanks for the help guys, at least i now know where to go to get some answers.
Name:
Anonymous2006-09-12 1:51
>>12
"break" gets you out of loops as well as switch statements. And you can call exit(n) at any time, where 'n' is the exit code -- 0 for success, anything else (typically 1) for failure.
The looping was a bit of a red herring since you never really asked for a loop, only that it exit if they put the wrong value in. That's probably why they asked you to do this _before_ they taught you about loops. :)
Of course, if checking that input is _all_ your app does, then the error message is the only thing that distinguishes failure from success. So everyone just automatically assumes you want a loop.
To do exactly what you wanted, generally you would just do
if (condition) {
print_error();
exit(1);
}
at any point in the program, whether you're inside a loop or not.
>>14
Break is much less bad than goto, as it goes to the end of the construct (loop or whatever), a location which is easy to find (as opposed to having to track down a goto target). It's still better to avoid breaks outside of switches, though.
Name:
Anonymous2006-09-12 12:56
I have a related question. Let's say I'm printing out 0 to infinity (arbitrary precision, okay). How do I get out of the loop and stop printing when 'q' is pressed on the keyboard?
Name:
Anonymous2006-09-12 15:49
>>16 while run_loop
print stuff
case key
when ?q:
run_loop = false
end
end
GETIN = ffe4 ;reads key from keyboard (key in .A)
CHROUT = ffd2 ;writes char in .A to screen
.org c000 ;begin at 49152
WEND
jsr GETIN ;get a key
cmp QKEY ;is it 'q' or whatever?
beq EXIT ;yep! ok, done
;time to print STUFF
ldy #$00 ;.Y is pointer to string, init it!
PRLOOP
lda TEXT1,y ;get next (or first) byte of string
beq WEND ;if it's 0, we're done, go back and check keyboard again
jsr CHROUT ;well, print that character
iny ;increment pointer
jmp PRLOOP ;go print it now
EXIT
rts ;kthxbye!
QKEY
.asc 'q' ;this is a 'q'
TEXT1
.asc "STUFF" ;the string we will print
.db 0D ;ending CR
.db 00 ;terminating zero
Name:
Anonymous2006-09-12 23:54
Here's a C version. You can also do it with select(), and you probably would in a bigger program, but this is short and simple. Note that unless you change the terminal mode to a more raw mode, you'll have to type "q<enter>".
int main(void) {
int in_fd = fileno(stdin);
fcntl(in_fd, F_SETFL, O_NONBLOCK);
int i = 0;
int done = 0;
char buf[11];
for (; done == 0; i++) {
int nread = read(in_fd, &buf, 10);
if (nread > 0) {
buf[nread] = '\0';
if (strchr(buf, 'q') != NULL)
done = 1;
} else if (errno != EAGAIN) {
perror("read");
exit(1);
}
}
printf("Counted to %d.\n", i);
return 0;
}
Name:
Anonymous2006-09-13 4:45
Use SDL events
Name:
Anonymous2006-09-13 10:20
Use a second thread to listen for 'q' and set a flag
Name:
Anonymous2006-09-13 13:48
>>26
Seeing as the kid is in an intro class, I think the teacher would know something is up if he came in with that and couldn't really explain it at all.
>>38
Lol, here's a student repeating what his programming 1 teacher told him believing it's the absolute truth in the Universe.
A good programmer will use the right tool for the right job, and break (and continue) is the right tool so many times for a number of reasons like avoiding duplicate code (duplicate code is a bad thing), simplifying loops, or not making your function wider than taller because of fugly indentation, plus a few purist causes in where you'd like to break rather than to indent differently code that works at the same level or in a similar set of cases. Even goto has its uses in languages when you can't break twice or low-level language when you need to achieve some extremely cunning hack there's no good way without.
Name:
Anonymous2006-09-18 10:51
>>41
there's no reason to use goto or break in a high-level language like c.
stop using your shitty formatting as an excuse for writing lousy code.
Name:
Anonymous2006-09-18 10:53
>>42
Lol, first you're University Hello World-purist enough to say you shouldn't use break, then you call code indentation "shitty formatting". Ok; in a few (5-6) years you'll get to work on real code and we'll see.
P.S.: How's your homework?
Name:
Anonymous2006-09-18 11:09
void input_loop() {
char i = get_input();
if (inrange(i)) {
do_stuff();
input_loop();
}
}
Name:
Anonymous2006-09-18 14:44
>>44
Recursion is superior! Iteration considered harmful!
Hey people, don't run the polite n00b out of town on a fucking rail now. We could be witnessing the birth of a new C programmer here, and that's something you don't get too often anymore.
>>53
because it's not true.
if you want to get a reaction, you have to say something true.
like this:
ruby is slow.
python is ugly.
no one uses smalltalk for anything other than trying to prove that it's better than ruby.
java, smalltalk, and python are the only languages worse than ruby.
Name:
Anonymous2006-09-19 13:44
>>51
You can't just push butan and then divide by zero!