HELP ME! I'm having trouble w/ while loops!

closed account (L1T0RXSz)
Okay, so today I was working on my game I was making and the while loop in it is not working. Can someone help me? It is strange!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 
//First draft for main.cpp
//Hopefully this will be a good game...
//This is the main file, connecting all the others
//This will also be the file being compiled
#include <string.h>
#include "includer.h"   // Header file with all the #includes
#include "definer.h"    // Header file with all the #defines
int main()              // Starts the file      
{
        char udoout;    // Declaration of variable "udoout" (udoout really means what you do)
        int hp, full, money;    // Declaration of integers 

land_a_1: "Hi!\n";      // Land piece A1

        cout << "\n\n\n\n\n\n\n\n\n\n\n";       //Beginning spaces      
        cout << "Welcome to Unitowny!\n";       //Greeting
        cout << "You are at your home.\n"       //Setting
        << "CTRL-D to quit.\n"                  //Info
        << "To look around the town you are in, type 'f'.\n"    //Info
        << "But your best friend will be 's' because it will show you the commands to use.\n"   //Info
        << "To use the tools below, type the letter that comes\n"       //Info
        << "At any time you may also use 'c' to help you.\n"    //Info
        << "before the task you want to do.\n"  //Things to remember
        << "Options:\n" //These are the things you can do at your home
        << "a: Go out of your home.\n"  //Option<< "b: Go to sleep.\n"  //Option
        << "c: Check on your stats.\n"  //Option
        << "d: See your home.\n"        //Option
        << "e: Type a command.\n"       //Option
        << "f: Look around your town.\n\n";     //Option
do {    //Beginning of do statement
cin >> udoout;  //command input
// I thought letters would be more interesting than typing in a command. I don't know if it's true.
// I'll find out! :D

                 switch (udoout)        //Beginning of switch statement
                 {
                case 'a':       //If 'a'
                        cout << "You are successfully out of your home.\n";     //Confirmation
                        goto land_a_1   // Why won't it go to land_a_1 here!?!
                        break;  //End of case
                case 'b':       //If 'b'
                        cout << "You slept and are now fully healed.\n";        //Confirmation
                        hp = full;      //HP set to full
                        break;  //End of case
                case 'c':       //If 'c'
                        cout << "Your stats are: HP: " << hp <<"\t Money:" << money << "\n" ; //Stats
                        break;  //End of case
                case 'd':       //If 'd'
                        cout << "\n";   //I still need to make this part.
                        break;  //End of case
                case 'e':       //If 'e'
                        cout << "Type a command.\n";    //I still need to make this part.       
                        break;  //End of case
                case 'f':       //If 'f'
                        //Town goes here
                        break;  //End of case
                case 's':       //If 's'
                        cout << "Options:\n"    //Options
                        << "a: Go out of your home.\n"  //Options
                        << "b: Go to sleep.\n"  //Options
                        << "c: Check on your stats.\n"  //Options
                        << "d: See your home.\n"        //Options
                        << "e: Type a command.\n"       //Options
                        << "f: Look around your town.\n\n";     //Options
                        break;  //End of case
                 }      //End of switch statement 
}       //End of do loop
while           (  (udoout == 'b')      // Beginning of while loop and if udoout = 'b' then recycle 
                || (udoout == 'c')      // If udoout = 'c' then recycle 
                || (udoout == 'd')      // If udoout = 'd' then recycle 
                || (udoout == 'e')      // If udoout = 'e' then recycle 
                || (udoout == 'f')      // If udoout = 'f' then recycle 
                || (udoout == 's')      // If udoout = 's' then recycle 
                );      //End of while loop

cout << "Command not found.\n\a"; // If the above is not true, say "Command not found." and beep.

        return 0;       //End
}       //End of main 


I also have other less important questions.

