473,395 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

string with spaces

I want to enter sentences with blanks between words,
names, cities, new york, pam anderson, etc.
as in:
Input"enter your name";name$
print"your name is: ";name$
C++ seems to make it rather difficult to do a simple thing as that.

#include<string>
using namespace std;

struct Cities {
string city;
int temp;
};

Cities info;

cout<<"\nplease enter "<<info[t].city<<"'s temp: ";cin>>info[t].temp;

if user enters 'new york' program crashes. why cant there be
a space when entering a string?

it works with cin.get(), but in a while loop the input is blank
after first name is entered.

this works:
#include <iostream.h>

void main( void ) {

char ch;

cout << "Enter a string:" << endl;
while( ( ch = cin.get() ) != '\n' ) {
cout<<ch;
}
cout << endl;
}

but how do i use it in a array, class or a struct?

--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
Jul 19 '05 #1
8 41244
"Developwebsites" <de*************@aol.combatSPAM> wrote in message
news:20***************************@mb-m04.aol.com...
I want to enter sentences with blanks between words,
names, cities, new york, pam anderson, etc.
as in:
Input"enter your name";name$
print"your name is: ";name$
C++ seems to make it rather difficult
Easy. You just need to find the proper tool.
to do a simple thing as that.

#include<string>
using namespace std;

struct Cities {
string city;
int temp;
};

Cities info;

cout<<"\nplease enter "<<info[t].city<<"'s temp: ";cin>>info[t].temp;

if user enters 'new york' program crashes.
I just don't believe that, because this will not compile.
'info' is not an array or a type which supports the []
operator.
why cant there be
a space when entering a string?
There can. But the istream extractors automatically
skip over whitespace by definition. They don't do
what you need.

You're using the wrong tool. Try:

std::getline(std::cin, info.city);

This (nonmember) function reads all characters up
to and including a newline character (this is a default
parameter you can change if you like). The newline
character is discarded, not stored in the input.

it works with cin.get(), but in a while loop the input is blank
after first name is entered.

this works:
#include <iostream.h>
#include <iostream>

void main( void ) {
int main(void) {

char ch;

cout << "Enter a string:" << endl;
while( ( ch = cin.get() ) != '\n' ) {
You forgot to check for EOF.
cout<<ch;
}
cout << endl;
}

but how do i use it in a array, class or a struct?


Don't. If you want to retrieve and store a whole
line of input in a string object, use the 'std::getline'
function I showed above. It doesn't matter where the
string object is stored, you just need to pass its
name to 'std::getline()'

-Mike
Jul 19 '05 #2

"Developwebsites" <de*************@aol.combatSPAM> wrote in message
news:20***************************@mb-m04.aol.com...
I want to enter sentences with blanks between words,
names, cities, new york, pam anderson, etc.
as in:
Input"enter your name";name$
print"your name is: ";name$
C++ seems to make it rather difficult to do a simple thing as that.

#include<string>
using namespace std;

struct Cities {
string city;
int temp;
};

Cities info;

cout<<"\nplease enter "<<info[t].city<<"'s temp: ";cin>>info[t].temp;

if user enters 'new york' program crashes.


What exactly is info[t]? That doesn't make sense. There's something going
on here you're not telling us.
Jul 19 '05 #3

"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"Developwebsites" <de*************@aol.combatSPAM> wrote in message
news:20***************************@mb-m04.aol.com...
I want to enter sentences with blanks between words,
names, cities, new york, pam anderson, etc.
as in:
Input"enter your name";name$
print"your name is: ";name$
C++ seems to make it rather difficult to do a simple thing as that.

#include<string>
using namespace std;

struct Cities {
string city;
int temp;
};

Cities info;

cout<<"\nplease enter "<<info[t].city<<"'s temp: ";cin>>info[t].temp;

if user enters 'new york' program crashes.
What exactly is info[t]? That doesn't make sense. There's something

going on here you're not telling us.


Common novice behavior here: Rather than post the real
code, they try to post ad-hoc fragments in an attempt
to 'describe' the real code. Almost always these fragments
are lifted from the real code 'as is', and losing context,
they become invalid.

"Developwebsites", are you listening? :-)
If you have code you need help with, post that code
verbatim, don't try to 'describe' it with fragments.

