int main()
{
key[100];
int i = 0, t = 0;
cout << "Enter the Keyword :";
while (t < 3)
{
cin.getline(key, 100);
i ;
t ;
}
cout << key[0] << endl;
}
I used this code. But it returns only a character of the word.Please say how to get a single word in array one by one.
CodePudding user response:
#include <iostream>
int main()
{
// 3 strings of 100 char max ( 1 nullchar)
char key[3][101];
int i = 0;
std::cout << "Enter the Keyword :\n";
while(i < 3) {
std::cin.getline(key[i],100);
i ;
}
for(i = 0; i < 3; i) {
std::cout << key[i] << std::endl;
}
return 0;
}
Alternative anwser using C vector and string
#include <iostream>
#include <string>
#include <vector>
int main()
{
// array of strings
std::vector<std::string> keyw_list;
// string for temporary operations
std::string keyw;
std::cout << "Enter the Keyword :\n";
while(key_list.size() < 3) {
std::cin >> keyw;
keyw_list.push_back(keyw);
}
for(size_t i = 0; i < keyw_list.size(); i) {
std::cout << keyw_list[i] << std::endl;
}
return 0;
}
CodePudding user response:
using namespace std;
#include<iostream>
using namespace std;
//include it for istringstream
#include<sstream>
int main ()
{
string inputString;
cout << "Enter String:" << endl;
getline (cin, inputString);
//Crete object of istringstream and initialize assign input string
istringstream iss (inputString);
string word;
//Extract each words only..no spaces.
//This way it can handle any
//special characters.
while (iss >> word)
{
//Display words
cout << word.c_str () << endl;
}
return 0;
}