1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

C++ Programmers, if there are any.

Discussion in 'BBS Hangout' started by Lil Pun, Dec 11, 2002.

  1. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    I am teaching myself the programming language of C++. I have some programs written or fragments of programs written that won't compile. If I were to post these wuld anybody be able to tell me what is incorrect with them?
     
  2. RunninRaven

    RunninRaven Member
    Supporting Member

    Joined:
    Jun 16, 2000
    Messages:
    15,268
    Likes Received:
    3,214
    I know C++ decently enough. Give it a shot.
     
  3. drapg

    drapg Member

    Joined:
    Mar 18, 2002
    Messages:
    9,683
    Likes Received:
    2
    post it. C++ is my livelihood and career.
     
  4. B

    B Member

    Joined:
    Jul 19, 2001
    Messages:
    1,901
    Likes Received:
    24
    Dang, I was hoping this would be a thread about someone looking to hire C++ programmers in Austin. I'll take a look if you post it.

    B
     
  5. Cold Hard

    Cold Hard Member

    Joined:
    Aug 28, 2000
    Messages:
    1,940
    Likes Received:
    1,005
    Sure, go ahead and post it. C++ is one of the languages that I'm pretty good at and comfortable with; I did more coding in that language than any other during my years at UT.
     
  6. heypartner

    heypartner Member

    Joined:
    Oct 27, 1999
    Messages:
    63,510
    Likes Received:
    59,002
    post any compiler error messages as well.
     
  7. rockit

    rockit Member

    Joined:
    Feb 14, 1999
    Messages:
    627
    Likes Received:
    2
    Whatever drapg said ... I live for c++ ... ok, so I'm not that pathetic, but I do love coding in it :)
     
  8. RunninRaven

    RunninRaven Member
    Supporting Member

    Joined:
    Jun 16, 2000
    Messages:
    15,268
    Likes Received:
    3,214
    I guess Lil Pun fixed it himself... :(
     
  9. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    No I haven't fixed it myself it's just taking me a while to pick which programs I am having trouble with first.

    But if you all are so eager to help, could somebody please explain arrays and possibly show an example?
     
  10. StupidMoniker

    StupidMoniker I lost a bet

    Joined:
    Jul 18, 2001
    Messages:
    16,150
    Likes Received:
    2,817
    An array is a data structure of pre-declared length for holding a single type of data.

    ex:
    An array of integers 1,2,3,4,5-
    int arr[5];
    arr[0] = 1;
    arr[1] = 2;
    arr[2] = 3;
    arr[3] = 4;
    arr[4] = 5;

    Very simple. Arrays are an important building block for better data structures like vectors as well. I think it might be possible to declare a size 0 array, then add items, but I am not sure.
     
  11. Woofer

    Woofer Member

    Joined:
    Oct 10, 2000
    Messages:
    3,995
    Likes Received:
    1
    Do you have any books? If you have Kernigan and Ritchey's Programming in C (sic?) it's a pretty concise explanation of C. If you are using C++ you don't use arrays. You want to use the STL containers instead.
     
  12. No Worries

    No Worries Member

    Joined:
    Jun 30, 1999
    Messages:
    32,850
    Likes Received:
    20,638
    And your debugger has an accident on the screen everytime you try to debug them :(
     
  13. Achebe

    Achebe Member

    Joined:
    Oct 26, 1999
    Messages:
    6,237
    Likes Received:
    3
    Lil Pun, it sounds as if you are getting good feedback here, but I just wanted to point out that there are some really good boards out there that you can peruse. Rather than even posting your specific question you can get a quick overview of the same query posted over and over again.

    ie, I hang out at javaranch quite a bit. There has to be a comparable bbs for c++ programmers.
     
  14. Mudbug

    Mudbug Member

    Joined:
    Sep 24, 2002
    Messages:
    137
    Likes Received:
    0
    No. Either:

    1) Use a vector some other list.

    or

    2) Dynamically allocate the memory for the array on the heap once the size is determined.
     
  15. StupidMoniker

    StupidMoniker I lost a bet

    Joined:
    Jul 18, 2001
    Messages:
    16,150
    Likes Received:
    2,817
    Mudbug,

    Yeah, I figured you would have to do a memory allocation.

    Like I, and several other people, have said, don't use arrays if you don't have to. There are much better data structures in C++. If it is something very simple where you know the size of the array beforehand, like in my example above, go ahead. Otherwise, vectors are just like arrays only better because you can add and remove values from the "end".
     
  16. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    Here is a shell of one of the programs which I am trying to write. It is a program using arrays to create a game of bridge with a deck of 52 cards. I wrote what I want the program to do in the sections I couldn't figure out. Here is the shell below:


    // Card Dealing. A deck of cards is a sequence of numbers 0,1,...,51
    // with (number / 13) equaling 0, for diamonds,1 for clubs 2 for hearts,
    // and 3 for spades and the value of (number % 13) represents the respective
    // faces of the cards. i.e. Cards 0...12 are the two, three, four, ... ,ace
    // of diamonds, 26...38 are 13...25 are hearts,clubs and 39...51 are spades.
    // The program initializes, shuffles and the deals-out
    // the cards to four players. Cards are sorted for each player.

    #include <iostream>
    using std::cout;
    using std::endl;

    #include <ctime>
    #include <string>
    using std::string;

    void initialize(int []);
    void shuffle(int []);
    void sort(int [], int);
    void swap(int &, int &);
    void deal(int[]);

    // create a deck of cards, shuffle, sort each hand
    // and then display each of the four player's cards
    main()
    {
    int deck[52];

    initialize(deck);
    shuffle(deck);
    sort(deck, 13);
    sort(&deck[13], 13);
    sort(&deck[26], 13);
    sort(&deck[39], 13);
    deal(deck);

    return (0);
    }

    // initialize the deck of cards so that card = i
    void initialize(int card[])
    {
    // code to initialize the deck
    }


    // This routine is passes a pointer to a deck of cards. It randomly
    // selects a card from the unshuffled part of the deck, swaps this card
    // with the last card in the unshuffled part of the deck. This effectively
    // reduces the size of the unshuffled deck by one card. It continues until
    // the unshuffled deck has only one card left. When done, the deck of cards
    // in the calling routine has been randomely rearranged (i.e. shuffled).
    void shuffle(int card[])
    {
    srand(time(NULL));
    int random_position, last_position;

    for (last_position = 51; last_position > 0 ; --last_position)
    {
    // write code to select a position at random from position
    // (from postiont 0 to last_position) and then
    // swap the card at this position with the card in last_position

    } // end for
    }

    // Sort the elenents in an array of specified size in increasing order
    void sort(int array[], int size)
    {
    for (int last_position = size -1; last_position > 0 ; --last_position)
    {
    // write code to find the position of the largest card
    // (from position 0 to last_position) and then
    // swap the card at this position with the card in last_position

    } // end for
    }


    // This function uses addresses to exchange the values of two
    // integer variables in the calling routine. Note the use
    // of pointers for each varaible's address passed to the function.
    void swap(int &number1, int &number2)
    {
    // write code exchange the values of number1 and number2
    }

    void deal(int card[])
    {

    // deal each of the four hands, consisting of the shufffled card, sorted
    // for each individual player.
    // For each card in the deck use print_card(card); to print out the
    // face_value and the suit of that card.

    } // end deal

    // this routine will print out the face value and the suit for
    // each of the cards in a deck. card must be between 0 and 51.
    void print_card(const int card )
    {
    string suit[4] = {"Clubs", "Diamonds", "Hearts", "Spades"};
    string face_value[13] = {"Two", "Three", "Four", "Five", "Six", "Seven",
    "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};

    cout << face_value[card % 13] << " of " << suit[card / 13] << endl;
    }
     
  17. Mudbug

    Mudbug Member

    Joined:
    Sep 24, 2002
    Messages:
    137
    Likes Received:
    0
    Here's your shuffle method. Also, your main should return an int and I think that you should avoid using "using" statements. If you have code that's millions of lines long and there are a lot of using statements then it can become confusing.

    for (last_position = 51; last_position > 0 ; --last_position)
    {

    int r = rand() % 52;
    int temp = card[ last_position ];
    card[ last_position ] = card[r];
    card[r] = temp;

    } // end for
     
  18. Another Brother

    Joined:
    Sep 28, 2001
    Messages:
    7,314
    Likes Received:
    881
    Dang I can barely count money!
     
  19. No Worries

    No Worries Member

    Joined:
    Jun 30, 1999
    Messages:
    32,850
    Likes Received:
    20,638
    FWIW, you are writing a C program here and not a C++ program. This is not necessarily a bad thing.
     
  20. Mudbug

    Mudbug Member

    Joined:
    Sep 24, 2002
    Messages:
    137
    Likes Received:
    0
    My guess is that this is just an exercise using arrays. But this is definitely not object-oriented programming which I think is a bad thing.
     

Share This Page