1. How do you use the goto statement? (It's not working for me)
2. Do I use too many comments? (I read somewhere it is bad to use too many...)

If you can answer these questions, I would appreciate it very much.

Thanx,
Akash Levy
Last edited on
Your goto statement isn't working, because it needs a semicolon at the end.
Are you seeing any error messages when you compile? They are quite useful for diagnosing problems. For example, "Error E2378 test.cpp 40: Goto statement missing ; in function", means that your goto statement is missing a ";"

too many comments is much better than too few comments. Don't repeat yourself, though.
hp = full; //HP set to full
That's pretty redundant.
closed account (L1T0RXSz)
No, I am not having trouble compiling and sorry for the semicolon error. I forgot to put the semicolon there, but in the program I compiled it is fine. Thanx for the tip about comments.
Can you please help me with the while loops though?

Thanx,
Akash
Hi Akash.
I don't like do....while loops much and i don't recommend it to beginners most miss the fact that it will always enter the loop at least once and its not what you want(most of the time you don't), anyway who cares what i like eh?
Your loop is kinda working i tested it on Microsoft's(my linux box is being used by my girlfriend) compiler but i see some problems. I will try to point you to the right direction ...
1º: initialize your variables before you use it you are trying to cout money and it's not initialized thats bad programming practice and will lead to bad results;
2º: gotos are ok but i would avoid them, the problem is that they can really go to anywhere in the code and when your code gets bigger you will wish that you never used it and its also considered by some programmers bad programming practice. I recommend a function here;
3º: its always good to tell the user how to exit the loop every time he chooses an option(a case in the loop) so the user know how to quit, go back to previous menu, etc... it also makes the program more user friendly.
4º comments is always nice but yeah thats a lot of comments you don't have to comment every single line in your code like magical said i see many redundant comments there but i will let you find your own style just try to not overdo it. The one that he pointed out should be enough to give you an idea.

so your code should look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <string.h>
#include "includer.h"   // Header file with all the #includes
#include "definer.h"    // Header file with all the #defines

void land_a_1(); // Shows the welcome message.
void gameOption();// Displays the commads
int main()              // Starts the file      
{
        char udoout = ' ';    // Declaration of variable "udoout" (udoout really means what you do)
        int hp = 0, full = 0, money = 0; // Declaration of integers full shouldn't really be 0 ;P
	//bool keepPlaying = true; // sentinel variable to exit the game loop.
	land_a_1();
	
//while(keepPlaying) //game loop
//{
while(1) /* game loop. warning infinite loop if you use one of these always tell the user how to get out
		but sice you already are its ok but try to avoid it your better off with a sentinel variable. */
{
	cin >> udoout;  //command input
// I thought letters would be more interesting than typing in a command. I don't know if it's true.
// I'll find out! :D

	switch (udoout)        //Beginning of switch statement
    {
        case 'a':       //If 'a'
		cout << "You are successfully out of your home.\n";     //Confirmation
		land_a_1(); // Why won't it go to land_a_1 here!?!
                break;  //End of case
	case 'b':       //If 'b'
		cout << "You slept and are now fully healed.\n";        //Confirmation
                hp = full;      //HP set to full
                break;  //End of case
	case 'c':       //If 'c'
		cout << "Your stats are: HP: " << hp <<"\t Money:" << money << "\n" ; //Stats
                break;  //End of case
	case 'd':       //If 'd'
		cout << "\n";   //I still need to make this part.
                break;  //End of case
        case 'e':       //If 'e'
		cout << "Type a command.\n";    //I still need to make this part.       
                break;  //End of case
        case 'f':       //If 'f'
       //Town goes here
	       break;  //End of case
        case 's':       //If 's'
		gameOption();
		break;  //End of case
	//case 'q':
	//	cout << "Bye! been fun!\n"; //;P
	//	keepPlaying = false;   // changes the sentinel so it exits the loop
        //      break;
	default: // this will happens everytime he uses an unknow command.
		cout << "Command not found.\n\a";// If the above is not true, say "Command not found." and beep.
		gameOption();
		break;
	} //End of switch statement 
} // End of while loop.

//cout << "Command not found.\n\a"; // If the above is not true, say "Command not found." and beep.

        return 0;       //End
}       //End of main

void land_a_1()
{
	cout << "Hi!\n"      // Land piece A1
		<< "\n\n\n\n\n\n\n\n\n\n\n"	   //Beginning spaces      
		<< "Welcome to Unitowny!\n"       //Greeting
		<< "You are at your home.\n"      //Setting
		<< "CTRL-D to quit.\n"            //Info
		<< "To look around the town you are in, type 'f'.\n"    //Info
	        << "But your best friend will be 's' because it will show you the commands to use.\n"   //Info
                << "To use the tools below, type the letter that comes\n"       //Info
                << "At any time you may also use 'c' to help you.\n"    //Info
                << "before the task you want to do.\n";  //Things to remember
         gameOption();
}

void gameOption()  //Option
{
	cout << "Options:\n" //These are the things you can do at your home
		<< "a: Go out of your home.\n" 
		//<< "b: Go to sleep.\n"
		<< "c: Check on your stats.\n"  
		<< "d: See your home.\n"        
                << "e: Type a command.\n"       
                << "f: Look around your town.\n\n"
		<< "CTRL-D to quit.\n"; //<< "q: To quit.\n";
}


I don't know if thats the behavior you want from land_a_1 function adapt it to your needs.
hope this helps its bed time here so i am sorry if i missed anything good luck with your game!!

sorry for the long post :D

Jeff.
Last edited on
closed account (L1T0RXSz)
whoa thank you thank you thank you!!! this is wonderful!!
akash
Topic archived. No new replies allowed.