Hi masters!

I have a problem with my while loop.
I realised that my code does not work the way I want.
It has to check if sum of def, mid and att variables is different from 10 or if style variable is different from multiple strings. So if any of them does not meet the requirement, repeat the code in {} .

My style condition which is repeated is interfering with other style conditions.

So my question is - How to write this while loop (or it doesn't have to be while) to give me the thing I want?

Is there any way to write it like:

while(style!={"contain","defensive","counter"}){
    //do something
}

Here is the code:

char znak; // just takes "-" in the line like this: 4-4-2
string style = "standard"; // default style

cout << "--- MATCH PREPARATION ---" << endl;
cout << "Choose formation (like 4-4-2): "; cin >> def >> znak >> mid >> znak >> fwd; 
cout << endl;
cout << "Choose style (contain,defensive,counter,standard,control,attacking,overload): " << endl; 
cin >> style; 
cout << endl;

/////////Problematic while loop////////////////////
while((def+mid+fwd)!=10 || style!="contain" || style!="defensive" || style!="counter" || style!="standard" || style!="control" || style!="attacking" || style!="overload")
	{
	cout << "Unknown style or formation! Please check your spelling..." << endl;
        cout << "Choose formation (like 4-4-2): "; 
        cin >> def >> znak >> mid >> znak >> fwd; 
        cout << endl;
        cout << "Choose style(contain,defensive,counter,standard,control,attacking,overload): " << endl; 
        cin >> style; 
        cout << endl;
        }

Recommended Answers

All 3 Replies

while(((def+mid+fwd)!=10) ||( style!="contain" && style!="defensive" && style!="counter" && style!="standard" && style!="control" && style!="attacking" && style!="overload"))

Use this condition, I think it will work for you, if I understand your problem correctly.

Yes, I believe the above will work.
As for the other question...

Is there any way to write it like while( style!={"contain","defensive","counter"} ) {/*...*/}

Yes, there is a way to do it like this:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

const string allowed_styles[7] = { "contain", "defensive",
    "counter", "standard", "control", "attacking", "overload" };

bool is_valid_style(const string & style)
{
    return find(allowed_styles,
                allowed_styles + 7,
                style) != allowed_styles + 7;
}

int main()
{
    string style;

    cout << "enter style: ";
    cin  >> style;

    while ( !is_valid_style(style) )
    {
        cout << "invalid style...\n";
        cout << "enter style: ";
        cin  >> style;
    }

    return 0;
}

Useful link -> http://cplusplus.com/reference/algorithm/find/

Yeah. Great guys.

Thanks a lot!

Hope it helps other beginners

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.