Re: Access one character in an array of characters

From: rossum (rossum48_at_coldmail.com)
Date: 02/06/05


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


Relevant Pages

  • Re: MFC dll and exe porting to 64-bit
    ... Why the obsolete 'char *' instead of the proper LPTSTR? ... We are no longer programming 16-bit windows in an 8-bit character world, ... string arguments should probably be LPCTSTR, that is, const parameters. ... declaration of the function itself, in context (meaning show the surrounding class ...
    (microsoft.public.vc.mfc)
  • Re: Banks and economy
    ... char cptr; ... I'm assuming that it's at file scope, because otherwise the array declaration wouldn't be allowed. ... In some cases, a tentative definition with no corresponding external definition ends up being treated as the actual definition, with an implicit initializer of 0. ...
    (comp.lang.c)
  • Re: malloc warning gcc > 4.0
    ... > char* p; ... > warning: incompatible implicit declaration of built-in function 'malloc' ...
    (comp.lang.c.moderated)
  • Re: Storing info for cards
    ... >> unsigned int rank; ... is there any particular reason you used int for rank ... > you only used char for suit? ...
    (comp.lang.c)
  • Re: Strange read problem
    ... The '<<' operator for streams accepts a 'char *' only if it points to ... a C-style string. ... the same reason 'strlen' would be broken here. ...
    (comp.unix.programmer)