Jump to content
  • Advertisement
Sign in to follow this  
MadsGustaf

Help to discover my infinity loop

This topic is 4059 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have looked for over 30 min, but i still cant seem to find my mistake.. when i run the program, and enter "1" to add a player, it enters a infinity loop and displays: 00000(m_pNavn, - whatever you entered)
#include <iostream>
#include <string>

using namespace std;

class Spiller
{ 
 public:
  Spiller(const string navn = ""): m_pNavn(navn), m_pNæste(0) {/*empty*/}
  string HentNavn() const { return m_pNavn; }
  Spiller* HentNæste() const { return m_pNæste; }
  void SætNæste(Spiller* næste) { m_pNæste = næste; }
 
 private:
  string m_pNavn;
  Spiller* m_pNæste;
};//slutningen af Spiller classen

class Lobby
{
 friend ostream& operator<<(ostream& os, Lobby& enLobby);
 
 public:
  Lobby(): m_pFørStart(0) {/*EMPTY*/}
  ~Lobby() { SletAlle(); }
  void TilføjSpiller();
  void SletSpiller();
  void SletAlle();
 
 private:
  Spiller* m_pFørStart;
};

void Lobby::TilføjSpiller()
{
 cout << "Skriv navnet på en spiller: ";
 string navn;
 cin >> navn;
 Spiller* pNySpiller = new Spiller(navn);
 
 if (m_pFørStart == 0)
 { 
 m_pFørStart = pNySpiller; 
 }
 else
 {
  Spiller* pIter = m_pFørStart;
  
  while( pIter->HentNæste() != 0 )
   {
    pIter = pIter->HentNæste();
   }
   pIter->SætNæste(pNySpiller);
 }
}

void Lobby::SletSpiller()
{
 if (m_pFørStart == 0) 
  cout << "Lobbyen er tom!\n";
 else
  {
   Spiller* pTemp = m_pFørStart;
   pTemp = (*pTemp).HentNæste();
  delete pTemp;
  }
}

void Lobby::SletAlle()
{
 while( m_pFørStart != 0)
  { 
   SletSpiller();  
  }
}

ostream& operator<<(ostream& os, Lobby& enLobby)
{
 Spiller* pIter = enLobby.m_pFørStart;
 
 os << "Here is who is in the lobby: \n";
 if (pIter == 0)
  os << "The lobby is empty\n";
 else
 {
  while ( pIter != 0)
  {
   os << pIter->HentNavn() << endl;
   os << pIter->HentNæste();
  }
 }
return os;
}
int main()
{
 Lobby MinLobby;
 int valg;
 
 do
 {
  cout << MinLobby;
  cout << "Her er dine valg\n";
  cout << "0 - Luk programmet\n";
  cout << "1 - Tilføj en spiller\n";
  cout << "2 - Slet en spiller fra lobbyen\n";
  cout << "3 - Slet hele lobbyen\n";
  cout << endl << "Hvad vil du foretage dig: ";
  cin >> valg;
  
  switch(valg)
  {
   case 0: cout << "Good-bye.\n"; break;
   case 1: MinLobby.TilføjSpiller(); break;
   case 2: MinLobby.SletSpiller(); break;
   case 3: MinLobby.SletAlle(); break;
   default: cout << "Det er ikke et lovligt valg\n"; 
  }
 }while (valg != 0);
return 0;
}
  
thanks for your help! it is greatly appriciated!

Share this post


Link to post
Share on other sites
Advertisement
Why not just use the debugger, halt execution during the infinite loop, and see where it is?

Share this post


Link to post
Share on other sites
Instead of LOOKING at the code, use a debugger and RUN the code. Took me about 2 minutes.

while ( pIter != 0)
{
os << pIter->HentNavn() << endl;
os << pIter->HentNaste();
}

The while loop starts on line 86. Take a closer look at the exit condition, and think about how the exit condition is met.

Share this post


Link to post
Share on other sites
Thanks all! i fixed it now, i thought it had something to do with the constructor or the TilføjSpiller() function.
i edited the while loop into:

while ( pIter != 0)
{
os << pIter->HentNavn() << endl;
pIter = pIter->HentNæste();
}

How would i halt it? do i use the breakpoint function? (that im also very unfamiliar with..)

Share this post


Link to post
Share on other sites
Quote:
Original post by MadsGustaf
How would i halt it? do i use the breakpoint function? (that im also very unfamiliar with..)

Under Visual Studio, you push the "pause" button.

Share this post


Link to post
Share on other sites
If you're smart enough to use std::string, you should be smart enough to use standard library containers as well; don't make your own linked list.

For example, with std::list (but consider that other container types might be more appropriate):


#include <iostream>
#include <string>
#include <list>

using namespace std;

class Spiller
{
public:
Spiller(const string navn = ""): m_pNavn(navn) {}
string HentNavn() const { return m_pNavn; }

private:
string m_pNavn;
};

class Lobby
{
friend ostream& operator<<(ostream& os, Lobby& enLobby);

public:
void TilføjSpiller();
void SletSpiller();
void SletAlle();

private:
std::list<Spiller> m_Spillere;
};

void Lobby::TilføjSpiller()
{
cout << "Skriv navnet på en spiller: ";
string navn;
cin >> navn;
m_Spillere.push_back(Spiller(navn));
}

void Lobby::SletSpiller()
{
if (m_Spillere.empty())
cout << "Lobbyen er tom!\n";
else
m_Spillere.erase(m_Spillere.begin());
}

void Lobby::SletAlle()
{
m_Spillere.clear();
}

ostream& operator<<(ostream& os, Lobby& enLobby)
{
std::list<Spiller>::iterator it = enLobby.m_Spillere.begin();
std::list<Spiller>::iterator end = enLobby.m_Spillere.end();

os << "Here is who is in the lobby: \n";
if (it == end)
os << "The lobby is empty\n";
else
{
for (; it != end; ++it)
{
os << it->HentNavn() << "\n"; // don't use endl in operator<< overloads;
// it's expected that the calling code has full control over when the stream
// is flushed.
}
}
return os;
}

int main()
{
Lobby MinLobby;
int valg;

do
{
cout << MinLobby;
cout << "Her er dine valg\n";
cout << "0 - Luk programmet\n";
cout << "1 - Tilføj en spiller\n";
cout << "2 - Slet en spiller fra lobbyen\n";
cout << "3 - Slet hele lobbyen\n";
cout << endl << "Hvad vil du foretage dig: ";
cin >> valg;

switch(valg)
{
case 0: cout << "Good-bye.\n"; break;
case 1: MinLobby.TilføjSpiller(); break;
case 2: MinLobby.SletSpiller(); break;
case 3: MinLobby.SletAlle(); break;
default: cout << "Det er ikke et lovligt valg\n";
}
} while (valg != 0);
}



(Also, consider using more than a single space of indentation o_O)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!