Solved

Using cout for string

Posted on 2013-05-19
3
278 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 74

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 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now