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.
You’re comparing
myCharwrong. Try this instead:A second problem is that
getcharreturnsint, notchar. Maybe you can rewrite it like this:EDIT
In light of comments, I think some stdio operation before that
whileis leaving a\nin the buffer.Instead of
scanf("%d", &radix)try:That space will make
scanfeat the remaining blanks (including the newline).