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

Recommended Answers

All 5 Replies

write a programme that asks the user for lists of five name and write the names to a file rewind the file and display its content on the screen using the seekkg() and get()(function)

string wordEasy[MAX_CHAR];

This will create an array of strings of MAX_CHAR elements. I think you're mixing up C-Strings and string objects. string wordEasy; would be what you want here.

You could do something like this:

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;

        do
        {
            if (!wordEasy.empty())
            {
                cout << "You entered a word that contains " << wordEasy.size()<< " characters.\n";
                cout << "Enter another word that contains 5 or less characters.\n";
            }

            cin >> wordEasy;
        }
        while (wordEasy.size() > MAX_CHAR || wordEasy.empty());
    }

While I'd use a (do-)while loop here, you could ofcourse also do it with a for-loop in which case you'd end up with something like this:

    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;

        for (cin >> wordEasy; wordEasy.empty() || wordEasy.size() > MAX_CHAR; cin >> wordEasy)
        {
            cout << "You entered a word that contains " << wordEasy.size()<< " characters.\n";
            cout << "Enter another word that contains 5 or less characters.\n";
        }
    }




write a programme that asks the user for lists of five name and write the names to a file rewind the file and display its content on the screen using the seekkg() and get()(function)

???????

Mary ...

Well learning how to write to file and read back from file, is a basic thing to learn, the OP does not seem to be using any file operations here :)

Note:

a lot of the time, rather then using an if ... elseif ... else structure

or a switch ... case structure

a 'table look up' will do very nicely ... and could save a lot of redundant coding:

#include <iostream>
#include <sstream> // re ostream objects ...
#include <string>


using namespace std;

const size_t MAX_CHARS[] = { 5, 10, 15 };

const string LEVELS[] = { "EASY", "NORMAL", "HARD" };


// returns a non-empty string with a max string length of char's
std::string takeIn( const std::string& msg, size_t maxStrLen )
{
    std::string val;
    for( ; ; ) // loop forever ... until break
    {
        std::cout << msg << std::flush;
        getline( std::cin, val );
        size_t len = val.size();

        if( len && len <= maxStrLen )
            break;

        if( len ) std::cout << "\nOnly " << maxStrLen
                            << " char's allowed here\n";
        else std::cout << "\nBlank line not valid here.\n";
    }

    return val;
}




int main()
{
    cout << "Welcome to 'CAN YOU GUESS MY NUMBER'\n\n";

    int difficulty;
    int pass = 0;
    bool done = false;
    while( !done )
    {
        cout << "Choose your difficulty: \n\n"
             << "1: Easy\n"
             << "2: Normal\n"
             << "3: Hard\n"
             << "0: Quit\n\n"
             << "Enter your choice: 0-3: " << flush;

        cin >> difficulty;

        if( cin.fail() )
        {
            cin.clear(); // in case NON interger was intered value is -1 still
            difficulty = -1;  // set error value to some not used value
        }

        cin.sync(); // 'flush' cin stream

        switch (difficulty)
        {
            case 0:
                if( pass != 0 ) cout << "Thank you for playing.\n\n";
                done = true;
            break;
            case 1: case 2: case 3:
                cout << "You will play an " << LEVELS[difficulty-1]
                     << " game.\n\n";
            break;
            default:
                cout << "\nYou have made an invalid choice.\n";
                continue; // jump to check at top of while NOW
        }

        if( !done )
        {
            // form prompt
            ostringstream oss;
            oss << "Enter a word with 1.." << MAX_CHARS[difficulty-1]
                << " characters: ";

            string word = takeIn( oss.str(), MAX_CHARS[difficulty-1] );

            cout << "You entered: " << word << endl;
        }

        ++ pass;
    }

    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.get();

}

Here's way that uses a while loop and is inside a function that will eliminate repeating your code 3 times:

#include <iostream>
#include <string>
using namespace std;
string GetWord(char);
int diflevel = 0;
int main()
    //Difficulty Loop
    //infinite loop error.
{
    char 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;
        }
    }
    string validword = GetWord(difficulty);
    cout << validword;
    return 0;
}

string GetWord(char difficulty)
{
    diflevel = (difficulty - 48) * 5;
    bool done = false;
    string word;
    while(!done)
    {
        cout << "Enter a word of your choice that contains no more than " << diflevel << " characters\n";
        cin >> word;
        if(word.size() > diflevel)
        {
            cout << "You entered a word that contains " << word.size()<< " characters.\n";
            word = "";
        }
        else 
            done = true;

    }
    return word;
}

Oops ... just noticed that in the above demo code ... that there should have been

<= (less than or equals)

// returns a non-empty string with a max string length of char's
std::string takeIn( const std::string& msg, size_t maxStrLen )
{
    std::string val;
    for( ; ; ) // loop forever ... until break
    {
        std::cout << msg << std::flush;
        getline( std::cin, val );
        size_t len = val.size();

        if( len && len <= maxStrLen ) // FIXED NOW to also accept maxStrLen chars
            break;

        if( len ) std::cout << "\nOnly " << maxStrLen
                            << " char's allowed here\n";
        else std::cout << "\nBlank line not valid here.\n";
    }

    return val;
}

And another way to code the logic in main ... to avoid using the 'continue' statement to jump out of the switch ...

int main()
{
    cout << "Welcome to 'CAN YOU GUESS MY NUMBER'\n\n";

    int difficulty;
    int pass = 0;
    bool done = false;
    while( !done )
    {
        cout << "Choose your difficulty: \n\n"
             << "1: Easy\n"
             << "2: Normal\n"
             << "3: Hard\n"
             << "0: Quit\n\n"
             << "Enter your choice: 0-3: " << flush;

        cin >> difficulty;

        if( cin.fail() )
        {
            cin.clear(); // in case NON interger was intered value is -1 still
            difficulty = -1;  // set error value to some not used value
        }

        cin.sync(); // 'flush' cin stream

        bool valid = true;
        switch (difficulty)
        {
            case 0:
                if( pass != 0 ) cout << "Thank you for playing.\n\n";
                done = true;
            break;
            case 1: case 2: case 3:
                cout << "You will play an " << LEVELS[difficulty-1]
                     << " game.\n\n";
            break;
            default:
                cout << "\nYou have made an invalid choice.\n";
                valid = false;
        }

        if( !done && valid )
        {
            // form prompt
            ostringstream oss;
            oss << "Enter a word with 1.." << MAX_CHARS[difficulty-1]
                << " characters: ";

            string word = takeIn( oss.str(), MAX_CHARS[difficulty-1] );

            cout << "You entered: " << word << endl;

            ++ pass;
        }
    }

    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.get();

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.