Hey, I need to know what identifier i can use instead of string that will output all of the words that you type in the program. For example: if i was to use

string ans1;

cout <<"How are you?: ";
cin >>ans1;


and for my answer i typed "very good"...it would just display "very" not "very good"

what would i use instead of string?

Recommended Answers

All 7 Replies

The problem isn't your data type, it's your input method: the >> operator stops reading at whitespace. To read a full line, use getline() instead:

string ans1;

cout << "How are you? ";
getline(cin, ans1);

didn't work

Ppl need more than "didn't work" to understand a problem.

my man

See her avatar? Read: "Code Goddess"?

didn't work my man

My apologies, I assumed you were competent enough to take the minor snippet change I provided and work it into a complete program. Try this:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string ans1;

    cout << "How are you? ";
    getline(cin, ans1);

    cout << ans1 << '\n';
}
commented: People these days +9

okay, it works except for it doesn't show the first word of the entered text. say i pulled the program up and it prompts....what is your name?...then i input...My name is john smith...... it would output...name is john smith. Why?

Ppl need more than "didn't work" to understand a problem.

See her avatar? Read: "Code Goddess"?

no need to be a douche. Mind your own business if you arent going to help. bye

okay, it works except for it doesn't show the first word of the entered text. say i pulled the program up and it prompts....what is your name?...then i input...My name is john smith...... it would output...name is john smith. Why?

Without seeing your code, I'd guess that you're still reading the first word separately and then overwriting it with the rest of the line. Perhaps something like this:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    string ans1;
 
    cout << "How are you? ";
    cin >> ans1;
    getline(cin, ans1);
 
    cout << ans1 << '\n';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.