My program is acting really weird, it will run fine sometimes and other times it will just go into a constant loop and screw up. Heres the code of the part thats screwing up.

void AmasiVillage()
{
CharacterLocationID = 1;
system("cls");
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "                                 Amasi Village\n";
cout << "\n";
cout << "                               1)Travel to Koru\n";
cout << "                               2)Travel to Reya\n";
cout << "                               3)Maktor Ride to Drasin Citadel\n";
cout << "                               4)Character Options\n";
cout << "                               5)Exit Game\n";
cout << "\n";
cout << "                               Enter an Option: ";
int choice;
cin >> choice;
if (choice == 1)
Koru();
if (choice == 2)
Reya();
if (choice == 3)
DrasinCitadel();
if (choice == 5)
;
else
system("cls");
cout << "\n                            Error: Invalid Command";
Sleep(2000);
system("cls");
AmasiVillage();
}

Recommended Answers

All 16 Replies

void AmasiVillage()
{
    CharacterLocationID = 1;
    system("cls");
    cout << "\n";
    cout << "\n";
    cout << "\n";
    cout << "\n";
    cout << " Amasi Village\n";
    cout << "\n";
    cout << " 1)Travel to Koru\n";
    cout << " 2)Travel to Reya\n";
    cout << " 3)Maktor Ride to Drasin Citadel\n";
    cout << " 4)Character Options\n";
    cout << " 5)Exit Game\n";
    cout << "\n";
    cout << " Enter an Option: ";
    int choice;
    cin >> choice;
    if (choice == 1)
        Koru();
    if (choice == 2)
        Reya();
    if (choice == 3)
        DrasinCitadel();
    if (choice == 5)
        ;
    else
        system("cls");
    cout << "\n Error: Invalid Command";
    Sleep(2000);
    system("cls");
    AmasiVillage();
}

Lines 26 and 27. What is this supposed to do? Currently you have an if statement with an empty command. You can delete these lines and it'll have no effect. Lines 30 - 33 are going to execute regardless of the value of choice. You are going to display "Invalid command" every time. Is that intentional? Line 33 calls the function recursively and will always be executed so it looks like you have infinite recursion.

I have no idea what the functions on lines 21, 23, and 25 do so I cannot comment on them. Is your intent to have the entire function execute for all values of choice? I'm guessing you want a return statement or an exit statement at least on line 27.

I want the program to shut down on line 27 and lines 28-33 I am trying to make something so if anyone types a command that is not programmed it will display an error message

I want the program to shut down on line 27 and lines 28-33 I am trying to make something so if anyone types a command that is not programmed it will display an error message

wrt lines 28-33, you need then change to

if (choice == 5)
        ;
    else
    [B]{[/B]
        system("cls");
        cout << "\n Error: Invalid Command";
        Sleep(2000);
        system("cls");
        AmasiVillage();
     [B]}[/B]

Note that you are still calling AmasiVillage() recursively, in case of invalid command.

that didn't help, but thanks for trying

Post your new code and problems. Saying: 'it still doesn't work' doesn't give us enough info to help you

the code posted earlier is the same, I tried some different things and nothing worked. So the code posted is what it is. Its still screwing up.

try this :)

void AmasiVillage() {
	CharacterLocationID = 1;
	system("cls");
	cout << endl << endl << endl << endl;
	cout << " Amasi Village" << endl << endl;
	cout << " 1)Travel to Koru" << endl;
	cout << " 2)Travel to Reya" << endl;
	cout << " 3)Maktor Ride to Drasin Citadel" << endl;
	cout << " 4)Character Options" << endl;
	cout << " 5)Exit Game" << endl << endl;
	cout << " Enter an Option: ";
	int choice;
	cin >> choice;
	switch (choice) {
	case 1:
		Koru();
		break;
	case 2:
		Reya();
		break;
	case 3:
		DrasinCitadel();
		break;
	case 5:
		return;
	default:
		cout << "Error: Invalid Command";
		Sleep(2000);
		system("cls");
		break;
	}
	AmasiVillage();
}

the code posted earlier is the same, I tried some different things and nothing worked. So the code posted is what it is. Its still screwing up.

They want more specific errors.

"Screwing up" is too vague.

My program is acting really weird, it will run fine sometimes and other times it will just go into a constant loop and screw up. Heres the code of the part thats screwing up.

for the sake of clarity your code might be condensed:

void AmasiVillage()
{
    CharacterLocationID = 1;
    system("cls");
    cout << endl << endl << endl << endl
         << " Amasi Village\n\n"
         << " 1)Travel to Koru\n"
         << " 2)Travel to Reya\n"
         << " 3)Maktor Ride to Drasin Citadel\n"
         << " 4)Character Options\n"
         << " 5)Exit Game\n\n"
         << " Enter an Option: ";

What are the circumstances surrounding the infinite loop phenomenon? Does it seem to happen when a specific item is selected?

A switch statement is a common way to handle code with multiple options to choose from. However, it can be done with ifs and an else or with an if, else ifs, and an else, whichever you're most comfortable with. To use the original syntax it would be:

if (choice == 5)
   return;
else
{
   system("cls");
   cout << "\n Error: Invalid Command";
   Sleep(2000);
   system("cls");
   AmasiVillage();
}

Using a loop with a sentinnel value changed in choice == 5 to allow repeat user choices rather than recursively calling AmasiVillage() is also an option----recursive calls can quickly cause stack overload and crashing of the program.

Ok I just tried putting it in a switch statement and it didn't change anything.

The Infinite loop happens when something other than the options in the list is typed and it goes to the "Error: Invalid Command" code it just displays Error: Invalid Command like I programmed it to and than it keeps reloading that statement in a loop type thing.

Ah! it is obvious then that you need to clear your input stream after an invalid choice is made so that the same 'invalid' selection doesn't remain stuck in the stream!

#
switch (choice) {

case 1:

Koru();

break;

case 2:

Reya();

break;

case 3:

DrasinCitadel();

break;

case 5:

return;

default:

cout << "Error: Invalid Command";
cin.ignore(10000,'\n');

Sleep(2000); //also what is the purpose of this sleep? get rid of it

system("cls");

break;

}

AmasiVillage();

for example, in the above code, try the added commands.

Try something like this:

bool flag = true;
while(flag)
{
   //display menu here
   cin >> choice;
   switch(choice)
   {
      case 5:
        flag = false;
        break;
      default:
        cout << "Input error.  Try again.  Choices are 1-5 only!";
        break;
    }
}

This gets rid of the recursive call and allows input as long as desired, whether it is valid or not, unless, of course, the invalid input isn't an int. If the invalid input isn't an int then you will need to clear the stream before proceeding, as ninjaneer has demonstrated.

also after you ignore, remember to cin.clear(); to reset the flags on your instream!

ok thanks guys

ok thanks guys

click solved! click solved! :)

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.