So i just discovered there's an ungetch function. Which i thought would make my life easier. Its been driving me crazy for an hour.
#include <stdio.h>
ungetch(getchar());
putchar(getchar());
//I tried it w/ ungetch and ungetchar
If I'm not mistaken, this should print whatever character you enter.
However, in visual studio 2005 express, i get
warning C4013: 'ungetch' undefined; assuming extern returning int
the same thing goes for ungetchar
in dev-cpp i get
[Linker error] undefined reference to `ungetchar'
or, if i use ungetch,
it just enters a newline (so its like its not putting the character back into the input, its only seeing the newline).
I also tried including stdlib.h. Help please, I really want to be able to use this lol.
Why isn't this working?
Started by Osnarf, Mar 23 2010 07:25 PM
2 replies to this topic
#1
Posted 23 March 2010 - 07:25 PM
|
|
|
#2
Posted 23 March 2010 - 07:31 PM
Okay this is really wierd.
I tried
in dev c++.
i enter 5, press return, and it prints a five. exactly what i'd expect.
if i change the second getchar() to a getch(), as soon as i press a key, it goes immediately to the system("pause"). doesn't even wait for me to key return.
I tried
#include <stdio.h>
int main(void)
{
ungetch(getch());
putchar(getchar());
system("pause");
return 0;
}
in dev c++.
i enter 5, press return, and it prints a five. exactly what i'd expect.
if i change the second getchar() to a getch(), as soon as i press a key, it goes immediately to the system("pause"). doesn't even wait for me to key return.
#3
Posted 24 March 2010 - 12:06 AM
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#define SIZE 10
int getch(void);
void ungetch(int);
int main(void)
{
char c = getchar();
int x;
ungetch(c);
scanf("%d", &x);
printf("\n%d\n", x);
system("pause");
return 0;
}
Similarly, if I enter, say... 54321 and key return, shouldn't this put '5' in c, put 5 back into the buffer, and then put 54321 in x and print it?
It prints 4321. Could anybody explain this please.


Sign In
Create Account


Back to top









