Re: Access one character in an array of characters
From: rossum (rossum48_at_coldmail.com)
Date: 02/06/05
- Next message: Val: "Re: [c++] passing parameters and safety in deletion."
- Previous message: George Huber: "Re: Can't understand this! (Help required please)"
- In reply to: Gregory W. Ernest: "Access one character in an array of characters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 06 Feb 2005 21:13:25 +0000
On Fri, 4 Feb 2005 20:30:43 -0500, "Gregory W. Ernest"
<Gernest@optonline.net> wrote:
>I am writing a c++ program for one of my classes. One of the assignments I
>had was to create a program that would bascially calculate the number of
>points in a game of Bridge using the principles of Object orientied
>programming.
What little you have shown us does not seem very object oriented. You
should show your instructor that you understand something about OO
programming. Some of the elements you might want to use are:
enum Suit { clubs, diamonds, hearts, spades };
struct Card {
Suit suit;
char value;
};
class Deck {
private:
std::vector<Card> m_cards;
public:
void shuffle();
};
class Hand {
private:
std::vector<char> m_clubs;
std::vector<char> m_diamonds;
std::vector<char> m_hearts;
std::vector<char> m_spades;
public:
int calc_score() const;
void display() const;
};
Since this is your assignment and not mine, all three classes are
incomplete.
>To handle that I have created four arrays of
>thirteen(since a suit can't have more than 13 values) characters , called
>ClubArray, DiamondArray, HeartArray, and SpadesArray to store the different
>values of a particulier suit.
As you will have noticed above I would strongly suggest that you use
std::vector instead of arrays.
>To print out the values by rank I have used this while loop sequence:
> cout<<"Clubs: ";
> while(ClubArray[i] != '\0'){
> cin<<ClubArray[i]<<" ";
I assume the "cin" is a typo. Better to cut-n-paste code from your
editor into your newsreader to avoid this sort of thing.
> i++;
> }
> cout<<endl;
>All the values have been initialized to the NULL character('\0'), so the way
>the loop works it would display any character that is not the null character
>where it would break out of the loop since there are no values to be stored.
>The problem that occurs though is that that line of code not only displays
>the character that at ClubArray[i] but all the characters that follow as if
>it was a string!
Without seeing the exact declaration of ClubArray I cannot be certain,
but you have probably declared it as a C-style string, so it is being
printed as a C-style string. With a vector there would be no
confusion between a string and a vector of char.
If you have to use arrays and not vectors, then try casting explicitly
to char:
cout << static_cast<char>(ClubArray[i]) << ' ';
This might work, depending on exactly how ClubArray is declared.
Alternatively, declare a temporary char variable and print that:
char temp_c = ClubArray[i];
cout << temp_c << ' ';
rossum
-- The ultimate truth is that there is no Ultimate Truth
- Next message: Val: "Re: [c++] passing parameters and safety in deletion."
- Previous message: George Huber: "Re: Can't understand this! (Help required please)"
- In reply to: Gregory W. Ernest: "Access one character in an array of characters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|