• ### Popular Now

• 12
• 12
• 9
• 10
• 13

#### Archived

This topic is now archived and is closed to further replies.

# stupid var question in C

This topic is 6062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
At a guess id say you need to flush the input buffer. Use:

fflush(stdin);

##### Share on other sites
  if(blabla = blabla)

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

Edited by - Clash Rocker on August 13, 2001 1:54:23 PM

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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'')

##### Share on other sites
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