Thread: Input/Output Streams

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    30

    Input/Output Streams

    Hello, Everyone:

    I am studying for my final exam on programming. I was checking from some previous exams that I got two input/output questions which I really don't understand how to figure out the answer.

    Here are the programs:

    Code:
    #include <iostream.h> 
    int main() 
    { 
    char line; 
    cin >> line;  // line 5 
    cout << line;  // line 6 
    return 0; 
    }
    The question is: If the input stream at line 5 is " CIS 101" (Note: " " represents blank space), The output at line 6 is:
    ??

    and

    Code:
    #include <iostream.h> 
    int main() 
    { 
    int charCount; 
    char next; 
    const char NWLN = '\n'; 
    charCount = 0; 
    while (next != NWLN) 
    { 
     cin.get(next);    // line 10 
        charCount++; 
        cout.put(next);    // line 12 
    } 
    cout << "The count is: " <<charCount;  // line 14 
    return 1; 
    }
    If the input at line 10 is "This is a test," what is the output at line 12?

    Referring to the previous program, what would be the output at line 14?

    Please someone help me how to interpret these question. I have a final comming up and really I don't understand these questions. I want to know to get the answer without using SSH.

    Thank you....

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    The extraction operator (>>) reads one non-spaced body of data at a time, so if the user typed " CIS 187" the space would be skipped and the first character would be extracted since "line" is a char variable. The istream::get() function similarly reads a single character, so if the user enters a string of characters each call of get() would return the next character in the input until it is exhausted.

    (I am aware that I am merely providing an explanation instead of answers to your questions, but the explanation should cause you to discover the answer within yourself. Inside yourself look, young Jedi.)

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    LuckY mentioned that >> skips any whitespace at the beginning, so it should be made clear that get() does not skip whitespace and will read in whatever character is there.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I want to know to get the answer without using SSH.
    Huh, what's SSH got to do with it?
    Oh, I get it - it's the secure shell gateway to your compiler.

    Just try stuff for yourself, it's really quite simple.

    Just so you know, I think LuckY's explanation of >> for a char variable is wrong. So simply reciting what someone told you on a message board without you actually verifying it yourself is just poor IMO.

    Also, there are at least 3 further problems with your 2nd program which haven't been mentioned yet.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I think LuckY's explanation of >> for a char variable is wrong.

    How is it wrong?

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by Salem
    Just so you know, I think LuckY's explanation of >> for a char variable is wrong. So simply reciting what someone told you on a message board without you actually verifying it yourself is just poor IMO.
    Just so you know, Salem's thought about my explanation is wrong. Perhaps you, Salem, should try verifying that someone is wrong before reciting your thoughts with such fervor.
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
      char ch;
      cin >> ch;
      cout << ch;
      return 0;
    }
    Input:
    Code:
     CIS 187
    Output:
    Code:
    C

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    30
    LuckY Thank you for your tips. These questions were not on the final exam, but anyway good explanation. I read it and now I understand it, even though is confusing reading it the first time.

    Good work!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to two streams: va_arg issues
    By dwks in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 10:14 AM
  2. input/output streams
    By cybernike in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2007, 11:15 PM
  3. input/output
    By JCK in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2002, 12:07 PM
  4. Independent streams of pseudo random number generator
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-15-2001, 05:32 AM
  5. File Streams, new error on me.
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 08:28 PM