Another C++ Snip:

Post your C++ code snippets in here.
5 posts Page 1 of 1
Contributors
User avatar
lawrence12
Dedicated Member
Dedicated Member
Posts: 73
Joined: Tue Mar 02, 2010 3:21 am

Another C++ Snip:
lawrence12
Here ya go:
Code: Select all
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;

int rand_0toN1(int n);
void get_an_object();
int select_next_available(int n);

char *objects[4] = {"Cube", "Ball", "Spade", "Diamond"};
char *colors[13] = {"White", "Yellow", "Red", "Blue", "Green", "Crimson", "Cyan", "Orange", "Gold", "Aqua", "Purple", "Pink", "Black"};

int things_drawn[52];
int things_remaining = 52;

int main(){
    int n, i;
    srand(time(NULL));
    while(1){
        cout << "WARNING: DONT TYPE LETTERS TYPE NUMBERS" << endl;
        cout << "Objects: Cube, Ball, Spade, Diamond" << endl;
        cout << "Colors: White, Yellow, Red, Blue, Green, Crimson, Cyan, Orange, Gold, Aqua, " << endl;
        cout << "Purple, Pink, Black" << endl;
        cout << "Press 0 to exit: ";
        cin >> n;
        if (n == 0)
        break;
        for (i = 1; i <= n; i++)
        get_an_object();
    }
    return 0;
}

void get_an_object(){
    int c; // Random index (0 thru 12) into
          // objects array.
    int o; // Random index (0 thru 3) into
          //  colors array.
    int n, thing;

    n = rand_0toN1(things_remaining--);
    thing = select_next_available(n);

    c = thing % 13; // o = random 0 to 12
    o = thing / 13; // c = random 0 to 3
    cout << colors[c] << " " << objects[o] << endl;
    cout << "----------------" << endl;
}

int select_next_available(int n){
    int i = 0;
    while (things_drawn[i])
    i++;

    while (n-- > 0){
        i++;
        while(things_drawn[i])
        i++;
    }
    things_drawn[i] = true;
    return i;
}

int rand_0toN1(int n){
    return rand() % n;
}
You do not have the required permissions to view the files attached to this post.
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

Re: Another C++ Snip:
Scottie1972
Umm?
What is this suspose to be?
Image
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Another C++ Snip:
MrAksel
What does this do?
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
lawrence12
Dedicated Member
Dedicated Member
Posts: 73
Joined: Tue Mar 02, 2010 3:21 am

Re: Another C++ Snip:
lawrence12
You can test it out for youself.
But it only works in Devc++ tho.
Its like a game of picking.
Example: You type 1, it will randomly give you a Ball, Cube, Diamond, Spade with colors like Red Ball.
Just type 0 to exit it.
User avatar
lawrence12
Dedicated Member
Dedicated Member
Posts: 73
Joined: Tue Mar 02, 2010 3:21 am

Re: Another C++ Snip:
lawrence12
Oh and dont worry, its perfectly harmless.
You can even ask the c++ programmers if this is safe or not.
5 posts Page 1 of 1
Return to “Quick Snips”