Moving between areas

This is a discussion on Moving between areas within the C++ Programming forums, part of the General Programming Boards category; Code: while (true) { // Display the players location and possible movements. cout << "------------------" << endl; cout << "Location: ...

  1. #91
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Code:
    	while (true) {
    
    		// Display the players location and possible movements.
    
    		cout << "------------------" << endl;
    		cout << "Location:   " << NewUser.location->name << endl;
    		cout << "\nDescription:   " << NewUser.location->description << endl;
    		if (NewUser.location->north)
    			cout << "(N)orth to: " << NewUser.location->north->name << endl;
    		if (NewUser.location->south)
    			cout << "(S)outh to: " << NewUser.location->south->name << endl;
    		if (NewUser.location->east)
    			cout << "(E)ast to:  " << NewUser.location->east->name << endl;
    		if (NewUser.location->west)
    			cout << "(W)est to:  " << NewUser.location->west->name << endl;
    		cout << "(Q)uit" << endl;
    
    		// Get input and decide where to go next.
    
    		char input;
    		cin >> input;
    		if (input == 'n' && NewUser.location->north)
    
                    cout << quizMaster.poseQuestion().c_str() << endl;
                    getline(cin, answer);
    
                if (quizMaster.isCorrectAnswer(answer))
                    cout << "Thats right" << endl;
                else
                    cout << "Better luck next time !" << endl;
    
    			NewUser.location = NewUser.location->north;
    
    		if (input == 's' && NewUser.location->south)
    
                    cout << quizMaster.poseQuestion().c_str() << endl;
                    getline(cin, answer);
    
                if (quizMaster.isCorrectAnswer(answer))
                    cout << "Thats right" << endl;
                else
                    cout << "Better luck next time !" << endl;
    
    			NewUser.location = NewUser.location->south;
    
    		if (input == 'e' && NewUser.location->east)
    
                    cout << quizMaster.poseQuestion().c_str() << endl;
                    getline(cin, answer);
    
                if (quizMaster.isCorrectAnswer(answer))
                    cout << "Thats right" << endl;
                else
                    cout << "Better luck next time !" << endl;
    
                    NewUser.location = NewUser.location->east;
    
    		if (input == 'w' && NewUser.location->west)
    
                    cout << quizMaster.poseQuestion().c_str() << endl;
                    getline(cin, answer);
    
                if (quizMaster.isCorrectAnswer(answer))
                    cout << "Thats right" << endl;
                else
                    cout << "Better luck next time !" << endl;
    
    			NewUser.location = NewUser.location->west;
    
    		if (input == 'q')
    			break;
    	}

    So basically it could be done like this?

  2. #92
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    I think a issue I need to address is how the loop is structured. so that moving to the loacation is not a part of the else

  3. #93
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    The logic could be reworked. Like so:
    Code:
    	while (true) {
    		// ... display possible directions for travel
    
    		// Get input and decide where to go next.
    		char input;
    		cin >> input;
    
    		if (input == 'q')
    			break;
    
                    cout << quizMaster.poseQuestion().c_str() << endl;
                    getline(cin, answer);
    
    		if (quizMaster.isCorrectAnswer(answer)) {
                    	cout << "Thats right" << endl;
    
    			if (input == 'n' && NewUser.location->north)
    				NewUser.location = NewUser.location->north;
    			if (input == 's' && NewUser.location->south)
    				NewUser.location = NewUser.location->north;
    			// ... other directions ...
    
    		} else {
                    	cout << "Better luck next time !" << endl;
    		}	
    
    	}

  4. #94
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    by doing it that way, when the user takes a direction, it does not move, it repeats the same room.

  5. #95
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    Can you tell why it's staying in the same room? You can use a debugger to find out exactly what code is being run when. If you pay careful attention to the output, the exact point at which something unexpected happens is usually a clue as to what is wrong.

    Does the application ask for a direction choice?
    Is the input getting read? (you can cout the input back to the user to confirm)
    Does it ask a test question?
    Does it correctly evaluate the answer to the test question? (display: That's right, or Better luck next time)
    Is it moving, but to the wrong room? Are rooms connected properly?

  6. #96
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    I believe it is something to do with the input, it does not ask for it and defaults to incorrect, hence not moving anywhere.

  7. #97
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    This could be because there is leftover in the input buffer after the cin >> input; line.
    Using cin for anything other than strings can be unreliable. You could try changing the line cin >> input; to this:
    Code:
    char input;
    string inputString;
    cin >> inputString;	// Grab everything in the buffer, even if it's more than one character.
    input = inputString[0];	// Only use the first character, discard the rest.

  8. #98
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    yeah I noticed that over the time I have been using c++, before I changed it to getline(cin,input) through out my program for example I had , Username, Age, Pet, This, That, as input for the user. The user could enter all of them in 1 line by putting a space between.

    But you cannot use the getline function for char right?

  9. #99
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    Not directly, but you can use a string inbetween:
    Code:
    char input;
    string inputString;
    getline(cin, inputString);
    input = inputString[0];

  10. #100
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Yep, I just figured that I needed a getline instead of the cin in between.

    Thanks alot for your help, I can put all my elements together now I got that troublesome part out the way!

  11. #101
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Why do you need a char in the first place?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 7 of 7 FirstFirst 1234567
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 04:20 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-24-2007, 12:05 AM
  3. rescaling areas
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 02-28-2006, 03:22 PM
  4. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 05:46 AM
  5. Simple program i cant get (dot moving)
    By Lupusk9 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 09:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21