Can somebody help me with this in C....

Hello.

I'm trying to make a input-sequense , where i'm

"typing" upp to 100 characters but when i'm

pressing key "F1" the sequense should end.

Can someone help me.....?

If so ..many thanks.


I've tried like this......but ofcourse it dont work.

.....

....

while( scanf("%c", &cha[i]) != 315); goto end;{

i++;}

end:

.....


Comments

  • : Hello.

    : I'm trying to make a input-sequense , where i'm

    : "typing" upp to 100 characters but when i'm

    : pressing key "F1" the sequense should end.

    : Can someone help me.....?

    : If so ..many thanks.


    : I've tried like this......but ofcourse it dont work.

    : .....

    : ....

    : while( scanf("%c", &cha[i]) != 315); goto end;{

    : i++;}

    : end:

    : .....




    scanf returns the number of 'elements' it was able to read in. With your call, it will return either 1 or 0 (it was able to read a character or it wasn't).


    You want to check the value of the character read in, I presume, but this will never equal 315, since 315 requires more than a single char to store.


    You probably want to be using getc or getch, which returns an int which can (as a nonstandard feature) return function key codes and that sort of stuff (not that I advocate using this trick for portability reasons).


    As a point of clean coding you probably shouldn't be using 'magic numbers' in your code anyway. A month from now you might have no idea what 315 means. Probably better to use a constant or a #define or something to give that particular number a name.




    (rant)

    However, the most horrifying thing about your example is the use of the goto. What demented teacher or abominable book even brought up the existence of that evil construct? Do yourself a favour. Get rid of 'goto' out of your vocabulary. If you think you absolutely need one, think again. If you still think you need one, think a third time. If you still absolutely think you need one, comment heavily. There's nothing worse than tangled code as a result of a goto. I've never used one in C personally (or any other high level language save ancient BASIC variants) in the years I've been programming. I've seen one almost barely possible acceptable use for it, and I still think it was stupid.


    (/rant)


    Anyway. Good luck with your problem! Hope this helped.




  • Hello Michael.

    Thanks for your answer.


    I've defined "315" as key F1.

    I must have specific "keys" to "goto" certain

    "areas" in the program.


    Because the program requires to "jump",to

    specific "areas" in specific situations.

    And I must decide to which part ,not the program.


    You didn't like this for portability reasons...?

    I wonder what you recomend instead..?


    And you didn't like "goto" at all.

    Is it better to use.....do..while......instead...?


    Thanks again.

    Peter.


  • : I've defined "315" as key F1.




    What I recommend is that, somewhere in your program, presumably at the top, you do one of the following lines:


    #define F1_CODE 315

    const int F1_CODE=315;


    That way, if you port to another machine, or if for some weird reason the value changes, you change it in only one place. It also gives a name to what is otherwise, to perhaps even yourself some months later, just a magic number in the middle of the code. Hereafter, you can use F1_CODE where you're currently checking against 315.


    Makes the code a LOT more readable.



    : I must have specific "keys" to "goto" certain

    : "areas" in the program.


    Nothing wrong with this as a concept.




    : You didn't like this for portability reasons...?


    I wasn't keen on using pseudo-ascii codes above 255, but if you have to use them, go for it. Just don't put magic numbers all over your code.



    : I wonder what you recomend instead..?


    See above about magic numbers.


    : And you didn't like "goto" at all.

    : Is it better to use.....do..while......instead...?




    It is almost always preferable in a structured language to avoid gotos. You can always rewrite without gotos. You almost always should. gotos end up producing 'spaghetti code', where the path of execution hops around. The language is complex enough without that, and is certainly complex enough not to require it.


    If I could, I'd petition the language committees to abolish the 'goto'.


    If you'd like some suggestions as to how to write without gotos, throw a goto-ridden piece of code at me, and I'll rewrite part of it for you.


    Perhaps I'm being really cranky about this, but it leads to very poor programming habits, and I've had to debug code with lots of gotos in it and it's just hideous. My immediate reflex on seeing them is 'ick'.


    I certainly believe that a key-driven program can be written without them. :)


    Good luck on your project.




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories