Jump to content

troubles with getchar

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1
elvira23

elvira23

    Newbie

  • Members
  • PipPip
  • 14 posts
Hi,
i have just started learning C and trying some code. Here is a snippet which is causaing some troubles.

printf("the value of EOF is %d\n",EOF);

c = getchar() ;
while (c != EOF){
putchar©;
c=getchar();
}
the printed EOF value is -1. that' fine. however i am having troubles with the rest of the code. when i run it, and say i type for fun cccccccccccccccccc and then hit return,the whole chain of characters is printed out. however getChar is meant to read only ONE character. when i type -1, the loop is not exited. instead -1 is printed. any explanation please.

Cheers

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
-1 is two characters, '-' and '1'. You will need to type Ctrl-D or Ctrl-Z to exit (depending on your OS).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
elvira23

elvira23

    Newbie

  • Members
  • PipPip
  • 14 posts
thanks for your reply. i was able to exit with ctrl+c being in winXP. but this not my main question. getchar is meant to read one char, why is it reading more, including ctrl-c or -1 or ccccccccccccccccc

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

elvira23 said:

the printed EOF value is -1. that' fine.
Now that you know that, forget it. :p EOF "expands to an integer constant expression, with type int and a negative value" -- not necessarily -1.

elvira23 said:

when i run it, and say i type for fun cccccccccccccccccc and then hit return,the whole chain of characters is printed out. however getChar is meant to read only ONE character.
It does read only one character at a time, and it is. You type a bunch of letters, but these are not "submitted" to the stdin until you press enter. Then all of the characters are available. It reads them one at a time, just like your loop requests, printing each after it is read.

elvira23 said:

when i type -1, the loop is not exited. instead -1 is printed. any explanation please.
WingedPanther already explained -1 being two characters. You really want the signal for EOF, which is done as he mentioned.

#5
elvira23

elvira23

    Newbie

  • Members
  • PipPip
  • 14 posts
thanks for your reply. silly me! Indeed, i changed the loop code with the following:
getchar();
if((c = getchar()) != EOF)
putchar©;
i typed in then: cfgh, only the char c was displayed afterwards. a side point though, i had to precede the if statement above with getchar, otherwise the program execution terminates before allowing me to type in any character. a bank space is printed instead automatically.
why is that? may be because stdin is empty but i did not hit return for the getchar function to go and read the content of stdin

#6
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

elvira23 said:

thanks for your reply. silly me! Indeed, i changed the loop code with the following:
getchar();
if((c = getchar()) != EOF)
putchar©;
i typed in then: cfgh, only the char c was displayed afterwards. a side point though, i had to precede the if statement above with getchar, otherwise the program execution terminates before allowing me to type in any character. a bank space is printed instead automatically.
why is that? may be because stdin is empty but i did not hit return for the getchar function to go and read the content of stdin
What you describe is not what I see.
#include <stdio.h>


int main ()

{

   int c;

   getchar();

   if ( (c = getchar()) != EOF )

      putchar(c);

   return 0;

}


/* my output

cfgh

f

*/


#7
elvira23

elvira23

    Newbie

  • Members
  • PipPip
  • 14 posts
there was a printf before getchar. i am using quincy to edit and run the code. here is the code
#include <stdio.h>

main()

{


float fahr, celcious;

float lowFahr, hiFahr;

int c;



printf(" please type in the low fahr degree ");

scanf("%f",&lowFahr);

printf(" please type in the high  fahr degree ");

scanf("%f",&hiFahr);


for(fahr=lowFahr;fahr<=hiFahr;fahr+=10)

{

celcious= (5.0/9)*(fahr-32);

printf(" fahr temp is %.2f and celcious is %.2f\n",fahr,celcious);

}


printf("the value of EOF is %d\n",EOF);


     /* while ((c = getchar()) != EOF)

         putchar(c);

*/

 getchar(); 

 if((c = getchar()) != EOF)

        putchar(c);


}


#8
elvira23

elvira23

    Newbie

  • Members
  • PipPip
  • 14 posts
ignore...

#9
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
What's wrong with something like this?
#include <stdio.h>

int main ()
{
   int c;
   for ( c = getchar() ; c != '\n'; c = getchar() )
      putchar(c);
   return 0;
}
And it's "Celsius". :P
Wow I changed my sig!

#10
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

elvira23 said:

there was a printf before getchar. i am using quincy to edit and run the code. here is the code
I figured as much. A scanf is leaving a newline.

#11
elvira23

elvira23

    Newbie

  • Members
  • PipPip
  • 14 posts
sorry for the confusion caused. what i should have said is the following. if i remove the getchar before the if statement, a new line is displayed and i am not allowed to type in any character. if i insert getchar as given above, i am able to type any my list of charcters. however, it is the first character and not the second which is displayed back by putchar

#12
elvira23

elvira23

    Newbie

  • Members
  • PipPip
  • 14 posts

ZekeDragon said:

What's wrong with something like this?
#include <stdio.h>


int main ()

{

   int c;

   for ( c = getchar() ; c != '\n'; c = getchar() )

      putchar(c);

   return 0;

}
And it's "Celsius". :P

nothing wrong. i am learning C so i am changing some codes from a textbook to improve my understanding