Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using cout for string

Posted on 2013-05-19
3
Medium Priority
?
309 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 200 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 35

Accepted Solution

by:
sarabande earned 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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.

722 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