home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C++...wtf?

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1578
Discussions: 200,954, Posts: 2,379,487, Members: 246,325
Old January 17th, 2006, 04:24 PM   Digg it!   #1 (permalink)
SoMuchAnime-SoLittleTime
 
EXreaction's Avatar
 
Join Date: Aug 2003
Location: Plymouth, WI
Posts: 14,983
Blog Entries: 1
Send a message via MSN to EXreaction
C++...wtf?

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?
__________________
My photography: Flickr

Lithium Studios - phpBB3, PHP, and Web Development

Last edited by EXreaction : January 17th, 2006 at 04:36 PM.
EXreaction is offline   Reply With Quote
Old 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
oliver w is offline   Reply With Quote
Old 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.
Peter M is offline   Reply With Quote
Old January 18th, 2006, 07:19 PM     #4 (permalink)
SoMuchAnime-SoLittleTime
 
EXreaction's Avatar
 
Join Date: Aug 2003
Location: Plymouth, WI
Posts: 14,983
Blog Entries: 1
Send a message via MSN to EXreaction
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...
EXreaction is offline   Reply With Quote
Old 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++.
Peter M is offline   Reply With Quote
Old January 19th, 2006, 02:07 PM     #6 (permalink)
SoMuchAnime-SoLittleTime
 
EXreaction's Avatar
 
Join Date: Aug 2003
Location: Plymouth, WI
Posts: 14,983
Blog Entries: 1
Send a message via MSN to EXreaction
Oh, ya, I can do that.

Well, I started coding with VB...and we always used globals...but I will work on it...
EXreaction is offline   Reply With Quote
Old January 19th, 2006, 02:10 PM     #7 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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.
jkrohn is offline   Reply With Quote
Old January 19th, 2006, 02:19 PM     #8 (permalink)
SoMuchAnime-SoLittleTime
 
EXreaction's Avatar
 
Join Date: Aug 2003
Location: Plymouth, WI
Posts: 14,983
Blog Entries: 1
Send a message via MSN to EXreaction
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!
EXreaction is offline   Reply With Quote
Old January 19th, 2006, 02:20 PM     #9 (permalink)
Perfetc Member
 
VHockey86's Avatar
 
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.
VHockey86 is offline   Reply With Quote
Old January 19th, 2006, 04:30 PM     #10 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,241
Send a message via AIM to Rootstonian
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!
Rootstonian is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Is It Just Me? (2944)
The disrespect of Obama by Russian .. (41)
Making Health Care Worse (178)
Wireless Televisions. (12)
CPU fan stops spinning randomly (9)
Regular Build (11)
windows 7 problem (7)
Laptop with wireless problem. (5)
Is the PSU I received dead? (12)
radeon x850xt platinum & shader.. (6)
Print spooler problem (15)
windows vista security holes (11)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
Recent Discussions
webcam (0)
upgrade for hp a6101 (0)
windows vista security holes (11)
Laptop with wireless problem. (5)
Modern Warfare 2: Who Bought It? (64)
tv not turn on-makes clicking sound (2)
CPU fan stops spinning randomly (9)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Virus advise (8)
My monitor won't turn on after instal.. (1)
Internet Lost (3)
Dept. of HS: NSA 'Helped' Develop Vis.. (16)
Point and Shoot Camera Suggestions. (4)
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Hp Artist Edition + Matching Bag (0)
Asus P4G8X Mobo (6)
Xbox 360 GTA: SA disk error (1)


All times are GMT -4. The time now is 05:37 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28