looking for help on looping

This is a discussion on looking for help on looping within the C Programming forums, part of the General Programming Boards category; Hi, i was looking for a sample code for reading in a word by looping scanf and so on, and ...

  1. #1
    BNG
    BNG is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    looking for help on looping

    Hi, i was looking for a sample code for reading in a word by looping scanf and so on, and be able to print it out again. i'm actually trying to write a program to count words, syllabals and lines in text, but i'm just starting slow. thx.
    Last edited by BNG; 02-25-2005 at 07:22 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    this bit of code reads a text and stores in a string and prints back again

    Code:
    #include<stdio.h>
    
    int main()
    {
            char str[30],c;
            int i=0;
            
            do
            {
                c=getchar();
                if(i<=30)
                str[i++]=c;
        }
        while(c!=EOF);   //in windows its ctl+Z == EOF
        str[i]='\0';
          printf("%s",str);
           
               getchar();
    }

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by ssharish
    this bit of code reads a text and stores in a string and prints back again

    Code:
    #include<stdio.h>
    
    int main()
    {
            char str[30],c;
            int i=0;
            
            do
            {
                c=getchar();
                if(i<=30)
                str[i++]=c;
        }
        while(c!=EOF);   //in windows its ctl+Z == EOF
        str[i]='\0';
          printf("%s",str);
           
               getchar();
    }
    I have noticed before that you assign the return value of getchar() to a type char variable. You should not do this. getchar() returns an int.

    ~/

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    you are right kermit . sorry about that code

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The reason you should use an int here is because you're testing for EOF. If you weren't, the assignment would be perfectly fine. However, EOF isn't a value that will fit in a char, so if it occurs and you're assigning it to a char, it will be changed to a different value so that it can fit. This in effect breaks your code, preventing it to work properly.

    If however, you don't need to check for EOF, the assignment would be fine. However, since you are in fact testing it for EOF, the assignment is incorrect. Just to clarify the reason it's wrong in this case.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 07:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 02:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 12:01 AM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21