• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cannot understand this code

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ankur Jain Kothari
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i wanted to understand the getchar function..i see it everywhere
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic