Jump to content

Why isn't this working?

- - - - -

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

#1
Osnarf

Osnarf

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
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.

#2
Osnarf

Osnarf

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Okay this is really wierd.

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
Osnarf

Osnarf

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
#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.