C++...wtf?  | | |
January 17th, 2006, 04:24 PM
|
#1 (permalink)
| | SoMuchAnime-SoLittleTime
Join Date: Aug 2003 Location: Plymouth, WI
Posts: 14,983
|
I have been making a BlackJack program with C++...and I am really stumped on this part...
For some reason, I noticed that the totals for how many times you have won were getting screwed up...starting at -1 instead of 0...so, I went through, and I found this part to be the problem: Code: for (int g=1; g<6; g++)
{
youCard[g]=-1;
compCard[g]=-1;
} That is the part that is screwing everything up...and just to show you, if you run this: Code: #include<iostream.h>
int youWin=0, compWin=0, bothLose=0, tie=0, numHands=0;
int youCard[5], compCard[5];
int main()
{
cout<<endl<<endl<<"You have won "<<youWin<<" times."<<endl;
cout<<"The computer has won "<<compWin<<" times."<<endl;
cout<<"Both of you have lost "<<bothLose<<" times."<<endl;
cout<<"You have tied "<<tie<<" times."<<endl;
for (int g=1; g<6; g++)
{
youCard[g]=-1;
compCard[g]=-1;
}
cout<<endl<<endl<<"You have won "<<youWin<<" times."<<endl;
cout<<"The computer has won "<<compWin<<" times."<<endl;
cout<<"Both of you have lost "<<bothLose<<" times."<<endl;
cout<<"You have tied "<<tie<<" times."<<endl;
return(0);
} It outputs this in the console: Quote:
You have won 0 times.
The computer has won 0 times.
Both of you have lost 0 times.
You have tied 0 times.
You have won -1 times.
The computer has won 0 times.
Both of you have lost 0 times.
You have tied 0 times.
Press any key to continue
| If you play with it, and try: Code: for (int g=1; g<5; g++) instead of: Code: for (int g=1; g<6; g++) It doesn't screw up the totals...but I need to set all the numbers in the arrays to -1...otherwise it gets errors in other sections...
Any ideas? 
I can set the arrays to have 6 numbers in them...and it will work in the program(since I only use the first 5)...but wtf is going on? 
Last edited by EXreaction : January 17th, 2006 at 04:36 PM.
|
| |
January 18th, 2006, 06:13 PM
|
#2 (permalink)
| | Member
Join Date: Oct 2005 Location: Australia
Posts: 58
|
um... im guessing you're meant to get all answers showing 0? and mustn't have any -1's?
i got all zeros and i tried some other numbers and they ended up the same...
maybe try a different compiler (i got dev-c++) or something in the other part of the program is stuffing it up :S |
| |
January 18th, 2006, 06:56 PM
|
#3 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Augsburg, Germany
Posts: 5,586
|
That's because C and C++ have array indices starting with 0.
You want to say
for (i=0; i<N; i++) {
there (N being the length of the array, 6 in your case).
Know your pointer arithmetics: stuff[n] is equal to *(stuff+n). Your loop is overrunning the end of the array, and screws up other data - which happens to be one of your totals.
Oh, and DON'T declare variables mid-code. That'll get you an F--- in coding style.
Last edited by Peter M : January 18th, 2006 at 06:59 PM.
|
| |
January 18th, 2006, 07:19 PM
|
#4 (permalink)
| | SoMuchAnime-SoLittleTime
Join Date: Aug 2003 Location: Plymouth, WI
Posts: 14,983
| Quote: |
Originally Posted by Peter M That's because C and C++ have array indices starting with 0.
You want to say
for (i=0; i<N; i++) {
there (N being the length of the array, 6 in your case).
Know your pointer arithmetics: stuff[n] is equal to *(stuff+n). Your loop is overrunning the end of the array, and screws up other data - which happens to be one of your totals.
Oh, and DON'T declare variables mid-code. That'll get you an F--- in coding style. | Oh, I get it now...so it is really 0-4 for an array of 5! xD Makes sense now. 
You mean declaring them in the loop? Whats so bad about that? I am only using it for the loop... |
| |
January 19th, 2006, 09:05 AM
|
#5 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Augsburg, Germany
Posts: 5,586
|
Don't obfuscate -
for (int i = 0, ...
does exactly that. Say
int i;
...
for ( i= ...
Also, pull your data into main(). Global variables are badbadbad as well. That'll get you less than an F.
C++ is about making objects and keeping data encapsuled with the code that handles it. Keeping everything in globals is the exact opposite of good C++. |
| |
January 19th, 2006, 02:07 PM
|
#6 (permalink)
| | SoMuchAnime-SoLittleTime
Join Date: Aug 2003 Location: Plymouth, WI
Posts: 14,983
|
Oh, ya, I can do that.
Well, I started coding with VB...and we always used globals...but I will work on it...  |
| |
January 19th, 2006, 02:10 PM
|
#7 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,552
|
Just out of curiosity, did someone actually TEACH you to use globals? Or is this something that you taught yourself.
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
January 19th, 2006, 02:19 PM
|
#8 (permalink)
| | SoMuchAnime-SoLittleTime
Join Date: Aug 2003 Location: Plymouth, WI
Posts: 14,983
|
I taught myself...I learned how to do it in VB...and just tried it in C++.  I like doing it...but a lot of people don't like it!  |
| |
January 19th, 2006, 02:20 PM
|
#9 (permalink)
| | Perfetc Member
Join Date: Jan 2003 Location: Maryland Suburbia
Posts: 4,334
|
Ya, you should never need to use globals in a program like that.
If your data is only used inside a function (i.e. main()), then it shouldnt ever need a higher scope than that function. If you want to use that in other functoins, pass the value as a parameter. |
| |
January 19th, 2006, 04:30 PM
|
#10 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,241
|
You know, after programming for 10+ years, it's just a "rule" professors have to make kids use pointers and pass by reference. LOL
I want to use a global (or 50) in a program I'm writing, I'll use it dang it. Trash that heap!!!  I'm not doing this for a grade. Worry about problems down the road? Ha! I"ll be gone before I have to fix it. My legacy to all the nice 60's, 70's et al programmers that left me to fix all that Y2k crap! |
| | | Thread Tools | Search this Thread | | | |
Similar Threads | | Thread | Thread Starter | Forum | Replies | Last Post | | Wtf? | Smidley | IMO Community | 2 | January 26th, 2005 02:00 AM | | WTF is this BS | ok.player | Technical Support | 14 | October 27th, 2004 05:29 AM | | wtf?? | Firebatt | IMO Community | 9 | July 16th, 2002 04:54 AM | | WTF!? | skullwerkz | Traders Forum: Buy, Sell, Trade | 5 | June 22nd, 2002 10:42 PM | | wtf is going on?!?! | Chooco | Webmastering and Programming | 2 | March 19th, 2002 02:45 AM | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |