0

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

Edited by niggz: n/a

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by niggz
0
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.

0

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/

Edited by m4ster_r0shi: n/a

0

Yeah. Great guys.

Thanks a lot!

Hope it helps other beginners

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.