Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Programming help

Name: Nobledragon 2006-09-10 16:38

Hi,

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.

Name: Anonymous 2006-09-10 17:12 (sage)

I'm feeling nice today so I'll help you out

get input
while(input > lower_range and input < higher_range)
{
    do_stuff
    get input
}
finished

Name: Nobledragon 2006-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: Anonymous 2006-09-10 17:26

>>2
for(get input; input > lower_range and input < higher_range; get input) do_stuff
fix'd

Name: Anonymous 2006-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?

>>3
lol, reading about the while loop

Name: Anonymous 2006-09-10 17:44 (sage)

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: Nobledragon 2006-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: Anonymous 2006-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: Anonymous 2006-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.

Name: Anonymous 2006-09-10 19:56 (sage)

>>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: Anonymous 2006-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.

Name: Nobledragon 2006-09-10 20:42

>>9

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: Anonymous 2006-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.

Name: Anonymous 2006-09-12 11:08

>>12
break == goto == FAIL

Name: Anonymous 2006-09-12 11:32

>>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: Anonymous 2006-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: Anonymous 2006-09-12 15:49

>>16
while run_loop
  print stuff
  case key
    when ?q:
      run_loop = false
  end
end

Name: Anonymous 2006-09-12 16:04

>>17
umm..real code plox. preferebly in c or c++.

Name: Anonymous 2006-09-12 16:40

int inkey; int stpflg = 0;
do {
 printf("stuff"); inkey = getchar();
 switch inkey {
  case 'q' : stpflg = -1; break;
  }
} while (!stpflg)

better way:
for ( int stpflg = 0 ; stpflg = getchar() ; stpflg == 'q' ) { printf("stuff"); }

Name: Anonymous 2006-09-12 16:51

>>19
btr wy am i rte?

Name: Anonymous 2006-09-12 17:03

moar liek
int i = 0;
while(getchar()!='q') printf("%d\n",i++);
Except I think getchar blocks or waits for enter or something...

Name: Anonymous 2006-09-12 17:08

>>18
10 n = 0
20 WHILE NOT INKEY$ = "q"
30 PRINT n
40 n = n + 1
50 WEND
60 END

Name: Anonymous 2006-09-12 18:12

>>19 and >>21 uses getchar() and it waits for input so it won't work.

Name: Anonymous 2006-09-12 18:31

>>23
>>22 is superior.

Name: Anonymous 2006-09-12 22:41

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: Anonymous 2006-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>".

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

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: Anonymous 2006-09-13 4:45

Use SDL events

Name: Anonymous 2006-09-13 10:20

Use a second thread to listen for 'q' and set a flag

Name: Anonymous 2006-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.

Name: Anonymous 2006-09-13 13:52

What the fuck is a Yourdon loop?

Name: Anonymous 2006-09-13 13:56 (sage)

>>29
He is not answering the OP question.

Name: Anonymous 2006-09-14 16:38 (sage)

USE A TACHYON BEAM

Name: Anonymous 2006-09-15 19:18

>>28
how do i maked new treads?

Name: Anonymous 2006-09-16 2:37

Name: Anonymous 2006-09-17 16:36


int n=0, smallrange=0, bigrange=100;
for(;;)
{
  cout << "\nEnter a number.\n";
  cin >> n;
  if( n > bigrange || n < smallrange )
    break;
}

Name: Anonymous 2006-09-17 17:02

The OP is obviously a complete retard so all your answers won't help.

Name: Anonymous 2006-09-17 19:10

>>33
look up posix threads, maybe? i dunno, what is the standard thread library for C?

Name: Anonymous 2006-09-18 0:14

>>35
break == goto == fail

Name: Anonymous 2006-09-18 2:04

>>38

Von Neumann disagrees

Name: Anonymous 2006-09-18 2:26

>>39
Well, von Fuck You agrees.

Name: Anonymous 2006-09-18 5:06

>>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: Anonymous 2006-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: Anonymous 2006-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: Anonymous 2006-09-18 11:09

void input_loop() {
 char i = get_input();
 if (inrange(i)) {
  do_stuff();
  input_loop();
 }
}

Name: Anonymous 2006-09-18 14:44

>>44
Recursion is superior! Iteration considered harmful!

Name: Anonymous 2006-09-18 18:08

>>44
Retarded

Name: Anonymous 2006-09-18 19:52

(define (input-loop)
  (when (inrange (get-input))
    (do-stuff)
    (input-loop)))

Terminal recursion for teh win!

Name: Anonymous 2006-09-18 20:20

recursion is just goto. therefore it fails.

Name: Anonymous 2006-09-18 22:23

>>38

How do I learned assembly?

Name: Anonymous 2006-09-19 0:27

>>49
Invent a time machine.

Name: Anonymous 2006-09-19 9:18

>>50
It works like this:

1. PRES BUTAN
2. TIME PARADOX

Name: Anonymous 2006-09-19 11:48 (sage)

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.

Name: Anonymous 2006-09-19 12:46

>>48
hay why is nobody biting my troll :(

Name: Anonymous 2006-09-19 13:14

>>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: Anonymous 2006-09-19 13:44

>>51
You can't just push butan and then divide by zero!

Name: Anonymous 2006-09-19 14:05

>>55
<html><body>
<input type="button" value="butan" onclick="alert(1/0)">
</body></html>

Name: Anonymous 2006-09-19 14:35

>>56
thread over OH SHI-

Name: Anonymous 2006-09-19 15:40 (sage)

<input type="button" value="butan" onclick="a=alert;alert=new Function('a(42)')"><br>
<input value="1" id="num0"><input value="0" id="num1">
<input type="button" value="divide" onclick="alert(document.getElementById('num0').value/document.getElementById('num1').value)">

Name: Anonymous 2006-09-20 7:55

>>58
Ew, new Function. gb2/Java

Name: Anonymous 2006-09-20 11:08 (sage)

>>59
how else would you what >>58 did?

Name: ​​​​​​​​​​ 2010-09-08 21:35

Name: Anonymous 2010-11-13 12:14

Name: Sgt.Kabu䵳享kiman⊗ِ 2012-05-28 20:10

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List