+ 2
PLS HELP ME
HOW TO DECLARE STRING INPUT IN C++
5 odpowiedzi
+ 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[ ]
+ 2
thank u guys
+ 1
you need #include <iostream>
then
string s = "";
std::cin >> s;
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")
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)