| | |
small problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 158
Reputation:
Solved Threads: 3
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();
}
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();
}
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
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
if (choice == 5)
;
else
{
system("cls");
cout << "\n Error: Invalid Command";
Sleep(2000);
system("cls");
AmasiVillage();
}Note that you are still calling AmasiVillage() recursively, in case of invalid command.
Post your new code and problems. Saying: 'it still doesn't work' doesn't give us enough info to help you
•
•
Join Date: Apr 2008
Posts: 129
Reputation:
Solved Threads: 22
try this 

cpp Syntax (Toggle Plain Text)
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(); }
Last edited by ivailosp; Jun 27th, 2008 at 2:00 pm.
•
•
Join Date: Jun 2008
Posts: 56
Reputation:
Solved Threads: 6
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
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?
![]() |
Similar Threads
- small problem in the string (C++)
- A small problem in the output (C++)
- Processor problem (Motherboards, CPUs and RAM)
- Small problem with Run... command (Windows NT / 2000 / XP)
- Tad small problem (Windows NT / 2000 / XP)
- Small problem with web links? (Windows 95 / 98 / Me)
- Java Game Applet Too Small (Java)
- ** Need Help ** in a small C++ problem (C++)
Other Threads in the C++ Forum
- Previous Thread: error C2677: binary '==' : no global operator found
- Next Thread: read line from file
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






