Can someone explain to me

This is a discussion on Can someone explain to me within the C++ Programming forums, part of the General Programming Boards category; Hello, Im trying to understand chapter 4 of Accelerated C++. But one thing I can't understand. We try to read ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    Can someone explain to me

    Hello,

    Im trying to understand chapter 4 of Accelerated C++.

    But one thing I can't understand.

    We try to read the homework grades from input stream into a vector <double>

    istream& read_hw (istream& in, vector <double>& hw)

    If I understand it right read_hw is another name for istream
    And in is another name for istream.

    But what's istream here doing. I tought that cin reads data in.

    Can someone shine a light on this ?

    Roelof

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "read_hw" looks like a function prototype to me, it returns a reference to an istream, probably the same stream it takes in, because it's going to be used to read data from that stream. Lemme see if I can write you an example of that, but I have to finish something else first...

    Yes, cin is an istream.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by roelof View Post
    Hello,

    Im trying to understand chapter 4 of Accelerated C++.

    But one thing I can't understand.

    We try to read the homework grades from input stream into a vector <double>

    istream& read_hw (istream& in, vector <double>& hw)

    If I understand it right read_hw is another name for istream
    And in is another name for istream.

    But what's istream here doing. I tought that cin reads data in.

    Can someone shine a light on this ?

    Roelof
    Yeah, from what I understand cin is an istream, and when you call this function you will (in this instance) call as such: read_hw(cin, homework), with homework being the vector name.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    It's a function prototype. It defines a new function that takes two arguments:
    - An istream (input stream).
    - A vector of doubles.
    It will then read from the input stream and store the data in the double (hw).

    When you call this function, you pass either cin or some opened file as the argument for in.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    And hw_read is then the name of the new function ?

    Does this the same as :

    cin >> name
    homework.push_back (name) ;

    or do I understand your wrong ?

    Roelof


    Roelof

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by roelof View Post
    Oke,

    And hw_read is then the name of the new function ?

    Does this the same as :

    cin >> name
    homework.push_back (name) ;

    or do I understand your wrong ?

    Roelof


    Roelof
    You still need to write the code within the function for it to do something. Within that chapter, i'm sure it shows an example of what the function body should look like for the read_hw function.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by roelof View Post
    And hw_read is then the name of the new function ?
    Yes.

    It will do whatever you code it to do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    When I read further I saw the whole function.
    But why first prototype a function and right after that write the code in it ?

    Roelof

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    I believe the book was just showing you how the function would look like (ie what parameters it takes and what it returns).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by roelof View Post
    Oke,

    When I read further I saw the whole function.
    But why first prototype a function and right after that write the code in it ?

    Roelof
    That will actually become fairly standard once you reach classes. Mainly for readability, with classes (particularly for functions more than a few lines long), you will declare the functions within the class and then define them outside of the class.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    Then I will now read the part of how the data will change from the earlier chapter.
    Thanks everybody for the help and explanation.

    Roelof

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello

    I read now the whole chapter and saw that the prototype is used in the header file and the function itself in the main file.

    Now trying to make the exercises.

    Roelof

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please explain..
    By madmax2006 in forum C Programming
    Replies: 1
    Last Post: 10-30-2009, 11:33 PM
  2. One Easy" C " Question. Please Solve and Explain.
    By RahulDhanpat in forum C Programming
    Replies: 18
    Last Post: 03-24-2008, 01:39 PM
  3. Please explain?
    By neo_phyte in forum C Programming
    Replies: 3
    Last Post: 08-25-2006, 05:23 AM
  4. Replies: 4
    Last Post: 11-19-2002, 08:18 PM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM

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