std::vector out of range problem



  • Hallo Leute..Ich habe vor kurzem angefangen mit Class zu arbeiten und wollte ein MasterMind Programm machen, es passieren keine Compiler fehler und alles läuft auch so wie es soll außer, dass nach den Eingaben vom Spieler, das Programm stoppt und mir den "Out of Range" Fehler beim Vector zeigt.

    Hier der Code...Hoffe er soweit ok, Tipps nehme Ich gerne noch an 🙂

    Code:

    #include "stdafx.h"
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <windows.h>
    #include <time.h>

    int main();
    short versuche = 12; // Number of Trys he has
    bool alleRichtig = false;

    class MasterMind {
    private:

    std::vector<unsigned short> spielerzahlen{ // Number Input from Player
    	{0,0,0,0}
    };
    
    std::vector<unsigned short> computerzahlen{ // Generated Number from Computer
    	{0,0,0,0}
    };
    
    
    short richtigeStelle = 0; // If the Number is on the right position from the Player
    short richtigeZahl = 0; // If the Number is right but its not on the right position
    bool _playerWin = false; // Checking if player Won (Versuche > 0) && Numbers from the Computer Identical?
    bool _computerWin = false; // Checking if Compuer Won (Versuche == 0)?
    

    public:

    bool _askExplain = false; // Checking if the Player need to call the Explain Function
    
    void _anleitung() { // Explain Game and Features
    
    	std::cout << "\n\tHallo Sterblicher, das Spiel funktioniert so:"
    		<< "\n\tDer Computer denkt sich 4 Zahlen aus ( von 1 - 6 ), du gebst dann 4 Zahlen ein und versuchst die Zahlen vom Computer zu erraten!"
    		<< "\n\tDabei hast du 12 Versuche mit Jokers! ( IN ENTWICKLUNG )"
    		<< "\n\tDu kannst ausserdem die Schwierigkeit erhoehen mit mehr Zahlen ( IN ENTWICKLUNG )"
    		<< "\n\tEin kleiner Hinweis, mit 9 beendest du das Spiel :)!"
    		<< "\n\tViel Spass und viel Erfolg!" << std::endl;
    }
    
    void _eingabeSpielerzahlen() {
    
    	for (unsigned short i = 0; i < 4; i++) {
    		std::cout << "\n\tBitte gebe deine " << (i + 1) << ": Zahl ein :: ";
    		std::cin >> spielerzahlen.at(i);
    		if (spielerzahlen.at(i) > 6 || spielerzahlen.at(i) < 1) { // If Input is not valid
    			
    			if (spielerzahlen.at(i) > 6) // Print Input is too big
    				std::cout << "\n\tZahl zu gross!"; i--;
    
    			if (spielerzahlen.at(i) < 1) // Print Input is too small
    				std::cout << "\n\tZahl zu klein!"; i--;
    		}
    	}
    }
    
    void _generateComputerzahlen() {
    	srand(time(0));
    
    	for (unsigned short i = 0; i < 4; i++) {
    		computerzahlen.at(i) = rand() % 6 + 1;
    		
    		for (unsigned short j = i, y = i; j < y; j++) {
    			if (computerzahlen.at(0) == computerzahlen.at(1) || computerzahlen.at(2) || computerzahlen.at(3))
    				i--;
    			
    			if (computerzahlen.at(1) == computerzahlen.at(2) || computerzahlen.at(3))
    				i--;
    
    			if (computerzahlen.at(2) == computerzahlen.at(3))
    				i--;
    		}
    
    	}
    	/*
    	for (unsigned short a = 0; a < 4; a++) {
    		std::cout << "\n\tZahlC " << (a + 1) << ": " << computerzahlen.at(a);
    	}
    	*/
    }
    
    void _gameLogic() {
    
    	versuche--;
    
    	for (unsigned short i = 0; i < 4; i++) {
    		if (spielerzahlen.at(i) == computerzahlen.at(i))
    			richtigeStelle++;
    
    		for (unsigned short j = spielerzahlen.at(i); j != 0; j--) {
    			if (spielerzahlen.at(i) == computerzahlen.at(j))
    				richtigeZahl++;
    		}
    
    	}
    
    	for (unsigned short i = 0, j = 0; i < 4; i++) {
    		if (spielerzahlen.at(i) == computerzahlen.at(i))
    			j++;
    
    		if (j == 4)
    			alleRichtig = true; _playerWin = true;
    	}
    
    	if (versuche == 0)
    		_computerWin = true;
    
    }
    
    	void _ausgabeLogicErgebnis() {
    
    		std::cout << "\n\tZahlen werden verglichen..";
    		std::cout << "\n\tEs sind " << richtigeStelle << " Zahlen an der richtigen Stelle";
    		std::cout << "\n\tEs sind " << richtigeZahl << " Zahlen richtig";
    
    	}
    
    	void _printWin() {
    		bool _playAgain = false;
    		if (versuche > 0 && _playerWin == true) {
    			std::cout << "\n\tHerzlichen Glueckwuensch!! DU HAST GEWONNEN YEAH!\n\tWillst du nochmal spielen? ( 1 oder 0 )"
    			<< "\n\tEingabe :: ";
    		std::cin >> _playAgain;
    		if (_playAgain == true) {
    			
    			versuche = 12;
    
    			for (short i = 0; i < 4; i++) {
    				spielerzahlen.at(i) = 0;
    				computerzahlen.at(i) = 0;
    			}
    
    			alleRichtig = false;
    			 richtigeStelle = 0;
    			 richtigeZahl = 0;
    			 _playerWin = false;
    			 _computerWin = false;
    			
    			 system("cls");
    			 main();
    		}
    		else if (_playAgain == false)
    			std::cout << "\n\tDanke fuers Spielen!\n\t"; system("pause"); exit(0);
    
    	}
    	
    }
    
    
    
    };
    

    int main()
    {
    SetConsoleTitleA("MasterMindGame EXTREME");
    system("color 74"); // Set Color in Console || Background, Text
    std::stringstream _spielVersion;
    _spielVersion << "V0.1"; // Set Version Number

    MasterMind Game;
    
    // <-- Start Game -->
    
    // <-- Start Explain Game 
    std::cout << "\tWillkommen beim MasterMind Game EXTREME\n\tSpielversion :: " << _spielVersion.str();
    std::cout << "\n\tBrauchst du eine Anleitung fuer das Spiel? Wir spielen naemlich mit Zahlen statt mit Farben!";
    std::cout << "\n\tBitte geben 1 oder 0 fuer deine Entscheidung ein :: ";
    std::cin >> Game._askExplain;
    
    if (Game._askExplain == true) { // Call Function 'Explain' if Input == 1
    	std::cout << "\n\tAnleitung wird angezeigt:";
    	Game._anleitung();
    }
    
    if (std::cin.fail()) { // If Input fails..
    	std::cout << "\n\tFehler bei der Eingabe. Fehlercode :: x104\n\t";
    	system("pause");
    	exit(0); // End Program
    }
    // End Explain Game -->
    
    std::cout << "\n\t<-- SPIEL BEGINNT -->";
    Game._generateComputerzahlen(); // Computer get his Numbers
    
    do {
    	std::cout << "\n\tErste Spielerzahl Eingabe";
    	Game._eingabeSpielerzahlen();
    	Game._gameLogic();
    	Game._ausgabeLogicErgebnis();
    } while (versuche != 0 && alleRichtig == false);
    
    Game._printWin();
    
    
    system("pause");
    return 0;
    

    }



  • @drawmaster1212 sagte in std::vector out of range problem:

    if (spielerzahlen.at(i) > 6) // Print Input is too big
    	std::cout << "\n\tZahl zu gross!"; i--;
    
    if (spielerzahlen.at(i) < 1) // Print Input is too small
    	std::cout << "\n\tZahl zu klein!"; i--;
    

    Schaut zwar nett aus, aber in Wirklichkeit steht da

    if (spielerzahlen.at(i) > 6)
     	std::cout << "\n\tZahl zu gross!";
    i--;
     
    if (spielerzahlen.at(i) < 1)
     	std::cout << "\n\tZahl zu klein!";
    i--;
    

    i wird also immer zwei Mal dekrementiert. Wenn Du Block meinst, dann schreib auch Block:

    if(42) {
    	foo();
    	bar();
    	i--;
    }
    

    @drawmaster1212 sagte in std::vector out of range problem:

    for (unsigned short i = 0; i < 4; i++) {
    	computerzahlen.at(i) = rand() % 6 + 1;
    		
    	for (unsigned short j = i, y = i; j < y; j++) {
    		if (computerzahlen.at(0) == computerzahlen.at(1) || computerzahlen.at(2) || computerzahlen.at(3))
    			i--;
    			
    		if (computerzahlen.at(1) == computerzahlen.at(2) || computerzahlen.at(3))
    			i--;
    
    		if (computerzahlen.at(2) == computerzahlen.at(3))
    			i--;
    	}
    }
    

    Ich weiß nicht, was das werden soll, aber if(a == b || c || d) ist nicht dasseble wie if(a == b && a == c && a == d).

    @drawmaster1212 sagte in std::vector out of range problem:

    for (unsigned short i = 0; i < 4; i++) {
    	if (spielerzahlen.at(i) == computerzahlen.at(i))
    		richtigeStelle++;
    
    	for (unsigned short j = spielerzahlen.at(i); j != 0; j--) {
    		if (spielerzahlen.at(i) == computerzahlen.at(j))
    			richtigeZahl++;
    	}
    }
    

    unsigned short j = spielerzahlen.at(i) Du willst doch nicht wirklich in diesem Fall eine Benutzereingabe als Startwert für j haben??

    Weiters:

    • <time.h> heißt in C++ <ctime> und die Funktionen liegen im Namensraum std.
    • Wozu die forward declaration von main()? Ach so, Du rufst main() in MasterMind::_printWin() rekursiv auf. Das ist laut Standard nicht erlaubt und damit Dein Code kein gültiges C++-Programm.
    • Wozu globale Variablen?
    • Mische nicht deutsche mit englischen Bezeichnern.
    • Markiere bitte beim posten Deinen Code als solchen: Code markieren und auf </> klicken. Alternativ schreibe ``` über und ``` unter Deinen Code.
    • Zur Frage allgemein: Benutze einen Debugger.


  • Ah Danke ^^ Das mit dem unsigned short j war gar nicht so gedacht..war da wohl nicht konzentriert ^^
    Danke dir habe meine Fehler dann selber auch nochmal gefunden 🙂


Anmelden zum Antworten