C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 24th, 2005, 11:57 PM
phobson phobson is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 2 phobson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 44 sec
Reputation Power: 0
C++ Help - Deck Of Cards - For Loop

Hi, i'm just trying to program some code that initializes an array of 52 cards, then a simple cout in a for loop, outputs each card one-by-one.
The output right now works, except every line ouputs A) the card, then B) the first character of next card. (SEE Output below)

Here's My Code:

#include <stdafx.h>
#include <stdlib.h> //needed for rand() and srand()
#include <iostream>
#include <string.h>
usingnamespace std;

//Global variables

int cards[53]; //array filled with card values
char names[53][25] = { //array filled with card names. Used for dealing
"Zero", "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades",
"Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades",
"Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades",
"King of Spades",

"Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts",
"Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts",
"Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts",
"King of Hearts",

"Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs",
"Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs",
"Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs",
"King of Clubs",

"Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds",
"Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds",
"Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds",
"King of Diamonds"
};

int
main ()
{
for( int i = 0 ; i < 54 ; ++i)
{
for( int j = 0 ; j < 26 ; ++j)
{
cout << names[i][j];
}


cout << "\n";
}cin.ignore();
return 0;
}

Thanks in advance for the help.. i'm pretty sure it's a problem with the for loops.. any help is appreciated.

OUTPUT
Zero A
Ace of Hearts T

(etc... etc...)

Reply With Quote
  #2  
Old April 25th, 2005, 01:36 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2Folding Points: 909006 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
yup, for loops, they should be:

Code:
for( int i = 0 ; i < 53 ; ++i)
{
	for( int j = 0 ; j < 25 ; ++j)
	{
		cout << names[i][j];
	}
}


If you declare an array of size 53, it has entries starting at 0, and the last one is 52, so your for-terminator was one 1 too big. Same went for the other one.
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > C++ Help - Deck Of Cards - For Loop


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT