dimanche 19 juin 2016

Function is skipping my for loop

I have checked around but can't find a solution to this issue I'm having while practicing c++. I'm just making a simple bulls and cows game. This is just throwaway code with weird names that are somewhere between mastermind and bulls and cows but ignore that. Also the program isn't finished. Basically my breaker_guess() skips over it's for loop and goes right for the error message on the if statement. The maker_input() works though and they're carbon copies of eachother. My guess is that the input buffer isn't empty and so the for-loop is fed a stored integer but I tried clearing it and it didn't solve anything. If anyone can help that would be greatly appreciated.

#include "stdafx.h"
#include "stdlib.h"

using namespace std;

struct scorecard {

string player;
int score;
scorecard(string p, int s) : player(p), score(s) {}
scorecard();

};

scorecard::scorecard() {

    return;

}

class Mastermind {
vector<int>colors{};
vector<int>guess{};
vector<int>bulls{};
string player1;
string player2;
scorecard p1score;
scorecard p2score;
string rules;


public:
void input_names();
void maker_input();
void breaker_guess();
void check_guess();
void check_bulls();
void score_board();
void swap_players();
void start_menu();
//void rules();
//void winner();
int games();
scorecard maker{ player1, 0 };
scorecard breaker{ player2, 0 };
Mastermind();

};

int Mastermind::games() {

    cout << "How many rounds do you want to play?nRounds: ";
int x;
cin >> x;
if (cin.fail()) error("Number of games must be an integer.");
return x;

}

void Mastermind::input_names() {

cout << "Enter name of player one: ";
string s1;
cin >> s1;
if (cin.fail())error("Player name can be any symbol, letter, or number.");
string player1{ s1 };
maker.player = player1;
cout << "Enter name of player two: ";
string s2;
cin >> s2;
if (cin.fail())error("Player name can be any symbol, letter, or number.");
string player2{ s2 };
breaker.player = player2;
cout << "Hello " << player1 << " and " << player2 << "! Let's break some code...n";

}

void Mastermind::maker_input() {

for (int digit; cin >> digit;) {
    if (digit > 9 || digit < 0) error("Code must be between 0 and 9");
    if (colors.size() > 4) break;
    colors.push_back(digit);
}
if (colors.size() < 4) error("Code must be four digits");
}

void Mastermind::breaker_guess() {

for (int digit; cin >> digit;) {
    if (digit > 9 || digit < 0) error("Guess must be between 0 and 9");
    if (guess.size() > 4) break;
    guess.push_back(digit);
}
if (guess.size() < 4) error("Guess must be four digits");
}

void Mastermind::check_bulls() {

for (int i = 0; i < 4; ++i) {
    if (colors[i] == guess[i]) {
        ++breaker.score;
        bulls.push_back(colors[i]);
    }
    else 
        ++maker.score;
}

}


void Mastermind::check_guess() {

for (int t = 0; t < 12; ++t) {
    switch (t) {
    case 0: 
        cout << "Guess: ";
        break;
    case 11: 
        cout << "Last try: ";
        break; 
    default:
        cout << "Guess again: ";
        break;
    }
    breaker_guess();
    check_bulls();
    if (bulls.size() == 4) break;
    else score_board();
}
}

void Mastermind::score_board(){

cout << maker.player << ':' << 't' << maker.score << 'n'
    << breaker.player << ':' << 't' << breaker.score << 'n';

 }

 void Mastermind::swap_players() {

string temp;
temp = breaker.player;
breaker.player = maker.player;
maker.player = temp;

}

void Mastermind::start_menu() {

cout << "Welcome to Mastermind... type s to start, or r for the rules: ";
char input;
cin >> input;
if (cin.fail())error("Menu options are only one letter (ex: 's' for start, 'r' for rules)");
switch (input) {
case 's':
    input_names();
    break;
//case 'r':
    //rules();
default:
    error("You need to enter either 's' or 'r'...");
    start_menu();
}

}

Mastermind::Mastermind() {

p1score.score = 0;
p2score.score = 0;

}

Mastermind mm;

int main() try {

mm.start_menu();
int x = mm.games();
for (int i = 0; i <= x; ++i) {
    cout << "Alright " << mm.breaker.player << ", look away from the screen so "
        << mm.maker.player << " can create their code...nCode: ";
    mm.maker_input();
    cout << "Okay " << mm.breaker.player << ", your turn to guess...n";
    mm.check_guess();

}

system("Pause");
return 0;
}
catch (exception& e) {
cerr << "error: " << e.what() << 'n';

system("Pause");
return 1;
}

/*
int main() try {

vector<int>guess;

int digit;
while (cin >> digit) {
    if (digit > 9 || digit < 0) error("Guess must be between 0 and 9");
    if (guess.size() > 4) break;
    guess.push_back(digit);
}

for (int x : guess)
    cout << x << 'n';

system("Pause");

}
catch (exception& e) {

cerr << "error: " << e.what() << 'n';

system("Pause");

}
*/

Also the code in a comment block was a check to see if it was an issue with the actual function and it all worked good which leads me to believe it is in fact something with the input stream.

Aucun commentaire:

Enregistrer un commentaire