Jump to content

Infinite prints

- - - - -

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

#1
skypower

skypower

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Hello, take a look at this C code:
while ((ch=getchar())!=EOF)
{
...random code....

ch=getchar();
}
I have included a printf in the code that prints the letter that getchar() gets. The problem is that I got that letter printed infinite times :mad:

Help please?

#2
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
can you please copy your whole program in here?
Then we'll see what we can do to help ^_^

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Why are you reading TWICE? It's only going to process every other letter.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
How to generate EOF from the keyboard depends on the operating system. MS-Windows is ^Z, while I think (but not certain) *nix is ^D. And since you are using getchar() the Enter key is also required.

#5
skypower

skypower

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts

WingedPanther said:

Why are you reading TWICE? It's only going to process every other letter.

My bad. Ok what the program does is that when I give 5g2f for example, I want gggggff for output. Here is the whole code:

#include <stdio.h>

int main()
{
    char ch;
    int k, i = 1;

    ch=getchar();

    while ((ch)!=EOF)
    {

        if ((ch>=0) && (ch<=9))

            if (i = 1)

                k = (int)ch ;

            else

                k = k * 10 + (int)ch ;

            i++ ;



        if ((ch>='a') && (ch<='z') || (ch>='A') && (ch<='Z'))
        {

            for (i=1; i++; i=k)

            {
                printf("%c", ch) ;
            }

            i = 1 ;
        }

        ch=getchar();
    }
}


#6
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
You still have two getchar() calls. You only need one of them
while ((ch = getchar())!=EOF)
{
   // blabla
}


#7
skypower

skypower

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts

Ancient Dragon said:

You still have two getchar() calls. You only need one of them
while ((ch = getchar())!=EOF)
{
   // blabla
}

Well yes, I think it's the same thing. Still printing infinite times though...

#8
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
you mean you can't get out of the loop? Or what exactly do you man?

  if ((ch>=0) && (ch<=9))
when you enter the digit 5 it is read by getchar() as the character '5', not the number 5. So the above check should be as follows: Note the single quote marks.
  if ((ch>='0') && (ch<='9'))


Here is that infinite loop; That loop will never stop because of the crazy things you are doing with variable i. This is sloppy coding at its best :)
            for (i=1; i++; i=k)


#9
skypower

skypower

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Well yes, it seems something is wrong with EOF...

As for getchar(), by changing I got the same result and to be honest I am confused now. I want it to compare the number not the character '5', what should I do? Is there something like getchar() for integers??

#10
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
When it gets a digit then convert it to an integer. To convert to an integer you have to subtract the letter '0' so that '5' - '0' is the same as 53 - 48 = 5. See this ascii chart for numeric values of characters.
if( isdigit(ch) )
{
   k = ch - '0';
}

Also, the data type of ch must be int, not char, because EOF may or may not fit in a char data type. getchar()is defined to return an int.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.