problem cant solve please help.

This is a discussion on problem cant solve please help. within the C Programming forums, part of the General Programming Boards category; write a program that reads in a sequence of lines of text and counts the number of lines entered, and ...

  1. #1
    sarah
    Guest

    Question problem cant solve please help.

    write a program that reads in a sequence of lines of text and counts the number of lines entered, and the number of alphabetic characters entered.

    Initialise linecount and alphacount to zero
    read a character
    while (character just read is not end of file indicator)
    if (character just read is equal to end-of-line character)
    increment the linecount
    else
    if (character just read is alphabetic character (ie. one of 'A'..'Z' or one of 'a'....'z'))
    increment alphacount
    read a character
    endwhile
    display linecount and alphacount


    I don't really understand what this question is asking me, could someone please give me some advice. thank you very much

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Geez, it's sitting there right in front of you!

    If you don't understand is it because your class is too hard?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi,
    after opening files...while(feof( )) end file.. you should read file character at a time....using fgetc...store character...if character alphabet....use if(isaplha)..if true add 1 to aplhabet count ....else
    if character ='\n'....add 1 to line count....end while...display result...close files....what more can I say....

    Okay then while(ch=fgetc(fp)!=EOF) instead of while(feof(fp))
    Last edited by bigtamscot; 09-03-2001 at 05:58 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    You really should try and acoid using feof, its not a very stable way of reading in lines of text even though it appears it does.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > Okay then while(ch=fgetc(fp)!=EOF)
    1. ch must be declared as an int.
    2. its actually while( (ch=fgetc(fp))!=EOF ) (read the precedence table)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Why is "feof()" unstable? I use it a lot and need to know!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > Why is "feof()" unstable? I use it a lot and need to know!
    It's not unstable, it's just not useful.

    The main problem is that feof() only returns true, when some file reading operation (eg. fgetc) has already returned true.

    So you end up having to do an end-of-file check in two places
    Code:
    while ( !feof(fp) ) {
       int ch = fgetc(fp);
       if ( ch != EOF ) {
          // do stuff with ch
       }
    }
    Since you can combine the assignment and test into one operation, it's much easier to write
    Code:
    while( (ch=fgetc(fp)) != EOF ) {
      // do stuff with ch
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solve This Problem Within 3 Hr Urgent Need
    By annum in forum C Programming
    Replies: 12
    Last Post: 10-04-2009, 10:56 AM
  2. Problem I can't seem to solve
    By psykik in forum C Programming
    Replies: 15
    Last Post: 05-19-2005, 01:18 PM
  3. problem solve
    By coolnarugodas in forum C Programming
    Replies: 7
    Last Post: 04-26-2005, 01:31 PM
  4. Replies: 2
    Last Post: 04-25-2005, 12:59 PM
  5. Bubble Sort / selection problem
    By Drew in forum C++ Programming
    Replies: 7
    Last Post: 08-26-2003, 04:23 PM

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