create a c++ program with a function named display that displays 10 random words input by user using array. It should accept spaces and don’t accept numbers. If numbers were input it should say “INVALID INPUT”
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string>vs(10);
string tmp;
bool isDig = false;
cout << "Please enter 10 words:\n";
for (int i = 0; i < 10; i++)
{
cout << "Enter " << i + 1 << " word: ";
cin >> tmp;
for (int j = 0; j < tmp.size(); j++)
{
if (isdigit(tmp[j]))
{
isDig = true;
break;
}
}
if (isDig == true)
{
cout << "INVALID INPUT";
break;
}
vs.push_back(tmp);
}
if (isDig == false)
{
cout << "Entered words: ";
vector<string>::iterator it;
for (it = vs.begin(); it != vs.end(); it++)
{
cout << *it << " ";
}
}
}
Comments
Leave a comment