2

For my homework assignment, I need to implement Horners Algorithm for converting between bases.

I have been told to use getchar() for this assignment. But I am having a problem where when I hit enter, the program doesn't terminate and just takes in more chars.

Example:

bryce> ./pa1 Enter the fromRadix:16 Enter the toRadix:2 abc abc ^C bryce> 

Code:

int readRadixA(int radixA) { char myChar = getchar(); int result = 0; int run = 0; while(myChar != EOF) { if(myChar == "\n") break; Horners(); myChar = getchar(); } return result; } 

I am not asking for help implementing Horners; I am asking for help to terminate the getchar() correctly.

5
  • I had to do ^C to terminate the program. Commented Feb 4, 2012 at 7:48
  • 1
    Have you tried ^D, the EOF character of Unix shells? :) On Windows, ^Z might work in this role. You could also consider finishing your program on two \ns in a row (that is, typing an empty line). Commented Feb 4, 2012 at 7:51
  • 1
    getchar returns an int. Not a char, an int. myChar needs to be an int. Commented Feb 4, 2012 at 7:51
  • I'm doing this via a Mac SSHed into a linux box. Commented Feb 4, 2012 at 7:54
  • thank you @9000 , your reply helped me. i use Debian 10 on virtualbox 6.1.2 Commented Feb 28, 2020 at 14:54

5 Answers 5

5
if(myChar=="\n") ^ ^ 

You're comparing myChar wrong. Try this instead:

if(myChar == '\n') ^ ^ 

A second problem is that getchar returns int, not char. Maybe you can rewrite it like this:

int myChar; while((myChar = getchar()) != EOF && myChar != '\n') { /* Your stuff. */ } 

EDIT

In light of comments, I think some stdio operation before that while is leaving a \n in the buffer.

Instead of scanf("%d", &radix) try:

scanf("%d ", &radix); ^ 

That space will make scanf eat the remaining blanks (including the newline).

Sign up to request clarification or add additional context in comments.

6 Comments

I try that, and my program immediately terminates after I enter my commands for enter radix from and to. Meaning I cannot input into the get char.
@Snow_Mac What did you enter ? What did you expect ?
Program prompts me: Enter the fromRadix:, then I type 16, then it prompts:Enter the toRadix:, then I type 2. After this is where my method gets called with getchar(); which I should be able to type abc.
I'm doing this via a Mac SSHed into a linux box.
@Snow_Mac: I'd bet that the code that reads in fromRadix and toReadix (scanf() I bet) is leaving the '\n' in the buffer, so it's the first character returned by getchar() when readRadixA() is executed.
|
1

Check the return type of getchar(). Yes, it's an int. That's because EOF must have a value that can be distinguished from a valid character. myChar must actually be made to be int.

Comments

0

Try this code

int readRadixA(int radixA) { char myChar; int result = 0; int run = 0; do { myChar = getchar(); // implement horners here }while(myChar != 13); return result; } 

3 Comments

What's up with the 13? Don't you know it's unlucky?
13 is the ascii value of ENTER key
But it is not immediately obvious to those who have not had their ASCII tables memorized. And it's a carriage return, BTW, not the "Enter" key. If you insist on not using '\r', at least use the conventional 0x0D. But then again, a linefeed, 10, 0x0A, or '\n' is much more standard than a carriage return.
0

I checked your code I think you are leaving a '\n' in the input keyboard buffer after the toRadix.

And their is one more thing that

 getchar() 

reads all the characters in one go till a '\n' is received.

And there is one more mistake you have committed by comparing a

 char to a pointer e.g mychar=="\n" 

further information about your implementation of toRadix can be really helpful to answer your question

Comments

0

On linux, to end the standard input, you have to type  Ctrl-D. The kernel and tty layers makes that an end-of-file mark or condition. Then getchar gives EOF (which is not a valid char, for example on systems where char are unsigned bytes between 0 and 255, EOF could be -1).

Notice that feof(3) is valid only after a read operation (e.g. getchar, fgets, etc...) so coding while(feof(stdin)) is generally wrong (contrarily to what I wrote in the previous version of this answer). You'll better test that getchar is returning EOF so your myChar should be an int (not a char).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.