+ 2

PLS HELP ME

HOW TO DECLARE STRING INPUT IN C++

26th Dec 2016, 12:14 PM
MEDHIR
MEDHIR - avatar
5 Answers
+ 2
#include <iostream> using namespace std; int main() { char str[100]; cout << "Enter a string: "; cin >> str; cout << "You entered: " << str << endl; cout << "\nEnter another string: "; cin >> str; cout << "You entered: "<<str<<endl; return 0; } Syntex -char str[ ]
26th Dec 2016, 1:07 PM
sreya
sreya - avatar
+ 2
thank u guys
27th Dec 2016, 7:34 AM
MEDHIR
MEDHIR - avatar
+ 1
you need #include <iostream> then string s = ""; std::cin >> s;
26th Dec 2016, 12:29 PM
Andreas K
Andreas K - avatar
0
@MEDHIR For the old style C-Strings, @sreya 's method is correct; I'd suggest just one minor change: instead of cin, when inputting strings, use gets() function. cin stops at the first blank space, gets() does not. similarly, for outputting, use puts() gets() and puts() require header file <stdio.h> (and, if you're curious, are pronounced "get-ess" and "put-ess")
12th Feb 2017, 8:30 AM
Nathan Algren
0
however, C++ also has a useful String Class - a derived datatype. //declare it as: string spam; //input as getline (cin, spam); //output using any method you want now, word of caution. string class is not supported by some compilers, especially old ones that do not follow the latest C++ standard. (e.g. I know Borland Turbo to not support the string class)
12th Feb 2017, 8:33 AM
Nathan Algren