-Mike
Jul 19 '05 #4
In article <20***************************@mb-m04.aol.com>,
de*************@aol.combatSPAM says...
I want to enter sentences with blanks between words,
names, cities, new york, pam anderson, etc.
as in:
Input"enter your name";name$
print"your name is: ";name$
C++ seems to make it rather difficult to do a simple thing as that.


Not particularly.

#include <iostream>

std::string name;

std::cout << "enter your name";
std::getline(std::cin, string, "\n");

std::cout << "Your name is: " << name << std::endl;

I'm afraid, the rest of your code looked like rather a mess to me, so I
ignored it.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #5

"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:MP***********************@news.clspco.adelphi a.net...
In article <20***************************@mb-m04.aol.com>,
de*************@aol.combatSPAM says...
I want to enter sentences with blanks between words,
names, cities, new york, pam anderson, etc.
as in:
Input"enter your name";name$
print"your name is: ";name$
C++ seems to make it rather difficult to do a simple thing as that.
Not particularly.

#include <iostream>


#include <string>
std::string name;

std::cout << "enter your name";
std::getline(std::cin, string, "\n");


Typo:

std::getline(std::cin, string, '\n');

Third argument is type char, not char*

Note to OP: You can call instead the overload
of std::getline which defaults to using a
delimiter of '\n':

std::getline(std::cin, string);

-Mike

Jul 19 '05 #6
In article <IE*****************@newsread3.news.pas.earthlink. net>,
mk******@mkwahler.net says...

[ ... ]
#include <string>


Hmmm...you mean I should have string defined before I use it? Either
you're being picky, or I'm up past my bedtime. :-)

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #7
"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:MP************************@news.clspco.adelph ia.net...
In article <IE*****************@newsread3.news.pas.earthlink. net>,
mk******@mkwahler.net says...

[ ... ]
#include <string>
Hmmm...you mean I should have string defined before I use it?


Even my cat knows that. Witness all the mangled
characters littering my patio.

Practicing string operations without a proper
#include directive? You will be cited and must
pay define, or we'll have to string you up.
Either
you're being picky,
Of course. And I've picked you as my victim this time.
or I'm up past my bedtime. :-)


What's a bedtime? What's a bed?

I know about Pacific time, Mountain time, Central time,
Eastern time, and Atlantic time, but Bed time? The time
in the Negev desert where the Bedouins hang out?

Oh, a bed, that thing on the back part of my pickup truck?

Sorry, I'm in a mood today. :-)

-Mike
Jul 19 '05 #8
>Not particularly.

#include <iostream>

std::string name;

std::cout << "enter your name";
std::getline(std::cin, string, "\n");

std::cout << "Your name is: " << name << std::endl;


obviously, the above is far more clearer and simpler than:
Input"enter your name";name$
print"your name is: ";name$

--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
Jul 19 '05 #9

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

Similar topics

9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
16
by: Steve | last post by:
Hi Guys, I have a string which contains data elements separated by spaces. I also have a function which returns the number of characters from the beginning of the string for a given number of...
18
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative...
3
by: Uttam | last post by:
Hello, Using ADO I have created a table and have also created fields. To create fields, I have used the following: ..Columns.Append "Field_Name", adWChar, 6 I load records into this...
20
by: Chris LaJoie | last post by:
I'm looking for some kind of simple string compression code I can use. I'm not looking for SharpZipLib. Their implimentation spans several classes and is very complex. I'm just looking for...
1
by: Anonieko Ramos | last post by:
> > > How to display multiple spaces in a dropdownlist webform1.aspx <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
2
by: akoymakoy | last post by:
is there a function to remove leading and trailing spaces on strings? example: word = " THE QUICK BROWN FOX " output: word = "THE QUICK BROWN FOX"
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
3
by: Elohim | last post by:
Hi to everybody, I'm a learner of C++. I'll really appreciate if you can help me or teach me something. Thank you in advance. #include<iostream> #include<string> int main() { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.