The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags:

Closed Thread
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 06-10-2007, 04:13 PM
acorn
Posts: n/a
[SOLVED] i need help on a program on C++?

why is it that in the middle of my program ( too long to put here) it skips over a cin>> statement and gives it the result of 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!
  #2 (permalink)  
Old 06-10-2007, 04:14 PM
ancient_nerd
Posts: n/a
Is your arrow pointing the right direction. It is supposed to be

cin <<

__________________

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!
  #3 (permalink)  
Old 06-10-2007, 05:00 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
As ancient said check your arrows are facing the correct way, it is a mistake that eveyone seems to make from time to time.

__________________

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!
  #4 (permalink)  
Old 07-08-2007, 03:31 AM
Opiate
Posts: n/a
Quote:
Originally Posted by ancient_nerd View Post
Is your arrow pointing the right direction. It is supposed to be

cin <<
No
Cin streams input into a variable. Think of it as pointing to where you want the input to go.
Code:
cin >> variable1

__________________

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!
  #5 (permalink)  
Old 07-08-2007, 08:25 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
is it necessary to put it into a variable if you're using cin? Isn't that just mean it's going into the stream or something like that and then cout?

__________________

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!
  #6 (permalink)  
Old 07-08-2007, 09: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)
  #7 (permalink)  
Old 07-08-2007, 02:09 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
thank you very much for the thorough explanation. Very well appreciate with the examples. Help a lot! rep added

__________________

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!
Closed Thread


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:24 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50