| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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...) |
|
#2
|
||||
|
||||
|
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
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > C++ Help - Deck Of Cards - For Loop |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|