Heng Aik Hwee
Heng Aik Hwee

Reputation: 159

i want to take a user inputs and output it in my shell environment

I want to take user input and output it on my screen. I'm suppose to let user to key in what kind of type do they need like Type O for example, but my output didnt capture my O , just my Type , so is there a way for it to capture the whole line inside of the Type only?

a sample output of my code.

Enter Sun Type: type k

Enter planets: 10

Sun type that was entered: type

No of Planets: 10

This is only a part of my whole lengthy code.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class LocationData
{   
    private:
    string sunType;
    int noOfEarthLikePlanets;
    int noOfEarthLikeMoons;
    float aveParticulateDensity;
    float avePlasmaDensity;
    public:

};
int main()
{
    int i;
    string s;
    LocationData test;
    cout<<"Enter Sun Type: ";
    cin>>s;
    test.setSunType(s);
    cin.clear();
    cin.ignore(10000,'\n');
    cout<<"Enter planets: ";
    cin>>i;
    test.setNoOfEarthLikePlanets(i);
    cout<<"Sun type that was entered: "<<test.getSunType();
    out<<"\nNo of Planets: "<<test.getNoOfEarthLikePlanets()<<endl;
}

Upvotes: 2

Views: 104

Answers (2)

taufique
taufique

Reputation: 2751

try using gets() / getline() function. cin and cout ignores whitespaces by default. I don't know how to make them accept whitespace. But with above functions you will get your desired result.

Reference: gets(), getline()

Upvotes: -1

john
john

Reputation: 8027

Yes

getline(cin, s);

reads a whole line and puts the line into the s variable. As you have found

cin >> s;

reads only a single word, which is why it stops at the space between Type and O.

Upvotes: 2

Related Questions