Help function not returning correct value

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Help function not returning correct value

 
0
  #1
May 11th, 2005
  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

  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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Help function not returning correct value

 
0
  #2
May 11th, 2005
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:

  1. ..
  2. cout << i;
  3. break;
  4. ..
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 716
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help function not returning correct value

 
0
  #3
May 11th, 2005
I would imagine that you initialize encryption with asterisks, right? So let's walk through the execution with that assumption in mind:
  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.
  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:
  1. return i;
0 is returned.

You should also be aware that this is incorrect:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Help function not returning correct value

 
0
  #4
May 12th, 2005
hello, I thought I best post the entire code:

  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.....
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Help function not returning correct value

 
0
  #5
May 12th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Help function not returning correct value

 
0
  #6
May 12th, 2005
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 ;-)
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Help function not returning correct value

 
0
  #7
May 12th, 2005
unlikely. I've got enough questions waiting for others to answer...
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Help function not returning correct value

 
0
  #8
May 12th, 2005
  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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,349
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help function not returning correct value

 
0
  #9
May 12th, 2005
This important part looks like it got lost.
   while ( game1.getlives() != 0 && game1.checkstatus() )

This is how I might write checkstatus.
  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. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC