stupid var question in C

Started by
8 comments, last by shun 22 years, 8 months ago
Hi there, got a question about a variable in C, it''s an integer that I called user_input and as the name says, it needs to get regulary some data from the user. so, the structure goes like this user_input = getchar() if (blabla = blabla){ printf("blablabla"/*multiple choice*/); user_input = getchar(); if(blabla = blabla) printf("blabla"); else printf("other"); else do some other thing; } The problem is that when I want to have the second getchar(), he doesn''t ask any question any more, and I can''t type the option I want, he follows the program and puts something in user_input where I don''t know he has it from. Can someone help? Thanks Life is a game, sometimes you win, sometimes you lose but in the end everyone loses
Life is a game, sometimes you win, sometimes you lose but in the end everyone loses
Advertisement
At a guess id say you need to flush the input buffer. Use:

fflush(stdin);

after your calls to getchar().
  if(blabla = blabla)  

Shouldn't that be:
  if(blabla == blabla)  


Edited by - Clash Rocker on August 13, 2001 1:54:23 PM
Yes it is
if (blabla == blabla)
do that;

sorry for mistake ^__^

thanks for the advise to flush ^__^

shun

Life is a game, sometimes you win, sometimes you lose but in the end everyone loses
Life is a game, sometimes you win, sometimes you lose but in the end everyone loses
could it be that user_input is of type char and getchar() returns an int?

maybe you're pressing special characters and getchar() returns 0x00 first and then the key code?

the code below should work:

    char c;c=0;while (c!=27){    c=getch();    switch(c)    {       case 'A': do_a(); break;       case 'B': do_b(); break;       default: break;    };}    


i'd use do_a() to create another one of these read loops if you want to read more data.


WeP
http://www.planetzztpp.com

Edited by - wep on August 14, 2001 1:59:20 PM
WePhttp://www.planetzztpp.com
I must admit I never tried the Switch function, but user_input is defined as int, and gives back integer information and on the first If function, it works good, but than it goes to another if, something like this

if (user_input == 49){
printf("blablablabla");
user_input = getchar();
if (user_input == 49)
printf("blablabla");
else
printf("blablabla");
}
else if (user_input ==50){
and continues like this for a long time

I hope this gives clear idea of what is going on.
someon sugested the fflush() fonction, but didn''t help :-(

why this 49 and 50? it''s the ascii code of 1 and 2, goes to 4

Thanks for helping me out of this guys, will try case and so, but have to learn it first ^__^,

Shun
Life is a game, sometimes you win, sometimes you lose but in the end everyone loses
I must admit I never tried the Switch function, but user_input is defined as int, and gives back integer information and on the first If function, it works good, but than it goes to another if, something like this

if (user_input == 49){
printf("blablablabla");
user_input = getchar();
if (user_input == 49)
printf("blablabla");
else
printf("blablabla");
}
else if (user_input ==50){
and continues like this for a long time

I hope this gives clear idea of what is going on.
someon sugested the fflush() fonction, but didn''t help :-(

why this 49 and 50? it''s the ascii code of 1 and 2, goes to 4

Thanks for helping me out of this guys, will try case and so, but have to learn it first ^__^,

Shun

Life is a game, sometimes you win, sometimes you lose but in the end everyone loses
Life is a game, sometimes you win, sometimes you lose but in the end everyone loses
quote:Original post by shun
why this 49 and 50? it''s the ascii code of 1 and 2, goes to 4

You don''t need to convert to ascii. Just compare directly to a char, eg.:

if (user_input == ''1'')


I tried in the begining to use the simple digits, but maybe I did something wrong, will try tonight ''cuz no programming stuff here ^__^

But, do you know how to help me to have second User Input?

Thanks a lot

PS will keep the case thing to try at home ^__^

Life is a game, sometimes you win, sometimes you lose but in the end everyone loses
Life is a game, sometimes you win, sometimes you lose but in the end everyone loses
...don''t know if you''re still referring to your first post above...but the if-else structure in that is very ambiguous, the last else will never be executed, maybe I''ve just not understood the following posts but it may be part of the problem.

I''d definately go with the switch statement....it''s great for situations like this...

This topic is closed to new replies.

Advertisement