Solved

Using cout for string

Posted on 2013-05-19
3
287 Views
Last Modified: 2013-12-18
I have a question about the following simple c++ program

#include <iostream>

using namespace std;

int main()
{
	while (cin >> s)
	{
		cout << s << endl;
	}
	return 0;
}	

Open in new window

This program just reads text into a string from the standard input, then outputs it to the screen via std::cout. My question if one enters lots of separate words separated by spaces and terminated by EOF, the program will output each word on a separate line. Why does this happen?
0
Comment
Question by:Rothbard
3 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 39178508
Another thing that is happening in your program is that you are using cin to read from STDIN.  This will read one word at a time not one line.

If you want to read one line you have to use

std::getline(std::cin, s);
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 50 total points
ID: 39178582
From http://www.cplusplus.com/doc/tutorial/basic_io/:

However, as it has been said, cin extraction stops reading as soon as if finds any blank space character

The combination of the while loop and the cout statement with an endl causes each word to be printed on a new line.

Also, where did you declare "s"?
0
 
LVL 33

Accepted Solution

by:
sarabande earned 50 total points
ID: 39185343
using std::getline for read has another advantage: your input at the console and the output from std::cout << were separated by a linefeed and show in separate lines.

in any case, your code should provide code to avoid an infinite loop:
while (std::getline(std::cin, s))
{
     if (s.length() == 1 && s.find_first_of("qQ") == 0)
          break;

Open in new window

if you need to parse the line after read, you might consider to using a std::istringstream which you can "feed" with the input line and then use the istringstream instead of std::cin to get item by item from input stream.

Sara
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question