| | |
Help function not returning correct value
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
C++ Syntax (Toggle Plain Text)
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; }
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)
int main() { char guess; hangman game1; cout << "Welcome to hangman alpha ver 1.2 " ; cout <<"You start off with : "<<game1.getlives() << " lives \n " << endl; cout << game1.checkstatus(); while((game1.getlives() !=0 ) && (game1.checkstatus() > 0)) { cout << game1.getencrption() <<endl; cin >> guess ; game1.getletter(guess); } return 0; }
well have fun! its been bugging me for hours!
•
•
•
•
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; }
C++ Syntax (Toggle Plain Text)
.. cout << i; break; ..
I would imagine that you initialize encryption with asterisks, right? So let's walk through the execution with that assumption in mind:
You create i and set it to 0. Since i is less than 5, the body of the loop is executed.
Since encryption was filled with asterisks, this test is true as the first character is indeed '*'. The body of the condition is executed:
0 is returned.
You should also be aware that this is incorrect:
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.
C++ Syntax (Toggle Plain Text)
for ( int i = 0; i < 5; i++ )
C++ Syntax (Toggle Plain Text)
if (encryption[i] == '*' )
C++ Syntax (Toggle Plain Text)
return i;
You should also be aware that this is incorrect:
C++ Syntax (Toggle Plain Text)
if ( i == 4 )
New members chased away this month: 3
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
hello, I thought I best post the entire code:
it still doesnt work and can't figure it out! If a player guess correctly it doesnt finish the game.....
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> using namespace std; class hangman { public: hangman(); char * getname(); char * getencrption(); void getletter(char letter); void looselife(); int getlives(); int checkstatus(); private: char name[20]; char encryption[20]; int lives; int total; }; hangman::hangman() { strcpy(name,"John"); strcpy(encryption, "****"); lives = 5; total = 4; } char * hangman::getname() { return name; } char * hangman::getencrption() { return encryption; } void hangman::looselife() { --lives; cout << "You now have "<< lives << " lives remaining "<<endl; } void hangman::getletter(char letter) { for (int i = 0 ;i < 4 ; i++) { if( letter ==name[i] ) { encryption[i] = name[i]; break; } } if (i == 4) { cout << "not found" <<endl; looselife(); } } int hangman::getlives() { return lives; } int hangman::checkstatus() { int i; for ( i = 4; i > 0; i--) { if (encryption[i] == '*' ) { return i; break; } } if ( i == 0 ) { cout<<"You win!"<<endl; } return 0; } int main() { char guess; hangman game1; cout << "Welcome to hangman alpha ver 1.2 " ; cout <<"You start off with : "<<game1.getlives() << " lives \n " << endl; cout << game1.checkstatus(); while(game1.getlives() !=0 ) { cout << game1.getencrption() <<endl; cin >> guess ; game1.getletter(guess); } if (game1.getlives() == 0) { cout << "You've lost the game!" <<endl; } return 0; }
it still doesnt work and can't figure it out! If a player guess correctly it doesnt finish the game.....
•
•
•
•
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.
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
C++ Syntax (Toggle Plain Text)
int hangman::checkstatus() { int i; for ( i = 4; i > 0; i--) { if (encryption[i] == '*' ) { return i; } } if ( i == 0 ) { cout<<"You win!"<<endl; } return 0; }
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?
This important part looks like it got lost.
This is how I might write checkstatus.
while ( game1.getlives() != 0 && game1.checkstatus() )This is how I might write checkstatus.
C++ Syntax (Toggle Plain Text)
int hangman::checkstatus() { int i; for ( i = 0; encryption[i] != '\0'; ++i ) { if ( encryption[i] == '*' ) { return 1; } } cout<<"You win!"<<endl; return 0; }
"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
![]() |
Similar Threads
- Returning a correct python list (Python)
- Forms Not Returning Correct Information (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: MP3 Player
- Next Thread: Search Utility
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






