Solved

Using cout for string

Posted on 2013-05-19
3
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

729 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