Function returns unknown error to me.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1314 Views - Last Post: 26 April 2012 - 02:29 PM Rate Topic: -----

#1 curedtc   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-April 12

Function returns unknown error to me.

Posted 25 April 2012 - 01:58 PM

I posted a couple of topics before and got them removed... Hopefully this is not the case.
I merely want a discussion rather than you giving me code.

This is what I have so far. The main code has the function name with the 2 variables it is passing over to it.


void wordGuess::checkLetter(std::string&input,char &guess)
{
	int w=0;  
	
	if(crs)
      crs->setPosition(xcd,ycd);
		if(cl);
	  	switch(clr)
		{
		case 1: cl->setForeground(Black); break;
		default: case 2: cl->setForeground(White); break;
		case 3: cl->setForeground(Blue); break;
		case 4: cl->setForeground(Green); break;
		case 5: cl->setForeground(Aqua); break;
		case 6: cl->setForeground(Red); break;
		case 7: cl->setForeground(Purple); break;
		case 8: cl->setForeground(Yellow); break;
		}

	  
while(w<input.size())
{
	  cout << "Enter your guess: ";
      cin >> guess;
	  for(int i=0;i<=10;i++)
	  {
		  if(guess==input[i])
		  {
			cout<<"You got one";
		    qin.get();
		    w++;
		    if(crs)
			crs->setPosition(xcd,ycd);
		    cin>>guess;
		  

		  }
		  else;
		  cout << "no";
	  }

	}
}



This code is looking at an inputted char in a string and hopes to find it. It is the game of hangman so eventually I hope this function can print out the correct letter and the wrong letters into various positions on screen.

However this function seems to return a horrible "Debug Assertion Failed" I have never seen before.

Any ideas of what to try with this function?

Is This A Good Question/Topic? 0
  • +

Replies To: Function returns unknown error to me.

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Function returns unknown error to me.

Posted 25 April 2012 - 02:07 PM

Please post the contents of the complete message.

I would also recommend that you comment out most of the unecessary code as possible and see if your problem is in that section, or in your actual game logic. The unecessary code I am refurring to is all the "color" code. Leave the "pretty" out until you get the actual game logic working correctly.

Jim

This post has been edited by jimblumberg: 25 April 2012 - 02:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 curedtc   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-April 12

Re: Function returns unknown error to me.

Posted 25 April 2012 - 02:31 PM

The colour bit works fine as it is in many of my functions. The main bit is after that.

And what complete message are you referring to?

Sorry I am a complete beginner really.

Oh I get it, one moment.

Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Function returns unknown error to me.

Posted 25 April 2012 - 02:40 PM

So it's talking about a string subscript out of range, What string variables do you have in that function? The only one I see is input. Where are you accessing this string with an index? What does this variable contain? What is the size of this string?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 curedtc   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-April 12

Re: Function returns unknown error to me.

Posted 25 April 2012 - 02:42 PM

Input is the word the user has entered in another function in that class. I then passed it to this one, along with the char guess which is the "guess" letter they then enter in this function to see if it matches anything.

The game is hangman, so it should find it etc.

The string is any size but I might say "Enter a word no larger than 10" if this creates a solution for example.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Function returns unknown error to me.

Posted 25 April 2012 - 02:49 PM

No your problem is not that the string is larger than 10, your problem is that the string is smaller than 10, remember the error states index out of range (Larger). This 10 is the problem. Wouldn't it be better just to loop thru the string based on it's size? What is the significance of the number 10?

Jim

This post has been edited by jimblumberg: 25 April 2012 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#7 curedtc   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-April 12

Re: Function returns unknown error to me.

Posted 25 April 2012 - 02:59 PM

View Postjimblumberg, on 25 April 2012 - 02:49 PM, said:

No your problem is not that the string is larger than 10, your problem is that the string is smaller than 10, remember the error states index out of range (Larger). This 10 is the problem. Wouldn't it be better just to loop thru the string based on it's size? What is the significance of the number 10?

Jim

There is no significance...Haha, I was just saying that I could make it that long if there was an issue.

