View Single Post
  #6 (permalink)  
Old 07-08-2007, 10:38 AM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by HelloWorld View Post
is it necessary to put it into a variable if you're using cin?
The whole idea behind cin is data input. Whatever is coming from cin goes into a variable.
Unlike Java, reading characters and lines and using that input can only be done after creating memory for the data to reside in. Java does that automatically for you in that it gives you a readLine() function (assuming you are using some form of java.io.BufferedReader). readLine() returns a java.lang.String, so you can automatically use it. I believe System.Console.ReadLine() automatically returns a string as well in C#.

However, what about just read() in Java? If you use read() with no arguments, it reads a single character. Other options require you to allocate storage for them, just like C and C++. The same is true in C# with the System.Console.Read() method, except it only reads the next character, unless you do things on the fly. In fact, Microsoft actually recommends the ReadLine() method right on the documentation page for System.Console.Read() at MSDN.

Quote:
Originally Posted by HelloWorld View Post
Isn't that just mean it's going into the stream or something like that and then cout?
No, the data you input goes into the stream, which is then stored in memory at the address that the variable was allocated at. The reason for the way the arrows are facing:
inputstream >> storage_variable
outputstream << whatever

Whatever direction the arrows are pointing, that is the object that the data is going into. You can put anything into an outputstream (almost) and you can store anything from an inputstream. With data input, you should always check to see if there is any characters remaining and if you can still read from the inputstream (in Java, I believe it is required to use try blocks to even read input because of the possible danger to your program that can result from lack of good input skills). If there are any characters remaining and you simply wish to discard them, use cin.sync(). If you need to check whether a stream is still good, use cin.good(). Following is a simple input routine to add two numbers. If the first number is not valid, the failbit is set. As a result, the states of the failbit, badbit and eofbit must be cleared to make the stream "good" again. Of course, you don't want that data that is stored in cin's buffer still, right? That's why you call cin.sync() to discard it. Also note that if any of the bits are set (failbit, badbit or eofbit), no data is stored into the variable.
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main () {
    int v1 = 0, v2 = 0;

    cout << "Enter a number: ";
    cin >> v1; // suppose you entered y4 instead of 64...

    if (!cin.good()) { // if any of bits are set, the stream isn't "good"
        cin.clear(); // clear the state bits
        cin.sync(); // discard whatever is in cin's buffer
    }

    cout << "Enter another number: ";
    cin >> v2;

    cout << "The sum is " << (v1 + v2) << endl;

    return 0;
}
A simple example, but it illustrates the idea. Note that other things can occur such as a user entering "14 20". The 20 is still in cin's buffer and only the 14 is put into v1. That means that the next cin statement will not block input and it will instead put 20 into v2. However, none of the state bits are set. What do you do about it? You have a couple of choices. You can let it do that (bad choice almost always), you can clear the buffer and let the user wonder what happened, or you can tell the user that the input was invalid and that whatever was after v1 was discarded (you would output the actual value of v1 of course). For example:
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::cerr;
using std::endl;

int main () {
    int v1 = 42, v2 = 0;

    cout << "Enter a number: ";
    cin >> v1;

    if (!cin.good()) {
        cin.clear(); // clear the state bits
        cin.sync(); // flush the input buffer
    }
    else if (cin.peek() != '\n') {
        cerr << "Sorry, but anything after " << v1
            << " was discarded due to invalid input." << endl;
        cin.sync();
    }

    cout << "Enter another number: ";
    cin >> v2;
    cout << "The sum is " << (v1 + v2) << endl;
    return 0;
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-08-2007)