Question #349704

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”


Expert's answer

#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 << " ";
		}
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS