• ### Announcements

#### Archived

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

# stupid var question in C

## 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

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

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627682
• Total Posts
2978615

• 13
• 12
• 10
• 12
• 22
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.17786796391010284, "perplexity": 5771.182477791743}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2017-43/segments/1508187823839.40/warc/CC-MAIN-20171020063725-20171020083725-00439.warc.gz"}