string with spaces

D

Developwebsites

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?
 
M

Mike Wahler

Developwebsites said:
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 said:
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
 
J

jeffc

Developwebsites said:
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.
 
M

Mike Wahler

jeffc said:
Developwebsites said:
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
 
J

Jerry Coffin

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.
 
M

Mike Wahler

Jerry Coffin said:
Not particularly.

#include <iostream>

#include said:
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
 
M

Mike Wahler

Jerry Coffin said:
[ ... ]
#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
 
D

Developwebsites

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$
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top