0

i wanted to let a user input a string which has whitespaces.but my compiler seems to only take the character and does not include the whitespace. I have entered the code below:

#include <iostream>

using namespace std;


int main ()
{
     char str[100];
     cout<<"Enter the value";
     cin>>str;
     cout<<"Value is :";cout<<str;

     return 0;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Ranjul Arumadi
  • 109
  • 1
  • 2
  • 11

1 Answers1

1

Try using std::string instead of char array along with std::getline:

#include <iostream>
#include <string>

int main () {
     std::string str;
     std::cout << "Enter the value: ";
     std::getline(std::cin, str);
     std::cout << std::endl << "Value is :" << str;
     return 0;
}

std::getline takes new line character \n as a default delimiter. Therefore, str variable will have whitespaces included.

NutCracker
  • 11,485
  • 4
  • 44
  • 68