Hello, I'm writing a mastermind program in c++. In mastermind you guess a 4 digitcode and the computer gives two answers after each guess:
Gold: Right digit in the right place
Silver: The digit is in the code but it the wrong place
The program runs and the actual game works fine but there should also be a top 10 highscorelist. And here i get a problem when i want to erase the 11th score and then save it too a file. I get an error that the iterator is not incrementable.
Any ideas?
P.S I've written some comments to show what the program does and where it goes wrong.
Code:
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <conio.h>
#include <ctime>
#include <string>
using namespace std;
struct Person
{
string name;
string tries;
};
void play(list<Person>& lista);
void highscore(list<Person> lista);
int main()
{
Person person;
list<Person> lista;
ifstream inFil; //loads highscorelist
inFil.open("C:\\Documents and Settings\\Philip\\Skrivbord\\Läxor\\Highscore.txt");
if(!inFil)
{
cout << "Highscorefile can't be opened";
exit(1);
}
while(getline(inFil, person.name))
lista.push_back(person);
inFil.close();
int menu;
cout <<" ============================ 1. PLAY ============================ \n";
cout <<" ========================== 2. Highscores ========================= \n ";
cin >> menu;
if(menu==1) //choice between playing and looking at the highscore
play(lista);
if(menu==2)
highscore(lista);
cin.ignore();
getchar();
return(0);
}
void play(list<Person>& lista)
{
Person person;
int answer[4], guess[4], copyanswer[4], i, gold=0, silver=0, a=0, k=0, j=0;
srand(time(NULL));
for(i=0; i<4; i++)
answer[i]=rand()%9+1; // randomises the answer
while(gold!=4)
{
for(i=0; i<4; i++)
cout << answer[i]; //cheat for now to see the answer
silver=0;
k=0;
gold=0;
a=a+1;
cout << "========== Try " << a << "======== " << endl;
cout << "Type in your guess, seperate each number with a space: ";
cin >> guess[0] >> guess[1] >> guess[2] >> guess[3]; //spara gissning
for(i=0 ; i<4 ; i++)
copyanswer[i] = answer[i];
// everything gets changed to 13 so the same digit doesn't get 2 hits
for(i=0; i<4; i++)
{
if(copyanswer[i]==guess[i]) //check for gold
{gold=gold+1;
copyanswer[i] = 13;
}
}
for(i=0; i<4; i++) //check for silver
{
for(j=0; j<4; j++)
{
if(copyanswer[i]==guess[j])
{
silver=silver+1;
guess[j]= 13;
copyanswer[i] =13;
}
}
}
if(gold==4)
{
cout << "CONANANAGRATAS YOU WERE CORRECT!!!!!" << endl;
cin.ignore();
}
else
{ cout << " Gold = " << gold << " and silver = " << silver << endl;
cin.ignore();
}
}
cout << "Maybe you have reached the highscore, please type in your name: "; // Gets names into the highscore list
getline(cin, person.name);
person.tries=a;
lista.push_back(person);
list <Person>::iterator it=lista.begin()++; //sorts the list after number of tries
list <Person>::iterator it2=lista.begin();
while(it!=lista.end())
{
it2=lista.begin();
while(it2!=it && (*it2).tries < (*it).tries)
{
it2++;
}
lista.insert(it2,*it);
lista.erase(it++);
}
for(int c = 1; c < 11; c++) //HERE SOMETHING GOES WRONG!!!
{
it++;
}
lista.erase(it); //should always be 10 highscores before the program starts
ofstream utFil;
utFil.open("C:\\Documents and Settings\\Philip\\Skrivbord\\Läxor\\Highscore.txt"); //saves the highscorelist
if(!utFil)
{
cout << "Highscore file can't be opened";
exit(1);
}
for(it = lista.begin(); it != lista.end(); it++)
utFil << (*it).name << '\t' << (*it).tries << endl;
utFil.close();
}
void highscore(list<Person> lista)
{
Person person;
int a=1;
cout << " ================ HIGHSCORE ============== ";
cout << "\n Lista:\n";
list<Person>::iterator it;
for(it = lista.begin(); it !=lista.end(); it++)
{
cout << a << '\t' << (*it).name << '\t' << (*it).tries << endl;
a=a+1;
}
cin.ignore();
}