end of file i need help

This is a discussion on end of file i need help within the C++ Programming forums, part of the General Programming Boards category; well how would EOF work in this code? #include <iostream.h> int main() { char ch; int count = 0; cin.get(ch); ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    end of file i need help

    well how would EOF work in this code?



    #include <iostream.h>
    int main()
    {
    char ch;
    int count = 0;
    cin.get(ch);
    while (cin.fail() == false)
    {
    cout<<ch;
    count++;
    cin.get(ch);
    }
    cout<<"\n"<<count<<"characters read\n";
    return 0;

    }

    how does taht get the output of

    u type in anything u want
    it repeats itself once
    keeps this apttern up till u hit ctrl+z

    how does that work
    hooch

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    first of all EOF is a compoenent of <fstream> not <iostream> it symbolizes end of file when you for example read from an existing file...

    in this particular example, you are using a so called "infinite loop" because in this situation cin.fail() will not be "true" as long as you will supply your input...so obviously you have to abnormally terminate the program by (ctrl and c) ...

    seems like you are trying to do something else in this program... if you need me to put you on the right track i would need more info...

    hope this helps....

    Regards,
    matheo917

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    well exactly my ? is that how does the program no to terminate after ctrl+z
    hooch

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i am sorry, i don't think i really understood your last post...
    please refraze it...i am not sure whether you are asking me what is EOF and how it used in C++ ??? or you are asking me what is going on with this code that you wrote and why doesn't you program end???
    i would be glad to help you but u gotta give me more than that

    Regards,
    matheo917

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    i mean like when im doing the program and its doing its loop of asking for input how does the program know to end when i press ctrl+z
    hooch

  6. #6
    Registered User argon's Avatar
    Join Date
    Jan 2002
    Posts
    12

    Question

    Could it be that ctrl+z makes cin.fail() = 0?

    Remember that when you type those type of key combinations you normally are trying to quit the program execution, but since you are expecting it, the program just breaks the loop and continue its normal flow.
    I'm not sure that is a correct answer, but is my best guess. Please correct me if I'm wrong.
    Greetings from México

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    hmm well i did some playing around with a typical piece of code here it is anyway:

    #include <iostream.h>
    #include <math.h>
    int main()
    {
    double x = pow(2,2);
    cout<<x<<endl;
    int hey;
    cin>>hey;
    cout<<"\n";
    return 0;
    }


    and that obviously as u can doesnt have a cin.fail() in it and ctrl+z terminated the program before it finished executing and other ctrl+v or whatever combinations did itneresting things so with taht in mind what does cin.fail() do with the original code on my very first post? since to me it seems to do nuthing?
    hooch

  8. #8
    Registered User JTtheCPPgod's Avatar
    Join Date
    Dec 2001
    Posts
    44
    Well, ctrl+z is not your problem, nor is the cin.fail. Really the reason it goes into an infinite loop is cos the program does absolutely nothing. If you think throught the code (at least if I'm thinking through it correctly) it does simply nothing. ch is something for files (the current character being read, if I'm not mistaken... I dunno, I havent used files in awhile) and so is eof. Now if you wanna calm down and tell us what the program is supposed to do instead of babbling on about ctrl+z and getting mad at people that are trying to help, we might just be able to fix your code.
    "No! I must have my delicious cupcakes, my sweet cakey treasures, piping hot from their 40 watt WOMB!!!"
    --Captain Murphy

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    im calm and not getting upset and well all i was asking about ctrl+z cuz i was wondering how the program knew to terminate when i pressed that but i found out sorta so now all im wondering is how does cin.fail() fit into all this because my book is teaching that but i dunt understand how cin.fail() fits into the code
    hooch

  10. #10
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    ok....it seems like you are looking for some history on computers...
    now....your question is jumping into implementation of Microsoft Disc Operating System and into each particular compiler one might use.... the truth is, what you write now are DOS console programs, that appear in a Dos mode black screen (if we want to be simple) and literaly whatever program you run in DOS you can terminate by pressing your CTRL + Z like you said, this is an aspect of an operating system, this might not necessarily work for Windows based programs ...
    if you ask as to explain in detail how does it happen that the "program" knows when to terminate --> there's 2 answers for you

    1. first of all, it's not the "program" that knows when to terminate it's the operating system,

    2. second of all, i don't think anyone here has time and enough patience to explain to you the inner workings of an operating system or a compiler....hint: think of it this way....imagine you write a program, let's call it an "operating system" that everyone uses when they turn the computer on, imagine you wrote your own code in C++ to implement it, and imagine you have one "if" statement saying that --- if at any time during the programs execution the user presses CTRL + Z your code (operating system) will terminate the current application....

    hope that helps....

    now... to answer your question on cin.fail() ... i assume you don't know "classes" and the meaning about OOP object oriented programming, so that might be tough to explain...but i'll give it my best..
    here it comes...
    obviously you know that "cin" is used for input ... (computer asks for a number and you give it to him) :-)
    "cin" is also capable of performing tasks, for instance... "fail()" is a function that checks whether or not the input given by the user keeps continueing "cin.fail()"...this function returns one out of two (2) VALUES either 0 or 1 (false or true) respectively...every time your loop "loops" around it checks the condition of this function, if this function returns 1 (true) it keeps iterating if returns 0 (false) it exist the loops and continues on with what ever code is underneath it...
    ....if for some reason you might redirect processor's attention into doing something else than taking input then this function might return 1 (true), meaning --- CIN.FAIL() returns 0, WHICH MEANS THAT "CIN" ACTUALLY FAILED AND INPUT IS NO LONGER SUPPLIED TO COMPUTER....

    once again, i hope this helped... i really tried to keep as simple (really) as possible

    good luck,

    Regards,
    matheo917

  11. #11
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    sorry a little mistake,,, the last sentence of my last post should not have "return 0" it only should have "return 1" meaning cin.fail() this function returns "true", which means the input (cin) actually failed , so function "fail()" agrees and returns to and says "O.K." to fail....

    Regards,
    mathe917

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Ctrl-Z produces EOF, which is then read by cin. When an istream reads EOF it sets its fail flag to true, hence cin.fail() returns true and your loop stops. I really don't know why people have had so much trouble explaining this.
    - lmov

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    So cin.fail( ) and ctrl+z relate because cin.fail( ) in the while statement its being told while cin.fail( ) ==false to continue the loop but if it equals true(ctrl+Z) the program terminates correct?
    hooch

  14. #14
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    i think i have everything figured out except that i dun understand how the varaiable count counts the number of characters?
    hooch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  5. checking for end of file
    By linucksrox in forum C Programming
    Replies: 7
    Last Post: 06-01-2004, 01:41 AM

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