Mastermind program problem with iterator

This is a discussion on Mastermind program problem with iterator within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm writing a mastermind program in c++. In mastermind you guess a 4 digitcode and the computer gives two ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    1

    Mastermind program problem with iterator

    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();
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    When the while loop ends, it will be, by definition, lista.end(). And the end of a container is not incrementable (since it is, well, the end of the container).

    (I mean the while loop directly above where you try to move forward 10 times.)

    (And when you put a new score in the list, don't you want to delete the bottom, not the person who was in the spot you just earned?)

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    You are erasing whatever the iterator "it" is pointing at, making "it" a null pointer, and then you try to use it. At least that's what it seems, but it's kinda hard to read through all that. Please use proper identation when you post code.

    Also, that way of saving scores is limited to 10, and confusing. Why don't you just load all the scores from a file, store them in a vector, update the vector after you play the game, sort it, and then save it? That way you would just check the size of the vector and it would work for everything.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    	inFil.open("C:\\Documents and Settings\\Philip\\Skrivbord\\Läxor\\Highscore.txt");
    There's no need to specify a full pathname. It makes it annoying to port. You can just say
    Code:
    inFil.open("Highscore.txt");
    and it will use the current working directory, which is probably what you want.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 10:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 01:34 AM
  3. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 01:10 AM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  5. Problem with Program not Quitting
    By Unregistered in forum Windows Programming
    Replies: 20
    Last Post: 06-11-2002, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21