C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

string with spaces

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Developwebsites
Guest





PostPosted: Fri Sep 26, 2003 6:09 pm    Post subject: string with spaces Reply with quote



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].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< }
cout << endl;
}

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

--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
Back to top
Mike Wahler
Guest





PostPosted: Fri Sep 26, 2003 7:02 pm    Post subject: Re: string with spaces Reply with quote



"Developwebsites" <developwebsites (AT) aol (DOT) combatSPAM> wrote

Quote:
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.

Quote:
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].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.

Quote:
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.

Quote:

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
Quote:

void main( void ) {

int main(void) {

Quote:

char ch;

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

You forgot to check for EOF.

Quote:
cout< }
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



Back to top
jeffc
Guest





PostPosted: Fri Sep 26, 2003 8:00 pm    Post subject: Re: string with spaces Reply with quote




"Developwebsites" <developwebsites (AT) aol (DOT) combatSPAM> wrote

Quote:
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].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.



Back to top
Mike Wahler
Guest





PostPosted: Fri Sep 26, 2003 8:43 pm    Post subject: Re: string with spaces Reply with quote


"jeffc" <nobody (AT) nowhere (DOT) com> wrote

Quote:

"Developwebsites" <developwebsites (AT) aol (DOT) combatSPAM> wrote in message
news:20030926140941.15300.00000278 (AT) mb-m04 (DOT) 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].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? Smile
If you have code you need help with, post that code
verbatim, don't try to 'describe' it with fragments.

-Mike



Back to top
Jerry Coffin
Guest





PostPosted: Sat Sep 27, 2003 4:21 am    Post subject: Re: string with spaces Reply with quote

In article <20030926140941.15300.00000278 (AT) mb-m04 (DOT) aol.com>,
[email]developwebsites (AT) aol (DOT) comb[/email]atSPAM says...
Quote:
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.

Back to top
Mike Wahler
Guest





PostPosted: Sat Sep 27, 2003 4:48 am    Post subject: Re: string with spaces Reply with quote


"Jerry Coffin" <jcoffin (AT) taeus (DOT) com> wrote

Quote:
In article <20030926140941.15300.00000278 (AT) mb-m04 (DOT) aol.com>,
[email]developwebsites (AT) aol (DOT) comb[/email]atSPAM 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
Quote:
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




Back to top
Jerry Coffin
Guest





PostPosted: Sat Sep 27, 2003 4:51 am    Post subject: Re: string with spaces Reply with quote

In article <IE8db.5922$NX3.2079 (AT) newsread3 (DOT) news.pas.earthlink.net>,
[email]mkwahler (AT) mkwahler (DOT) net[/email] says...

[ ... ]

Quote:
#include

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.

Back to top
Mike Wahler
Guest





PostPosted: Sat Sep 27, 2003 6:50 am    Post subject: Re: [OT] string with spaces Reply with quote

"Jerry Coffin" <jcoffin (AT) taeus (DOT) com> wrote

Quote:
In article <IE8db.5922$NX3.2079 (AT) newsread3 (DOT) news.pas.earthlink.net>,
[email]mkwahler (AT) mkwahler (DOT) net[/email] says...

[ ... ]

#include
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.

Quote:
Either
you're being picky,

Of course. And I've picked you as my victim this time.

Quote:
or I'm up past my bedtime. Smile

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



Back to top
Developwebsites
Guest





PostPosted: Sun Sep 28, 2003 4:39 am    Post subject: Re: string with spaces Reply with quote

Quote:
Not particularly.

#include
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
--------------------------------------------------

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.