Return on function error.

Hello I am working on my fist "Game" using c++. I have not yet met even a year of programming so of course I get lost easy. Here is the code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// War Card Game! (Build 15w30)
// This is is a player vs. cpu simulatation.
// The player will press space to place the
// next card down. The side with the higher
// card of the two wins the round. First one
// to have a full deck wins! (2 low - A high)

#include <iostream>
#include <algorithm>
#include <array>
#include <random>
#include <chrono>

float cards[52];

float collectDeck(float) //finds full deck.
{
	int i = 0; // Counter
	int c = 1; // Number of Card
	int s = .1;// Suit of the Card
	while(i < 52) // Makes sure only 52 cards are ran.
		{
			if(c == 13) // If a full suit is counted.
			{
				c = 0;
				s = s + .1;
			}
			
			cards[i] = (c+s);
			
			c++;
			i++;
		}
	
	return (cards);
}

float shuffleCards (float)
{
	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
	
	shuffle (cards.begin(), cards.end(), std::default_random_engine(seed));
	
	std::cout << "Shuffled Cards:";
	for (int& x: foo) std::cout << ' ' << x;
	std::cout << '\n';
	
	return (cards);
}

int main ()
{
	collectDeck();
	shuffleCards();
	
	return 0;
}


I need help on two things.

1. Get this script to run.
2. Know, if any, simpler way to shuffle my array!

This is only the beginning of my game (script wise) so I may of course be back for help if I absolutely need it.

Thanks!
FireCoder
Last edited on
2. Know, if any, simpler way to shuffle my array!
That looks pretty simple to me. What do you think is wrong with it?
1. Get this script to run.

Please, describe the errors that you get. (All Forum users are naturally psychics, who already see all the details, but you have to learn to look at the error messages as they can actually help you solve the problems.)

See http://blog.codinghorror.com/rubber-duck-problem-solving/
Why did you use float as their data type?
I hope this will help you. I think this is the one you want to happen to your code. Just ask me whenever you have questions regarding my modifications to your code.

See this also:
http://www.cplusplus.com/reference/array/array/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <algorithm>
#include <array>
#include <chrono>

std::array<float, 52> collectDeck(std::array<float, 52> cards)
{
    int numberOfCards = 1;
    int maximumCardsPerSuit = 13;
    float cardSuit = 0.1;

    for(std::array<float, cards.size()>::iterator iter = cards.begin(); iter != cards.end(); ++iter)
    {
        *iter = static_cast<float>(numberOfCards) + cardSuit;
        if(numberOfCards++ == maximumCardsPerSuit)
        {
            numberOfCards = 1;
            cardSuit += 0.1;
        }
    }
    return cards;
}

std::array<float, 52> shuffleCards(std::array<float, 52> cards)
{
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    shuffle(cards.begin(), cards.end(), std::default_random_engine(seed));
    return (cards);
}

void displayCards(std::array<float, 52> cards)
{
    for(auto n : cards)
        std::cout << n << " ";
    std::cout << std::endl;
}

int main ()
{
    std::array<float, 52> cards = collectDeck(std::array<float, 52>());

    std::cout << "Deck cards:\n";
    displayCards(cards);

    cards = shuffleCards(cards);

    std::cout << "\nShuffled cards:\n";
    displayCards(cards);

    return 0;
}
Topic archived. No new replies allowed.