473,486 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

dereferencing a passed reference to cin

3 New Member
Hi all,

I'm teaching myself C++ from C++ Primer 4th Ed., but I've not done any programming before and the damn thing has no answers in it!

If I pass a reference to cin to a function called 'test' like so:

string s;
cin >> s;
test(cin);

...test being defined as:

void test (istream &input)
{
// want to print text entered into 'cin' above.
}

...how do I "get at" the original stuff I typed in to 'cin' when I'm within the body of 'test'? 'getline' just gets me to type something new in and everything else I've tried just yields an address. Help!

Any tips anyone could give would be *massively* appreciated.

Tristan.
Jul 27 '06 #1
4 3120
D_C
293 Contributor
Change the definition of function test to:
void test(string input)

Then input is what you typed into your program, you don't have to do any other I/O commands.
Jul 27 '06 #2
Tristan
3 New Member
Hi,

Thanks for your reply. I don't think I made clear what I was looking for- I'm doing this excercise to try and understand how passed iostreams work. I know I can just pass 's' as an argument if I just want the body of the input, but I'm trying to understand how to pass a reference to cin (or another istream) and read what has been previously input into it while in the body of the function it's been passed to. Is this, in fact, possible?
Jul 28 '06 #3
vmohanaraj
14 New Member
Yes. This is possible.

There are some facts to consider though. The cin object reprsents the standard input as a stream. It extracts the characters from the input stream that we generate at the keyboard.

">>" is an extraction operator. As the name implies, if we apply this on cin it removes the read characters from cin and returns it. And when we apply this extraction operator to string(or char array), it extracts only one word at a time. So we use "get" and "getline" member functions to read one line.

In your example, before passing the reference to your test function, you extract the value. So we can not get that value again in the test method. Some members of cin like peek() reads the character from cin without extracting. But we can use that only for reading one character(the next character in the cin).

Having said this, the following code might be helpful to you. It reads a string of atmost 99 characters and prints that out. The additional one character in the array is used for the terminating character.

#include <iostream>
using namespace std;
void test (istream &input);

int main(){
cout << "Input the string: ";
test(cin);
return 0;
}

void test (istream &input)
{
char a[100];
cin.getline(s, 100);
cout << a;
}


In this code I have used getline to read a whole line. We can use the operator << in a loop but it skips over white spaces. Depending on the usage you can choose either of this.
Jul 28 '06 #4
Tristan
3 New Member
Awesome. That's great, thanks for your help.
Jul 29 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

51
3106
by: BigMan | last post by:
Does the C++ standard define what should happen in case of NULL pointer dereferencing. If not, does it say that it is illegal? Where, if so, does it say it?
1
2349
by: Vam | last post by:
dereferencing a pointer to base always provides a base-class slice even if the object pointed to was of a derived type, correct?
2
8833
by: Matthias Kaeppler | last post by:
Hello, I was wondering, does dereferencing past-the-end iterators yield undefined behavior? Especially, is the result of calling an STL algorithm on an empty range undefined? For example...
2
7200
by: Dennis Jones | last post by:
Hello, Given something like: boost::shared_ptr<T> t( new T() ); What is the best (correct?) way to dereference the pointer? The following two methods work. Is there a difference?
3
3393
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
24
2570
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
28
2400
by: Martin Jørgensen | last post by:
Hi, I have a "funny" question, which I think is pretty "healthy" to examine... This program is being investigated: - - - - - - - #include <iostream> using namespace std; #define DAYS 7
4
5216
by: Pritam | last post by:
line 7: error: dereferencing pointer to incomplete type 1. #include<stdio.h> 2. #include<sys/stat.h> 3. #include<stdlib.h> 4. void execname() { 5. struct task_struct *my; 6. my =...
1
1892
by: David Mathog | last post by:
We've established that a line of code like: (void) function( (long *) &(something) ); may (and probably should) generate one of these warning: dereferencing type-punned pointer will break...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.