-2

I want to input a string with VC, but it seems the getline function didn't work, when I run my program, the "cin" part was skipped. Here is my code

printf("Exercise 1\n");
    printf("Please enter the sentence you want\n");
    char str[256];
    std::cin.getline(str, 256);
    std::cout << str;

Here are my headers

#include <iostream>
#include <string>
#include <bitset>

I'm using VS2015 community, is there anything wrong with my compiler?

D.Kenny
  • 121
  • 1
  • 1
  • 12

1 Answers1

0

Why don't you just do?

char str[256];
std::cin >> str;

or

string mystring;
std::getline (std::cin,mystring);
Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
  • Well, I did that before. But I need to count the word and characters in my string, and the blank space between two words need to be skipped. That's why I used getline function. So how can I do that? – D.Kenny Nov 29 '16 at 00:21
  • BTW, `std::cin >> mystring` will only read a word. To get the enter text line, `std::getline` must be used. – Thomas Matthews Nov 29 '16 at 00:23
  • I see. Edited. Thanks for the comment @ThomasMatthews – Imesha Sudasingha Nov 29 '16 at 00:31
  • @D.Kenny : For that, see the most voted(1800+) answer of http://stackoverflow.com/questions/236129/split-a-string-in-c and split strings later. – Imesha Sudasingha Nov 29 '16 at 00:39