small problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 158
Reputation: cam875 is an unknown quantity at this point 
Solved Threads: 3
cam875 cam875 is offline Offline
Junior Poster

small problem

 
0
  #1
Jun 26th, 2008
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();
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: small problem

 
0
  #2
Jun 27th, 2008
  1. void AmasiVillage()
  2. {
  3. CharacterLocationID = 1;
  4. system("cls");
  5. cout << "\n";
  6. cout << "\n";
  7. cout << "\n";
  8. cout << "\n";
  9. cout << " Amasi Village\n";
  10. cout << "\n";
  11. cout << " 1)Travel to Koru\n";
  12. cout << " 2)Travel to Reya\n";
  13. cout << " 3)Maktor Ride to Drasin Citadel\n";
  14. cout << " 4)Character Options\n";
  15. cout << " 5)Exit Game\n";
  16. cout << "\n";
  17. cout << " Enter an Option: ";
  18. int choice;
  19. cin >> choice;
  20. if (choice == 1)
  21. Koru();
  22. if (choice == 2)
  23. Reya();
  24. if (choice == 3)
  25. DrasinCitadel();
  26. if (choice == 5)
  27. ;
  28. else
  29. system("cls");
  30. cout << "\n Error: Invalid Command";
  31. Sleep(2000);
  32. system("cls");
  33. AmasiVillage();
  34. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 158
Reputation: cam875 is an unknown quantity at this point 
Solved Threads: 3
cam875 cam875 is offline Offline
Junior Poster

Re: small problem

 
0
  #3
Jun 27th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: small problem

 
0
  #4
Jun 27th, 2008
Originally Posted by cam875 View Post
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
    {
        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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 158
Reputation: cam875 is an unknown quantity at this point 
Solved Threads: 3
cam875 cam875 is offline Offline
Junior Poster

Re: small problem

 
0
  #5
Jun 27th, 2008
that didn't help, but thanks for trying
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,902
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: small problem

 
0
  #6
Jun 27th, 2008
Post your new code and problems. Saying: 'it still doesn't work' doesn't give us enough info to help you
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 158
Reputation: cam875 is an unknown quantity at this point 
Solved Threads: 3
cam875 cam875 is offline Offline
Junior Poster

Re: small problem

 
0
  #7
Jun 27th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: small problem

 
0
  #8
Jun 27th, 2008
try this
  1. void AmasiVillage() {
  2. CharacterLocationID = 1;
  3. system("cls");
  4. cout << endl << endl << endl << endl;
  5. cout << " Amasi Village" << endl << endl;
  6. cout << " 1)Travel to Koru" << endl;
  7. cout << " 2)Travel to Reya" << endl;
  8. cout << " 3)Maktor Ride to Drasin Citadel" << endl;
  9. cout << " 4)Character Options" << endl;
  10. cout << " 5)Exit Game" << endl << endl;
  11. cout << " Enter an Option: ";
  12. int choice;
  13. cin >> choice;
  14. switch (choice) {
  15. case 1:
  16. Koru();
  17. break;
  18. case 2:
  19. Reya();
  20. break;
  21. case 3:
  22. DrasinCitadel();
  23. break;
  24. case 5:
  25. return;
  26. default:
  27. cout << "Error: Invalid Command";
  28. Sleep(2000);
  29. system("cls");
  30. break;
  31. }
  32. AmasiVillage();
  33. }
Last edited by ivailosp; Jun 27th, 2008 at 2:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 25
Reputation: Shaun32887 is an unknown quantity at this point 
Solved Threads: 2
Shaun32887 Shaun32887 is offline Offline
Light Poster

Re: small problem

 
0
  #9
Jun 27th, 2008
Originally Posted by cam875 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 56
Reputation: ninjaneer is an unknown quantity at this point 
Solved Threads: 6
ninjaneer ninjaneer is offline Offline
Junior Poster in Training

Re: small problem

 
0
  #10
Jun 27th, 2008
Originally Posted by cam875 View Post
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:

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

What are the circumstances surrounding the infinite loop phenomenon? Does it seem to happen when a specific item is selected?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC