| Author |
cannot understand this code
|
Ankur Jain Kothari
Ranch Hand
Joined: Feb 08, 2010
Posts: 152
|
|
can anyone explail me this code? what is the use of getChar function? i want to test 2 numbers 1 and 2...when i press 1 and then Enter the program stops gives me absurd result...i want to enter 2 also and test it 1...
Thank you
|
Do What You Wanna Be....Taking Things The Way They Come
scjp 1.6 91 percent, scmad 90 percent(rounded off to nearest integer)
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8457
|
|
|
To me it looks like, the program is requesting for entries from the user (probably from the command prompt) and then compares the two characters that the user entered and prints the result accordingly. I really do not know much about C programming language. I can't say if the getChar() function would request for any input from the keyboard.
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
Ankur Jain Kothari
Ranch Hand
Joined: Feb 08, 2010
Posts: 152
|
|
ya to me the program seems to work the same as you think...but it doesnt work that way.....there is something wrong with the getchar function...as soon as i press a number and press enter the program stops....without even asking me 2nd number...how dumb the computer can get
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
Okay. So you press the 1 key and the Enter key. The first getchar() gets the first character you typed (the 1) and the second getchar() gets the second character you typed (the Enter).
I don't think we can say it's the computer being dumb here.
|
 |
Ankur Jain Kothari
Ranch Hand
Joined: Feb 08, 2010
Posts: 152
|
|
So what is the solution? if i press 12 and then ENTER...how does this work? why does the program end as soon as i press enter?
btw the computer has no feelings..it wont mind
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
|
You call getchar twice. The first call gets 1 and the second call gets 2. The code prints something to the console and then returns 0. What did you expect to be the result of returning from the main method?
|
 |
Ankur Jain Kothari
Ranch Hand
Joined: Feb 08, 2010
Posts: 152
|
|
ok...but what if i want to enter 1 first...then computer asking me to type the second number and then i type 2 and then Enter.? Can this be possible? how can we do this with getchar? here with getchar, it reads the first charachter and then 2nd charachter upon being called again...?
i hope my question is clear
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 14606
|
|
getchar() is a low level function. It doesn't read numbers, it read characters and returns them as ASCII values. It doesn't do parsing of any type. And of course, it can't guess what you want.
If you want to read numbers, then you have to read for the first digit, read til you no longer get a valid digit, and then parse it. Or you can call scanf() instead.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
amitabh mehra
Ranch Hand
Joined: Dec 05, 2006
Posts: 98
|
|
Ankur Jain Kothari wrote:ok...but what if i want to enter 1 first...then computer asking me to type the second number and then i type 2 and then Enter.? Can this be possible? how can we do this with getchar? here with getchar, it reads the first charachter and then 2nd charachter upon being called again...?
i hope my question is clear 
any specific reason that you want only getchar()? you can very well use scanf or fscanf to get user input, one at a time, and parse them in whatever way you want or validate them before using them.
|
 |
Ankur Jain Kothari
Ranch Hand
Joined: Feb 08, 2010
Posts: 152
|
|
|
i wanted to understand the getchar function..i see it everywhere
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
|
And are there no books about the C language? You've spent several days here misunderstanding that function when I'm sure you could have found out about it in the first couple of chapters in a book about C. If there were any, that is. To me that seems like a very ineffectual way of learning a language.
|
 |
 |
|
|
subject: cannot understand this code
|
|
|