"Jump to Case Label"

Status
Niet open voor verdere reacties.

555Martijn

Gebruiker
Lid geworden
15 jun 2008
Berichten
163
Code:
// Created by Martijn for learning purposes.
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    SetConsoleTitle(TEXT("4Crowns"));
    
    int choice;
    
    for (;;){
    do {
    menu:
    cout << "\t\t\t================================" << endl;
    cout << "\t\t\t Main Menu" << endl;
    cout << "\t\t\t================================" << endl;    
    cout << "\t\t\t Menu Options: " << endl;
    cout << "\t\t\t 1 - Fight!" << endl;
    cout << "\t\t\t 2 - View Stats" << endl;
    cout << "\t\t\t 3 - Purchase Equipment" << endl;
    cout << "\t\t\t 4 - Quit Game" << endl;
    cout << "\t\t\t================================" << endl;
    cout << "\t\t\t\n"; // Spacer
    cout << "\t\t\t Option: "; cin >> choice;
    } while ( choice < '1' || choice > '4' && choice != 'q');
   if (choice == 'q') break;
   switch (choice) {
    
    
    // Fight!        
    case '1':
         
         int bchoice;    
    system("CLS");     
    cout << "\t\t\t==============================" << endl;
    cout << "\t\t\t Battle Menu" << endl;
    cout << "\t\t\t==============================" << endl;
    cout << "\n\t\t\t Who do you want to battle?" << endl;
    cout << "\t\t\t Crown1 - Level 2 (melee)" << endl;
    cout << "\t\t\t Option: "; cin >> bchoice;
    
    if ( bchoice == 'Crown1' | 'crown1' ) {goto Crown1;}
    else {goto menu;}
    // Crown1
    Crown1:
    int Crown1HP = 80;      
    
    system("CLS");
    cout << "Crown1 has "<< Crown1HP << " hitpoints." << endl;
    system("PAUSE");

    char answer;
    // View Stats
    case '2':
         cout << "empty" << endl;
         
               
    // Purchase Equipment
    case '3':
         cout << "empty" << endl;
          
    default:
            cout << "empty" << endl;
            
    return 0;
}}}

Sorry van de nog messy code, maar ik was bezig met het vecht systeem, en ik compile weer is om te testen, en nu heb ik iets kapot gemaakt waar ik niet mee bezig was 0_o.
 
goto en label zijn structuren die vantegenwoordig niet echt als 'proper' worden aanzien. Tevens zijn ze niet ongelooflijk bevorderend om je code te kunnen volgen.

Als het gaat over de leesbaarheid van je routine, gebruik je (meestal) beter functies ipv goto. In jouw geval loopt er hier en daar ook wat mank met de opzet van je luscode.

Ook vergelijk je of choice (een integer) verschilt van q (een char), terwijl invoer van een letter wanneer je een cijfer verwacht bovendien je programma laat vastlopen.

Ik heb je code wat herschreven, zodat ze exact hetzelfde doet. Ik zeg er wel meteen bij dat ook deze structuur verre van ideaal is voor een game...
Code:
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    SetConsoleTitle(TEXT("4Crowns"));
    
    int choice = 0;
    bool quit = false;
	
    while (!quit) {
		//menu weergeven
		do {
			cin.clear(); //wis eventuele vroegere foute invoer
			system("CLS");
			cout << "\t\t\t================================" << endl
				 << "\t\t\t Main Menu" << endl
				 << "\t\t\t================================" << endl
				 << "\t\t\t Menu Options: " << endl
				 << "\t\t\t 1 - Fight!" << endl
				 << "\t\t\t 2 - View Stats" << endl
				 << "\t\t\t 3 - Purchase Equipment" << endl
				 << "\t\t\t 4 - Quit Game" << endl
				 << "\t\t\t================================" << endl
				 << "\t\t\t\n"
				 << "\t\t\t Option: ";
		} while (!(cin >> choice) || ((choice < 1) && (choice > 4));
		
		switch (choice) {
			case 1: { // Fight!
				do {
					cin.clear(); //wis eventuele vroegere foute invoer
					system("CLS");
					cout << "\t\t\t==============================" << endl
						 << "\t\t\t Battle Menu" << endl
						 << "\t\t\t==============================" << endl
						 << "\n\t\t\t Who do you want to battle?" << endl
						 << "\t\t\t 1. Crown1 - Level 2 (melee)" << endl
						 << endl
						 << "\t\t\t 2. Back to the main menu" << endl
						 << "\t\t\t Option: ";
				} while (!(cin >> choice) || ((choice < 1) && (choice > 2));
				
				if (choice == 1) {
					system("CLS");
					cout << "Crown1 has 80 hitpoints." << endl;
					system("PAUSE");
				}
			} break;
			
			case 2: { // View Stats
				cout << "Stats" << endl;
			} break;
    
			case 3: { // Purchase Equipment
				cout << "equipment" << endl;
			} break;
			
			case 4: { // Quit
				cout << "Quitting..." << endl;
				quit = true;
			} break;
			
			default: { //we never should get here
				cout << "There was a big boo-boo, but we'll just carry on." << endl;
			}
		} // end of choice-switch
	} // end of gameloop
            
    return 0;
}
Ik heb ze nog niet getest, maar vermoed niet dat ik me zoveel misschreven heb ;)
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan