943,754 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3787
  • C++ RSS
May 11th, 2005
0

Help function not returning correct value

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. int hangman::checkstatus()
  2. {
  3. for ( int i = 0; i < 5; i++ )
  4. {
  5. if (encryption[i] == '*' )
  6. {
  7. return i;
  8. cout << i;
  9. break;
  10. }
  11. }
  12.  
  13. if ( i == 4 )
  14. {
  15. cout<<"You win!"<<endl;
  16.  
  17. }
  18. return 0;
  19. }

Hello guys, this function always returns a '0' and can't figure out why...... Perhaps someone could shread some light into the situation? It's ment to check if an element in the array is = to '*' and if so return that number, else it will assume i == 4 (gone through the array and now hit the end) and return a '0' in which case main as an if statment asking this.........

here is main

C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. char guess;
  4. hangman game1;
  5.  
  6. cout << "Welcome to hangman alpha ver 1.2 " ;
  7. cout <<"You start off with : "<<game1.getlives() << " lives \n " << endl;
  8.  
  9. cout << game1.checkstatus();
  10.  
  11. while((game1.getlives() !=0 ) && (game1.checkstatus() > 0))
  12. {
  13. cout << game1.getencrption() <<endl;
  14.  
  15. cin >> guess ;
  16.  
  17. game1.getletter(guess);
  18.  
  19.  
  20. }
  21.  
  22.  
  23.  
  24. return 0;
  25. }

well have fun! its been bugging me for hours!
Similar Threads
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
May 11th, 2005
0

Re: Help function not returning correct value

Quote originally posted by Acidburn ...
int hangman::checkstatus()
{
   for ( int i = 0; i < 5; i++ )
   {
      if (encryption[i] == '*' )
      {
      return i;
      cout << i;
      break; 
      }
   }

   if ( i == 4 ) 
   {
   cout<<"You win!"<<endl;

   }
   return 0;
}
Maybe you can tell me how one can return (jump) out of this function and then execute:

C++ Syntax (Toggle Plain Text)
  1. ..
  2. cout << i;
  3. break;
  4. ..
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 11th, 2005
0

Re: Help function not returning correct value

I would imagine that you initialize encryption with asterisks, right? So let's walk through the execution with that assumption in mind:
C++ Syntax (Toggle Plain Text)
  1. for ( int i = 0; i < 5; i++ )
You create i and set it to 0. Since i is less than 5, the body of the loop is executed.
C++ Syntax (Toggle Plain Text)
  1. if (encryption[i] == '*' )
Since encryption was filled with asterisks, this test is true as the first character is indeed '*'. The body of the condition is executed:
C++ Syntax (Toggle Plain Text)
  1. return i;
0 is returned.

You should also be aware that this is incorrect:
C++ Syntax (Toggle Plain Text)
  1. if ( i == 4 )
Since i was declared in the for loop, it only exists until the end of the loop. So any conforming compiler will fail to compile this function because i doesn't exist at this point.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 12th, 2005
0

Re: Help function not returning correct value

hello, I thought I best post the entire code:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. class hangman
  8. {
  9. public:
  10. hangman();
  11.  
  12. char * getname();
  13. char * getencrption();
  14. void getletter(char letter);
  15.  
  16. void looselife();
  17.  
  18. int getlives();
  19. int checkstatus();
  20.  
  21. private:
  22.  
  23. char name[20];
  24. char encryption[20];
  25. int lives;
  26. int total;
  27.  
  28. };
  29.  
  30. hangman::hangman()
  31. {
  32. strcpy(name,"John");
  33. strcpy(encryption, "****");
  34. lives = 5;
  35. total = 4;
  36. }
  37.  
  38. char * hangman::getname()
  39. {
  40. return name;
  41. }
  42.  
  43. char * hangman::getencrption()
  44. {
  45. return encryption;
  46. }
  47.  
  48. void hangman::looselife()
  49. {
  50. --lives;
  51. cout << "You now have "<< lives << " lives remaining "<<endl;
  52. }
  53.  
  54. void hangman::getletter(char letter)
  55. {
  56. for (int i = 0 ;i < 4 ; i++)
  57. {
  58. if( letter ==name[i] )
  59. {
  60. encryption[i] = name[i];
  61.  
  62. break;
  63. }
  64. }
  65.  
  66. if (i == 4)
  67. {
  68. cout << "not found" <<endl;
  69. looselife();
  70. }
  71. }
  72.  
  73. int hangman::getlives()
  74. {
  75. return lives;
  76. }
  77.  
  78. int hangman::checkstatus()
  79. {
  80. int i;
  81.  
  82. for ( i = 4; i > 0; i--)
  83. {
  84. if (encryption[i] == '*' )
  85. {
  86. return i;
  87.  
  88. break;
  89. }
  90. }
  91.  
  92. if ( i == 0 )
  93. {
  94. cout<<"You win!"<<endl;
  95. }
  96. return 0;
  97. }
  98.  
  99. int main()
  100. {
  101. char guess;
  102. hangman game1;
  103.  
  104. cout << "Welcome to hangman alpha ver 1.2 " ;
  105. cout <<"You start off with : "<<game1.getlives() << " lives \n " << endl;
  106.  
  107. cout << game1.checkstatus();
  108.  
  109. while(game1.getlives() !=0 )
  110. {
  111. cout << game1.getencrption() <<endl;
  112.  
  113. cin >> guess ;
  114.  
  115. game1.getletter(guess);
  116.  
  117.  
  118. }
  119. if (game1.getlives() == 0)
  120. {
  121. cout << "You've lost the game!" <<endl;
  122. }
  123.  
  124. return 0;
  125. }

it still doesnt work and can't figure it out! If a player guess correctly it doesnt finish the game.....
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
May 12th, 2005
0

Re: Help function not returning correct value

This line (line 66 ) won't compile for me because i only exists in the scope of the for loop above it..

if (i == 4)

Also, line 88 - the break doesn't make any sense - you'll never get there - once you do the return, the entire function ends.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 12th, 2005
0

Re: Help function not returning correct value

Quote originally posted by winbatch ...
This line (line 66 ) won't compile for me because i only exists in the scope of the for loop above it..

if (i == 4)

Also, line 88 - the break doesn't make any sense - you'll never get there - once you do the return, the entire function ends.
I think they're really waiting for you to post back the correct working code ;-)
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 12th, 2005
0

Re: Help function not returning correct value

unlikely. I've got enough questions waiting for others to answer...
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 12th, 2005
0

Re: Help function not returning correct value

C++ Syntax (Toggle Plain Text)
  1.  
  2. int hangman::checkstatus()
  3. {
  4. int i;
  5.  
  6. for ( i = 4; i > 0; i--)
  7. {
  8. if (encryption[i] == '*' )
  9. {
  10. return i;
  11. }
  12. }
  13.  
  14. if ( i == 0 )
  15. {
  16. cout<<"You win!"<<endl;
  17. }
  18. return 0;
  19. }

Hello , here's the fixed verision..... however since an array gets its indexs at '0' then I dont think implmenting it this way is gonna work....anyone get any beter ideas?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
May 12th, 2005
0

Re: Help function not returning correct value

This important part looks like it got lost.
   while ( game1.getlives() != 0 && game1.checkstatus() )

This is how I might write checkstatus.
C++ Syntax (Toggle Plain Text)
  1. int hangman::checkstatus()
  2. {
  3. int i;
  4. for ( i = 0; encryption[i] != '\0'; ++i )
  5. {
  6. if ( encryption[i] == '*' )
  7. {
  8. return 1;
  9. }
  10. }
  11. cout<<"You win!"<<endl;
  12. return 0;
  13. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: MP3 Player
Next Thread in C++ Forum Timeline: Search Utility





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC