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: , , , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-19-2007, 12:50 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Icon9 Question about COUT

Ok, I understand on how it works, but once again, I want to know what's really behind it! Here's the code that I used for the exercise from the Deitel book series (I love their book!)

PHP Code:
#include "stdafx.h"
#include <iostream>
int main() {
int n1n2n3;
std::cout << "Input three integers: ";
std::cin >> n1;
std::cin >> n2;
std::cin >> n3;
std::cout << n1;
std::cout << n2;
std::cout << n3;
system("pause");

It seems like cout stores all of of the input value from the command line into arrays? (Is this true?)

__________________
PHP Code:
System.out.println("Hello World!"); 

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!
Reply With Quote
  #2 (permalink)  
Old 07-19-2007, 05:34 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
I'm not sure how you came to that conclusion based on that program.

exactly how cin and cout works depends on which implementation of the STL you're using, and on the platform you're writing your program for - the C++ standard doesn't specify if or how any data is stored. before its transferred from the standard input device (which may have its own buffer - this is the case with keyboard input on Windows machines, but other platforms it may not be).

Just to clarify, the standard input device could be anything. On your computer, it might be the keyboard buffer - but it could be, for example, a mobile phone keypad, or a network connection, depending what sort of device you're programming for. Its up to the O/S (Which might be windows/linux, or may be firmware stored on a flash ROM chip) to handle the standard input device.

Understanding what goes on under the hood of cout and i/o streams isn't really very important - What is more important, with regards to streams, is that you can rely on stream objects to all behave in a consistant manner. That is to say, if you write a bit of code using cin/cout, you could replicate that same bit of code almost exactly with a stringstream (usually for string formatting or string parsing), or an fstream (file access).

The point is that the family of 'stream' libraries in C++ are designed to behave the same regardless of what input or output devices or buffers they may be attached to

__________________

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!

Last edited by Bench : 07-19-2007 at 05:47 AM.
Reply With Quote
  #3 (permalink)  
Old 07-19-2007, 07:49 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
Like Bench, I'm not really sure how you arrived at the conclusion that you did. Could you explain what you mean by "input value" as well as what led you to that conclusion? I think I know what you mean by "input value" since you mention the command line, and you aren't working with the command line in terms of getting command line arguments and using them. However, it would be beneficial for you to explain a little bit more since it seems to be unclear for both Bench and me, which means that it could also be unclear for future readers that might have a similar idea.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
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!
Reply With Quote
  #4 (permalink)  
Old 07-19-2007, 09:42 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Quote:
I'm not sure how you came to that conclusion based on that program.
I came up with that conclusion based on the test that I made. As all of you may see that there's only one cout statement, however, there are many CIN, and somehow, my input that is seperated by spaces are automatically tokenized and stored to each of the variables by using CIN.

__________________
PHP Code:
System.out.println("Hello World!"); 

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!

Last edited by HelloWorld : 07-19-2007 at 10:08 AM. Reason: Making it clearer
Reply With Quote
  #5 (permalink)  
Old 07-19-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
If you are referring to something such as:
Code:
int n1 = 1, n2 = 2, n3 = 4; //unimaginative... I know :p
cout << "Enter three integers: ";
cin >> n1 >> n2 >> n3;
The answer is rather simple. What happens is it reads the value into a buffer and then tries to store that value in the first variable after it. I say "tries" because if you enter something like "1a", it will only store the 1 and then will set the badbit (or maybe failbit, I can't remember) because 'a' is invalid for int. This will prevent any other things from being read improperly (an advantage of C++ over C). Supposing that first value was a proper value, it would then try to read the next value. The idea is that with streams like cout and cin (basic_ostream& and basic_istream&, I believe is what they are actually), the order of operations is that the variable following cout/cin is outputted/inputted first. From there, it reads the next one, etc. The idea illustrated:
Code:
cout << "Hello World!" << endl;
(cout << "Hello World!") << endl; //same output as above
cout << ("Hello World!" << endl); //error because you are trying to treat the const char array/pointer with the value "Hello World!" as an output stream
Obviously, a character array that has a constant value/length defined cannot be written to as that would cause some severe memory issues. For example, endl is more than just a '\n' character. Yes, it often simply suits that purpose, but it also flushes the output buffer if the output stream is buffered (writes to the output stream whatever unwritten characters are left in the output buffer).

The same idea is true of the example with cin above:
Code:
cin >> n1 >> n2 >> n3;
((cin >> n1) >> n2) >> n3; //same as above
cin >> (n1 >> (n2 >> n3)); //error because you are trying to treat n2 as an input stream

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
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!
Reply With Quote
  #6 (permalink)  
Old 07-20-2007, 12:19 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
I also just found this out:

Quote:
When cin is used with the stream extraction operator, it reads characters until the first white-space character is reached.
from Deitel lol...

__________________
PHP Code:
System.out.println("Hello World!"); 

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!
Reply With Quote
  #7 (permalink)  
Old 07-20-2007, 12:59 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
That is true. Be aware that white-space characters include tabs, spaces and newlines. You don't necessarily need to use a space, in other words.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
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!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-20-2007)
Reply


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 07:04 AM. 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