Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using cout for string

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

885 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