Hikari Iwasaki
Hikari Iwasaki

Reputation: 941

Using cin in C++

I'd like to use cin and I used char for the int type (do you call it like that?) and it just shows one letter of what typed. How can I get the whole sentence?

Upvotes: 1

Views: 6510

Answers (2)

greatwolf
greatwolf

Reputation: 20838

Since you're using c++ why not use std::string instead? Something like this should do what you're looking for:

#include <iostream>
#include <string>

int main()
{
  using namespace std;
  string sentence;
  cout << "Enter a sentence -> ";
  getline(cin, sentence);
  cout << "You entered: " << sentence << endl;
}

Upvotes: 5

Nick Radford
Nick Radford

Reputation: 1038

use cin.getline()

char name[256];
cout << "What is your name?\n>";
cin.getline(name, 256);

cout << "Your name is " << name;

Upvotes: 2

Related Questions