Snow_Mac
Snow_Mac

Reputation: 5797

Terminate a while loop using `getchar()`

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.

Upvotes: 2

Views: 5691

Answers (5)

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).

Upvotes: 0

rakesh
rakesh

Reputation: 612

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

Upvotes: 0

cnicutar
cnicutar

Reputation: 182639

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).

Upvotes: 5

Sunil Kumar B M
Sunil Kumar B M

Reputation: 2795

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;
}

Upvotes: 0

FatalError
FatalError

Reputation: 54551

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.

Upvotes: 1

Related Questions