The bit where it says 10 was actually input.size()
I changed it to 10 just to test it and clearly copied that version of code.

The error is the same for both sizes.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Function returns unknown error to me.

Posted 25 April 2012 - 03:05 PM

What do you mean by both sizes? You should be using the size of the string not your "magic" number 10. The size of a std::string is returned by the size() member function.

If you are still getting errors post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 curedtc   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-April 12

Re: Function returns unknown error to me.

Posted 25 April 2012 - 03:11 PM

Yes I know it is. :)
while(w<input.size())
{
	  cout << "Enter your guess: ";
      cin >> guess;
	  for(int i=0;i<=input.size();i++)
	  {
		  if(guess==input[i])
		  {
			cout<<"You got one";
		    qin.get();
		    w++;
		    if(crs)
			crs->setPosition(xcd,ycd);
		    cin>>guess;
		  

		  }
		  else;
		  cout << "no";
	  }

	}




That is what it is.

By sizes I meant the for loop size. I merely copied the wrong code. I was trying many different things. 10 is not a magic number. I was just trying it out with my code.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Function returns unknown error to me.

Posted 25 April 2012 - 03:15 PM

So with the above code does your program work? If not try changing the <= in your for loop to a < operator. Remember arrays (this includes strings) sizes , start at zero and end at size -1.




Jim

This post has been edited by jimblumberg: 25 April 2012 - 03:16 PM

Was This Post Helpful? 0
  • +
  • -

#11 curedtc   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-April 12

Re: Function returns unknown error to me.

Posted 25 April 2012 - 03:19 PM

Taking away the = sign certainly took away the error, but I am not sure it worked as such. I shall post again shortly with an altered code.

Thank you very much for your time, but I shall update this topic shortly while I look at what it is now doing.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Function returns unknown error to me.

Posted 25 April 2012 - 03:32 PM

You may also find this explanation of "Magic Numbers" interesting.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 curedtc   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-April 12

Re: Function returns unknown error to me.

Posted 25 April 2012 - 03:38 PM

The current situation is this:
I enter a word.. In this case.. hello.

I can then enter a guess as the function outlines and the guess is correct.
However, I then enter "e" which is also correct and get the "no" output AND the "You got one".

I know I have more to do with the function like building the hangman but I initially need this to work.
Walking before running etc...
Thanks
Posted Image
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Function returns unknown error to me.

Posted 25 April 2012 - 03:44 PM

You have a semicolon after your else statement, try removing it. That should solve some of the problem. I would also recommend that you use braces with your control statements, even one liners. In my opinion that reduces errors and doesn't affect readability.

Jim
Was This Post Helpful? 0
  • +
  • -

#15 curedtc   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-April 12

Re: Function returns unknown error to me.

Posted 25 April 2012 - 03:52 PM

It is rather odd.
You were right to suggest the braces etc. And it seemed to work other than the letter "o" in hello which produced this when entered first.
The code as it stands is this for the whole function
void wordGuess::checkLetter(std::string&input,char &guess)
{
	int w=0;  
	
	if(crs)
      crs->setPosition(xcd,ycd);
		if(cl);
	  	switch(clr)
		{
		case 1: cl->setForeground(Black); break;
		default: case 2: cl->setForeground(White); break;
		case 3: cl->setForeground(Blue); break;
		case 4: cl->setForeground(Green); break;
		case 5: cl->setForeground(Aqua); break;
		case 6: cl->setForeground(Red); break;
		case 7: cl->setForeground(Purple); break;
		case 8: cl->setForeground(Yellow); break;
		}

	  
while(w<input.size())
{
	  cout << "Enter your guess: ";
      cin >> guess;
	  for(int i=0;i<input.size();i++)
	  {
		  if(guess==input[i])
		  {
			cout<<"You got one";
		    qin.get();
		    w++;
		    if(crs)
			crs->setPosition(xcd,ycd);
		    cin>>guess;
		  }
		  else
		  {
			  cout << "no";
		  }
		}

	}

}


Posted Image
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2