I'm trying to create a loop if the string wordEasy contains more than 5 letters.
I am using the 'while' and 'for' but none seem to work. I am hopping if you guys would be kind enough to help me figure out what I'm doing wrong.
Here is the 'for' method.
// Easy word verification loop
if (difficulty == 1)
{
cout << "Enter a word of your choice that contains no more than 5 characters\n";
const int MAX_CHAR = 5;
string wordEasy[MAX_CHAR];
cin >> wordEasy;
//for method
for (int i = 0; i < wordEasy; ++i)
{
while (wordEasy.size()>=5)
{
cout << "You entered a word that contains " << wordEasy.size()<< " characters.\n";
cout << "Enter another word that contains 5 or less characters.\n";
}
}
}
And here is the while method:
if (difficulty == 1)
{
cout << "Enter a word of your choice that contains no more than 5 characters\n";
string wordEasy;
cin >> wordEasy;
while (wordEasy.size()>=5);
{
cout << "You entered a word that contains " << wordEasy.size()<< " characters.\n";
cout << "Enter another word that contains 5 or less characters.\n";
}
}
And here is the complete program:
#include <iostream>
#include <string>
using namespace std;
int main()
//Difficulty Loop
//infinite loop error.
{
int difficulty;
bool pass1 = false;
cout << "Welcome to 'CAN YOU GUESS MY NUMBER'\n" <<endl;
while(!pass1)
{
cout << "Choose your difficulty: \n" <<endl;
cout << "1: Easy" <<endl;
cout << "2: Normal" <<endl;
cout << "3: Hard" <<endl;
cout << "0: Quit\n" <<endl;
cout << "Enter your choice: 0-3:\n"<< endl;
cin >> difficulty;
switch (difficulty)
{
case 0:
cout << "Thank you for playing.\n" << endl;
pass1=true;
break;
case 1:
cout << "You will play the game in easy\n" << endl;
pass1 = true;
break;
case 2:
cout << "You will play the game in Normal difficulty\n" << endl;
pass1 = true;
break;
case 3:
cout << "You will play the game in Hard\n" << endl;
pass1 = true;
break;
default:
cout << "\nYou have made an invalid choice." << endl;
pass1=false;
break;
}
}
// Easy word verification loop
if (difficulty == 1)
{
cout << "Enter a word of your choice that contains no more than 5 characters\n";
const int MAX_CHAR = 5;
string wordEasy[MAX_CHAR];
cin >> wordEasy;
//for method
for (int i = 0; i < wordEasy; ++i)
{
while (wordEasy.size()>=5)
{
cout << "You entered a word that contains " << wordEasy.size()<< " characters.\n";
cout << "Enter another word that contains 5 or less characters.\n";
}
}
}
//Normal word verification loop
if (difficulty == 2)
{
cout << " Enter a word of your choice that contains no more than 10 characters\n";
string wordNormal;
cin >> wordNormal;
while (wordNormal.size()>=10);
{
cout << "You entered a word that contains more than 10 characters\n";
cout << "Enter another word that contains 10 or less characters.\n";
}
}
//Hard word verification loop
if (difficulty == 3)
{
cout << " Enter a word of your choice that contains no more than 15 characters\n";
string wordHard;
cin >> wordHard;
while (wordHard.size()>=14);
{
cout << "You have entered a word that contains more than 15 characters\n";
cout << "Enter another that contains 15 or less characters.\n";
}
}
return 0;
}