9 Replies - 756 Views - Last Post: 20 November 2010 - 10:50 PM Rate Topic: -----

#1 rookie007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-November 10

Loop problem

Posted 20 November 2010 - 08:45 PM

Hello,
I am having problem with this loop. Problem is with case '?' and the loop doesnt break. I want to print the help for entering '?'. Also its accepting any value and if a value apart from 'd' 'u' 'r' 'l' is entered twice it keeps printing the help(directions). Hope I am clear enough. Any help appreciated.


void Solitaire::Move()							//Move Function Defition
{
	int x, y;
	char m;
	cout << "For help press '?'"<<endl;
	cout<< "To move, enter a valid move and press enter" <<endl;
	cout << "Enjoy" << endl;
	do
	{
		cout << "\nPlease Enter your move"<<endl;
		cin >> m;
		if (m!='e') 
		{cin >> x >> y;
		switch (m)
		{
		case 'u':
			{
				if (Valid(m,x,y)==true) 
					Up(x,y);
				else
					cout << "Invalid move!"<<endl;
				display(cout);
			}
			break;

		case 'd':
			{
				if (Valid(m,x,y)==true)
					Down(x,y);
				else
					cout << "Invalid move!"<<endl;
				display(cout);
			}
			break;

		case 'r':
			{
				if (Valid(m,x,y)==true)
					Right(x,y);
				else
					cout << "Invalid move!"<<endl;
				display(cout);
			}
			break;

		case 'l':
			{
				if (Valid(m,x,y)==true)
					Left(x,y);
				else
					cout << "Invalid move!"<<endl;
				display(cout);
			}
			break;

		case '?':
			{
				x=y=0;
				cout << "Directions are 'l' for left, 'r' right, 'u' for up and 'd' for down. \n                               To Exit enter 'e'"<<endl;
				break;
			}

		default: cout << "Please Enter a valid command"<<endl;
			break;
		}
		}
		else
			cout << "\nGAME EXITED. THANKS FOR PLAYING " <<endl;
	}while (m!='e');
}



Is This A Good Question/Topic? 0
  • +

Replies To: Loop problem

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3777
  • View blog
  • Posts: 13,674
  • Joined: 08-August 08

Re: Loop problem

Posted 20 November 2010 - 08:58 PM

What is this???

                x=y=0;


Do you mean this?

                x=0;
                y=0;


Was This Post Helpful? 0
  • +
  • -

#3 rookie007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-November 10

Re: Loop problem

Posted 20 November 2010 - 09:03 PM

View PostCTphpnwb, on 20 November 2010 - 07:58 PM, said:

What is this???

                x=y=0;


Do you mean this?

                x=0;
                y=0;



yes x and y both = 0
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2030
  • View blog
  • Posts: 5,430
  • Joined: 27-December 05

Re: Loop problem

Posted 20 November 2010 - 09:20 PM

Some suggestions:
You don't need braces {} around the statements under every "case". Your code will be cleaner & easier to read without them. (You do need the opening brace immediately after "switch(m)" and the closing brace following the last break.)

Add another if statement for input of values for x and y because user shouldn't have to enter a move before getting help. (i.e.: right after "if(m!='e')" say
if (m!='?') {
    cin >> x >> y;
}


Add a buffer, e.g., char buf[80] to use for dumping invalid inputs such as the multiple characters that cause your program to go into an infinite loop. Then, immediately after the "cout << m << endl;", add this line:
cin.getline(buf,80);


If there is any junk left behind after you retrieved the m value, that will clear it out.

I don't know what you mean when you say the loop "doesn't break"; I think the above suggestions should clear things up.

This post has been edited by r.stiltskin: 20 November 2010 - 09:24 PM

Was This Post Helpful? 1
  • +
  • -

#5 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: Loop problem

Posted 20 November 2010 - 09:25 PM

View PostCTphpnwb, on 20 November 2010 - 10:58 PM, said:

What is this???

                x=y=0;


Do you mean this?

                x=0;
                y=0;



thats legal its just a little unorthodox.
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2030
  • View blog
  • Posts: 5,430
  • Joined: 27-December 05

Re: Loop problem

Posted 20 November 2010 - 09:30 PM

View Post-shadow-, on 20 November 2010 - 11:25 PM, said:

thats legal its just a little unorthodox.

It wouldn't even occur to me to criticize it. That's perfectly valid syntax.
Was This Post Helpful? 1
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2030
  • View blog
  • Posts: 5,430
  • Joined: 27-December 05

Re: Loop problem

Posted 20 November 2010 - 09:37 PM

Oh, and by the way, you also don't need the "break" after the default case. The breaks are there just to prevent the other cases following the correct one from being executed. After the default you immediately exit the switch anyway, so there's no need to break there.

e.g.:
    switch(m)
    {
     // various cases
            break;
        default:
            cout << "Please enter a valid command" << endl;
    }
    // rest of the function


Was This Post Helpful? 0
  • +
  • -

#8 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: Loop problem

Posted 20 November 2010 - 09:38 PM

@r.stiltskin
CTphpnwb did:

View PostCTphpnwb, on 20 November 2010 - 10:58 PM, said:

What is this???

                x=y=0;


Do you mean this?

                x=0;
                y=0;


This post has been edited by -shadow-: 20 November 2010 - 09:39 PM

Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2030
  • View blog
  • Posts: 5,430
  • Joined: 27-December 05

Re: Loop problem

Posted 20 November 2010 - 09:54 PM

In case you're still confused, here's a greatly simplified version of that function which works the way you want (but does nothing useful). I changed the condition for entering x and y also.

#include <iostream>
using namespace std;


void Move()
{
    int x, y;
    char m;
    char buf[80];
    cout << "For help press '?'"<<endl;
    cout<< "To move, enter a valid move and press enter" <<endl;
    cout << "Enjoy" << endl;
    do
    {
        cout << "\nPlease Enter your move"<<endl;
        cin >> m;
        cin.getline(buf, 80);
        if (m!='e') {
            if (m == 'u' || m == 'd' || m == 'r' || m == 'l') {
                cout << "Enter values for x and y" << endl;
                cin >> x >> y;
            }
            switch (m)
            {
                case 'u':
                    cout << "case u" << endl;
                    break;

                case 'd':
                    cout << "case d" << endl;
                    break;

                case 'r':
                    cout << "case r" << endl;
                    break;

                case 'l':
                    cout << "case l" << endl;
                    break;

                case '?':
                    x=y=0;
                    cout << "Directions are 'l' for left, 'r' right, 'u' for up and 'd' for down. \n                               To Exit enter 'e'"<<endl;
                    break;

                default:
                    cout << "Please Enter a valid command"<<endl;
            }
        }
        else {
            cout << "\nGAME EXITED. THANKS FOR PLAYING " <<endl;
        }
    }while (m!='e');
}

int main() {
    Move();
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#10 rookie007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-November 10

Re: Loop problem

Posted 20 November 2010 - 10:50 PM

Thanks everyone for the help.
Its just a little console game app. everything was working fine. including the moves and validation. just had a little problem with switch.

thanks again :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1