473,890 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with simple cin problem please.

So it's been a while since I've done anything in C++, but I thought I
could grab

cin >> input;

and it would go into string input, even if what's coming in happens to
be 3 words separated by spaces. I don't want to use c strings, I want
to use std::string.

So imagine the code

#include <iostream>

int main()
{
string input;

while(true)
{
cout << "Enter three words: ";
cin >> input;
cout << input;
}
}
So you would enter 3 words on the command line, but only one is spit
back at you because only the first one is stored in the string. Then
the loop continues and it actually picks up the next value for the
second loop, and so on.

What I want to happen is for input to store one string with three
words, etc.

Can someone help?

Shane
Jul 22 '05 #1
3 1248
On 7 Jul 2004 17:08:21 -0700, de**********@za ft.com (Shane) wrote:

[...]

So you would enter 3 words on the command line, but only one is spit
back at you because only the first one is stored in the string. Then
the loop continues and it actually picks up the next value for the
second loop, and so on.
And it should.
What I want to happen is for input to store one string with three
words, etc.

Can someone help?

Have a look at getline

std::string input;
std::getline(st d::cin, input);
std::cout << input << std::endl;

or
copy(istream_it erator<string>( cin),
istream_iterato r<string>(),
ostream_iterato r<string>(cout , " "));
Mark
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
Jul 22 '05 #2

"Shane" <de**********@z aft.com> wrote in message
news:d0******** *************** ***@posting.goo gle.com...
So it's been a while since I've done anything in C++, but I thought I
could grab

cin >> input;

and it would go into string input, even if what's coming in happens to
be 3 words separated by spaces. I don't want to use c strings, I want
to use std::string.

So imagine the code

#include <iostream>

int main()
{
string input;

while(true)
{
cout << "Enter three words: ";
cin >> input;
cout << input;
}
}
So you would enter 3 words on the command line, but only one is spit
back at you because only the first one is stored in the string. Then
the loop continues and it actually picks up the next value for the
second loop, and so on.

What I want to happen is for input to store one string with three
words, etc.

Can someone help?

Shane


Instead of using cin, you might consider using getline:

getline(cin, input, ' ');

Note: This will only accept words separated by a space. If there is a
newline in there, it'll be lumped in with the word, so be careful.

-- Matt
Jul 22 '05 #3
Mark <ma**********@h otmail.com> wrote in message news:<ck******* *************** **********@4ax. com>...
On 7 Jul 2004 17:08:21 -0700, de**********@za ft.com (Shane) wrote:

[...]

So you would enter 3 words on the command line, but only one is spit
back at you because only the first one is stored in the string. Then
the loop continues and it actually picks up the next value for the
second loop, and so on.

And it should.
What I want to happen is for input to store one string with three
words, etc.

Can someone help?

Have a look at getline

std::string input;
std::getline(st d::cin, input);
std::cout << input << std::endl;

or
copy(istream_it erator<string>( cin),
istream_iterato r<string>(),
ostream_iterato r<string>(cout , " "));
Mark

Thanks for the point in the right direction. That's exactly what I needed.

Shane
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2331
by: Lodewijk van Haringhal | last post by:
I'am new with javascritping not with programming. Is there nobody who can help me with ths simple promblem? :) Please, please give me a hint. Please help me with this script. I have two lists in my form. One to choose a directory and one to choose a picture. The script bellow works perfect without the directory choose function. I tried to make the directory choose function myself but I did not succeed. I think I made a mistake here:...
6
2773
by: max reason | last post by:
A method in one of my classes needs to call one of 256 other methods in the same class based on an unsigned 8-bit value (0x00 to 0xFF). How is this done? Everything I try generates errors. Something this basic can't be so difficult (or impossible) - can it? Thanks. The following roughly shows what I want: class simple { public:
12
2149
by: Christo | last post by:
borland c++ 5.01 character constant must be one or two characters long get this when compiling my first c++ program can anyone out there help? it is highlighting this line as the problem cout << "Please Enter First number: ";
6
2171
by: francisco lopez | last post by:
ok , first of all sorry if my english is not so good, I do my best. here is my problem: I don´t know much javascript so I wrote a very simple one to validate a form I have on my webpage. could you please have a look at the following script: ------------------------------------------------------------
13
2161
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times will fall somewhere between a Start & End time Further more, there will be Break & Lunch times between Start & End. Example... Start 08:00 Break start 10:30
11
1709
by: samuelberthelot | last post by:
Hi, I've got 3 input HTML (dropdown lists) on my page. One for selecting a Month, one for the day, one for the year. Very simple... My problem is that I'd like to update the Days one according to what month was selected (31,30 or 28 days). I should use a simple javascript to populate the input boxes, but I'm a bit new to javascript. Please can you help me ?
2
2585
by: Ken Crismon | last post by:
Hello, I am currently working on an embedded systems project where by I have written a small web server that is being hosted on our internet appliance (running on an Atmega128 chip and doing lots of col RFID and Mesh network stuff). In this example I am trying to get the AuthenticateUser web service to work. I am using the following WSDL:
0
5585
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
3
1352
by: zapras | last post by:
I need some help pretty please? :confused: Im very new to vba access and been trying to work out this problem for a couple of hours. It's probably simple for other people to see the problem though. (I copied it from vba in access for dummies book) This is my code.. DoCmd.openform "form2", acNormal, , , acFormAdd Forms!!ID.Value = Me!ID.Value DoCmd.Close acForm, "form3", acSaveYes I have this in a click event on one form called form3. Im...
9
7368
by: Chris Ahmsi | last post by:
I have been tasked to create a 'simple' form in Access providing managers to input necessary changes. I have 2 command buttons on the form and a check box. Command button 1 updates my table for multiple entries, and command button 2 e-mails the table in HTML format to my team, appends the data entered to a back-up table, and deletes the entries on the current table. When the check box on my form is checked (indicating a permanent change) I...
0
9980
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10926
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5856
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6064
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4683
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.