I am creating an RPG shop. It must have items, gold, and item price. Essentially creating an inventory. What i am trying to accomplish is, where the players gold is 0 they cannot add any more items to their inventory, and cannot have negative gold.
When running my code in debug mode it appears to be doing what i want, but when the function exits the amount the player requested has not been countered.
Keep in mind i am still new to c++.
Thanks
#include <iostream>
#include <string>
using namespace std;
// Global consts
const int numItems = 4;
const string items[numItems] = {"boots", "hats", "cats", "bats"}; // create string array of numItems items.
// Create stuct, that holds:
// Item, gold, price.
struct Inv {
int pInv[numItems] = {0, 0, 0, 0};
int gold = 100;
int itemPrice[numItems] = { 10, 6, 12, 15 };
}inv;
void iniItems();
void printItems();
bool buyItems();
bool sellItems();
int main() {
bool isDone = false;
iniItems();
while (isDone == false) {
printItems();
int choice;
bool x = false;
cout << "\nWhat would you like to do? Enter (" << 1 << "-" << 2 << "): " << endl;
cout << "1: Buy Items. \n2: Sell Items." << endl; cin >> choice; cout << endl;
while (x == false) {
if (choice == 1) {
x = buyItems();
}
if (choice == 2) {
x = sellItems();
}
}
}
system("pause");
// dynamic memory not implemented yet. Must wait for working fix of shoppe.cpp
}
void iniItems() {
cout << "** Shop Inventory: **" << endl;
for (int i = 0; i < numItems; i++) {
cout << i + 1 << " - " << items[i] << " - price: $" << inv.itemPrice[i] << endl;
}
}
void printItems() {
cout << "\n** Player Inventory: **" << endl;
cout << "Gold: $" << inv.gold << endl;
for (int i = 0; i < numItems; i++) {
cout << inv.pInv[i] << " x " << items[i] << endl;
}
}
bool buyItems() {
bool exit = false;
int amount;
const int remainder = 10;
printItems();
cout << "\nEnter -1 to quit." << endl;
cout << "What would you like to buy? Enter (" << 1 << "-" << 4 << "): " << endl;
// Get item info.
while (exit == false) {
int inp;
cout << "Item: "; cin >> inp; cout << endl;
cout << "Amount: "; cin >> amount; cout << endl;
// Check if input is valid.
if (inp > 0 && inp <= numItems) {
if (amount >= 0) {
inv.pInv[inp - 1] = 1 * amount;
inv.gold = inv.itemPrice[inp - 1] / amount;
}
// If gold is 0, make sure the user cannot gain more items.
if (inv.gold <= 0) {
int tmp;
inv.gold = 0;
tmp = remainder - amount;
for (int i = tmp; i >= 0; i++) {
inv.pInv[inp - 1]--;
}
return inv.pInv[inp - 1];
}
if (inp == -1) {
return true;
}
if (inp > numItems) {
cout << "Enter valid number." << endl;
return false;
}
else return false;
}
}
if (exit == true) {
return true;
}
}
So i limited the code down into a do while loop with a counter for the gold, in the buyItems() function.
Here it is, if anyone is interested.
do {
inv.gold -= inv.itemPrice[inp - 1];
++(inv.pInv[inp - 1]);
} while (inv.gold > 0);
if (inv.gold < 0) {
inv.gold = 0;
inv.pInv[inp - 1]--;
}
printItems();
Related
I am working on a program in which I have a function and I am hoping to time it. Currently, I considered clock_t in the ctime module to be useful, but when I entered the code for timing, I got (I used Dev-C++ to edit) "permission denied". Below is my code:
#include <iostream>
#include <vector>
#include <ctime>
// Initialization
const int maxN = 99999;
int tokens[maxN] = {}; // All the tokens at mission 1
bool use[maxN] = {}; // Status of the tokens (used or not used)
int bag[maxN]; // Bag to put the tokens into
using namespace std;
int ansIdx = 1; // Answer number
char menu() {
// Print menu
cout << "0: terminate\n";
cout << "1: mission 1 - permutations from 1 ~ N\n";
cout << "2: mission 2 - permutations from input\n";
// Ask for input
cout << "Please input a choice (0 ~ 2): ";
char choice;
cin >> choice;
// Valid command?
if (choice >= '0' and choice <= '2') {
return choice;
}
return '\0';
}
int permutations(int depth, int n, int l) {
if (depth == n) {
// Arranged all elements, so cout each of the elements.
cout << "[" << ansIdx << "] ";
for (int i = 0; i < n; i++) cout << bag[i] << " ";
cout << "\n";
ansIdx++;
return l;
}
for (int i = 0; i < n; i++) {
// Else, for each UNUSED element at this layer:
if (use[i] == 0) {
// Put it into bag[i].
bag[depth] = tokens[i];
// Set the status of it used.
use[i] = 1;
// Call recursion.
permutations(depth + 1, n, l+1);
// Backtrack.
use[i] = 0;
}
}
}
int main() {
// Print menu.
cout << "PERMUTATION GENERATOR\n";
char executionMode;
while (executionMode != '0') {
executionMode = menu();
while (executionMode == '\0') {
executionMode = menu();
}
int layers;
clock_t start;
double ms;
switch (executionMode) {
case '1':
cout << "Enter N: ";
int N;
cin >> N;
for (int i = 0; i < N; i++) {
tokens[i] = i + 1;
}
start = clock();
layers = permutations(0, N, 0);
ms = ((double)(clock() - start)) / CLOCKS_PER_SEC;
cout << "L = " << layers << "\n";
cout << "Mission 1: " << ansIdx - 1 << " permutations\n";
cout << "Used: " << ms << "ms\n";
ansIdx = 1;
break;
case '2':
int M = 0;
while (M < 2 or M > 9) {
cout << "Enter length of input (2 ~ 9): ";
cin >> M;
}
for (int i = 0; i < M; i++) {
cout << "Enter a number: ";
cin >> tokens[i];
}
start = clock();
layers = permutations(0, M, 0);
ms = ((double)(clock() - start)) / CLOCKS_PER_SEC;
cout << "L = " << layers << "\n";
cout << "Mission 2: " << ansIdx - 1 << " permutations\n";
cout << "Used: " << ms << "ms";
ansIdx = 1;
break;
}
}
return 0;
}
Can anyone help me? Any suggestions are appreciated.
I am using C++ and want to do a 2-dimensional array. 10 rows and 3 columns. First column is(1 through 10). For Second column, user enters his/her choice of a number from (1-10) resulting in a times table displaying the results as follows: In this example the user's choice is '4':
1x4=4
2x4=8
3x4=12
4x4=16
5x4=20
6x4=24
7x4=28
8x4=32
9x4=36
10x4=40
I can't get the user's input to calculate correctly when using the for loop.
Well you can try this to get that output
#include<iostream>
using namespace std;
int main()
{
int n; //To take input
int table[10][3]; // Table
cout << "Input a number: ";
cin >> n;
// Generating Output
for (int i = 0; i < 10; i++)
{
table[i][0] = i + 1;
table[i][1] = n;
table[i][2] = table[i][0] * table[i][1];
}
for (int i = 0; i < 10; i++)
{
cout << table[i][0] << " * " << table[i][1] << " = " << table[i][2]<<endl;
}
return 0;
}
Output
SOLVED: Everything seems to be working now!! Here's the code:
#include <iostream>
#include<cstdlib>
#include<iomanip>
#include <ctime>
using namespace std;
void displayTable(int table[10][3]);
bool testMe(int testTable[10][3]);
void createTables(int testTable[10][3], int ansTable[10][3], int
usersChoice);
bool AllAnswersAreTested(bool tested[10]);
void gradeMe(int testTable[10][3], int ansTable[10][3]);
void displayMenu();
int main()
{
srand(time(NULL));
int userInput = 0;
int tableChoice = 0;
int myTable[10][3] = {0};
int testTable[10][3];
int ansTable[10][3];
bool tested = false;
do
{
displayMenu(); //Display the menu of choices
cin >> userInput;
cout << endl;
switch (userInput) //Validate menu choices 1-4
{
case 1: //Display a users choice of table
displayTable(myTable);
break;
case 2: //Test user on users choice of table
cout << "What times table test would you like to take? > ";
cin >> tableChoice;
createTables(testTable, ansTable, tableChoice);
tested = testMe(testTable);
if (tested)
{
gradeMe(testTable, ansTable);
}
break;
case 3: //Display a new table of the users choice
displayTable(myTable);
break;
case 4: //Quit program menu option
cout << "Program ending.\n";
return 0;
default: //Invalid entry
cout << "You entered an invalid item number. Please enter a number from 1 to 4.\n";
cout << endl;
}
} while (userInput != 4);
return 0;
}
void displayTable(int myTable[10][3])
{
int num; //initialize local variables
//Ask the user what times table they would like to review
cout << "What times table would you like to review?" << endl;;
cout << "Please enter a value from 1 to 12 > \n";
cout << "\n";
cin >> num;
cout << endl;
for (int i = 0; i < 10; i++)
{
myTable[i][0] = i + 1;
myTable[i][1] = num;
myTable[i][2] = myTable[i][0] * myTable[i][1];
}
for (int i = 0; i < 10; i++)
{
cout << setw(3)<< myTable[i][0] << " * " << myTable[i][1] << " = " << myTable[i][2] << endl;
}
cout << endl;
}
void createTables(int testTable[10][3], int ansTable[10][3], int usersChoice)
{
for (int i = 0; i < 10; i++)
{
testTable[i][0] = i + 1;
testTable[i][1] = usersChoice;
testTable[i][2] = 0;
ansTable[i][0] = i + 1;
ansTable[i][1] = usersChoice;
ansTable[i][2] = usersChoice * (i + 1);
}
}
bool testMe(int testTable[10][3])
{
bool tested[10] = { false, false, false, false, false,false, false, false, false, false };
while (!AllAnswersAreTested(tested))
{
int index = rand() % 10;
if (tested[index] == false)
{
int randomNum = testTable[index][0];
int tableChoice = testTable[index][1];
int answer;
cout << "What is " << randomNum << " X " << tableChoice << " = ";
cin >> answer;
testTable[index][2] = answer;
tested[index] = true;
}
}
return true;
}
bool AllAnswersAreTested(bool tested[10])
{
for (int i = 0; i < 10; i++)
{
if (tested[i] == false)
{
return false;
}
}
return true;
}
void gradeMe(int testTable[10][3], int ansTable[10][3])
{
int correctAnswers = 0;
for (int i = 0; i<10; i++)
{
if (testTable[i][2] == ansTable[i][2])
{
correctAnswers++;
}
}
int score = (correctAnswers * 10);
if (score == 100)
{
cout << "You passed the test! PERFECT SCORE!!" << endl;
cout << endl;
}
else if (score >= 70)
{
cout << "You passed the test. Your Score is: ";
cout << score;
cout << endl;
}
else if (score < 70)
{
cout << "You did not pass the test. Your Score is: ";
cout << score;
cout << endl;
}
}
//Display the menu function
void displayMenu()
{
cout << " Multiplication Tables" << endl;
cout << endl;
cout << " 1. Review MyTable" << endl;
cout << " 2. Test Me" << endl;
cout << " 3. Enter a New Multiplication Table (1-12)";
cout << " 4. Quit" << endl;
cout << " Enter a Menu Item > ";
}
#include <iostream>
using namespace std;
int main()
{
int a[100][100];
for(int i=1;i<10;i++){
for(int j=1;j<10;j++){
a[i][j] = (i)*(j);
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
There is how the output looks like:
The following program is an Inventory Menu. For some reason everything seems to work except when I searching for the name of the product (option 3) function lookupName. It was working before I put a condition to where if nothing was returned, to give an error message, which is the same I used for lookupSku and that one is working fine. I am not sure what is wrong with the code anymore.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct inventory {
string name;
int sku;
int quantity;
double price;
};
int size = 0;
void fillArray ( inventory[], int&, ifstream& );
void sortArray ( inventory[], int );
void displayIn ( inventory[], int );
int lookupSku ( inventory[], int, int );
int lookupName ( inventory[], int, string );
int main(){
// Constants for menu choices
const int DISPLAY_INVENTORY = 1,
LOOKUP_SKU = 2,
LOOKUP_NAME = 3,
QUIT_CHOICE = 4;
int choice;
inventory products [100];
ifstream fin;
fin.open ("inventory.dat");
if (!fin)
{
cout << endl << endl
<< " ***Program Terminated. *** " << endl << endl
<< " Input file failed to open. " << endl;
system ( "PAUSE>NUL" );
return 1;
}
fillArray ( products, size, fin );
sortArray ( products, size );
// Set up numeric output formatting.
cout << fixed << showpoint << setprecision(2);
do
{
// Display the menu.
cout << "\n\t\t Manage Inventory Menu\n\n"
<< "1. Display inventory sorted by sku\n"
<< "2. Lookup a product by sku\n"
<< "3. Lookup a product by name\n"
<< "4. Quit the Program\n\n"
<< "Enter your choice: ";
cin >> choice;
cout << endl;
// Validate the menu selection.
while (choice < DISPLAY_INVENTORY || choice > QUIT_CHOICE)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
// Validate and process the user's choice.
if (choice != QUIT_CHOICE)
{
int indexSku,
indexName,
skuChoice;
string nameChoice;
switch (choice)
{
case DISPLAY_INVENTORY:
displayIn ( products, size );
break;
case LOOKUP_SKU:
cout << "Enter the Sku number: ";
cin >> skuChoice;
cout << endl;
indexSku = lookupSku( products, size, skuChoice );
if ( indexSku >= 0 )
{
cout << "Product Name: " << products[indexSku].name << endl
<< "Sku: " << products[indexSku].sku << endl
<< "Quantity: " << products[indexSku].quantity << endl
<< "Price: " << products[indexSku].price << endl;
}
else
cout << "No product found with this sku!" << endl;
break;
case LOOKUP_NAME:
cout << "Enter product name with no spaces: ";
cin >> nameChoice;
cout << endl;
indexName = lookupName( products, size, nameChoice );
if ( indexName >= 0 )
{
cout << "Product Name: " << products[indexName].name << endl
<< "Sku: " << products[indexName].sku << endl
<< "Quantity: " << products[indexName].quantity << endl
<< "Price: " << products[indexName].price << endl;
}
else
cout << "No product found with this product name!" << endl;
break;
}
}
} while (choice != QUIT_CHOICE);
fin.close ();
return 0;
}
void fillArray ( inventory product[], int &size, ifstream &fin )
{
int counter = 0;
while (fin >> product[counter].name)
{
fin >> product[counter].sku>> product[counter].quantity
>> product[counter].price;
counter ++;
}
size = counter;
}
void sortArray ( inventory product[], int size )
{
bool swap;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if ( product[count].sku > product[count + 1].sku )
{
inventory temp = product[count];
product[count] = product[count + 1];
product[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void displayIn ( inventory product[], int size )
{
for ( int i = 0; i < size; i++ )
cout << product[i].sku << " " << product[i].quantity << " "
<< product[i].price << " " << setw(4) << product[i].name << endl;
}
int lookupSku(inventory product[], int size, int value)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (product[middle].sku == value)
{
found = true;
position = middle;
}
else if (product[middle].sku > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
int lookupName ( inventory product[], int size , string value )
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (product[middle].name == value)
{
found = true;
position = middle;
}
else if (product[middle].name > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
You code for looking up the product name using Binary Search is correct, but it appears that the data is sorted by SKU. The Binary Search algorithm will only work if the data is sorted.
You could modify your program to sort the data by product name before applying the Binary Search, but in this case since you are using a Bubble Sort which is an N^2 search time, you would be better off with just a linear search for the product name.
This program should be accepting a bet, generating a set of cards for each player, and adding the bet to the winner's pool. This is a class project. I cannot figure out what the problem is on line 60.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct cards {
int value;
string face;
string suit;
bool facebool;
};
struct hand {
cards card[4];
int antiduplicate[52];
};
struct player {
string name;
int sum;
int money;
hand hand;
int bet;
};
struct points {
int player1;
int player2;
};
hand drawhand(int [52]);
points calcvalue(player, player);
int winner(player, player);
int main() {
points store = {};
player house = {"The House"};
player player1 = {};
srand (time(0));
cout << "Enter player 1's name:";
getline(cin,player1.name);
cout << "Enter player 1's money:";
cin >> player1.money;
cout << endl;
house.money = (player1.money + 1);
do{
int deckarray[52] = {0};
//do{
cout << "What do you want to bet?\n";
/* line 60 */ cin >> player1.bet;
cout << "check";
if (player1.bet < player1.money){
}
else if (player1.bet > player1.money){
cout << "\nYou cannot go into debt!!\n";
}
else if (player1.bet == 0){
cout << "\nYou ended the game!";
return 0;
}
//}while (bet > player1.money);
house.hand = drawhand(deckarray);
for (int i = 0; i < 52; i++) {
if (house.hand.antiduplicate[i] == 1)
deckarray[i] = 1;
}
player1.hand = drawhand(deckarray);
for (int i = 0; i < 52; i++) {
if (player1.hand.antiduplicate[i] == 1)
deckarray[i] = 1;
}
cout << "\nCheck check\n";
store = calcvalue(player1, house);
player1.sum = store.player1;
house.sum = store.player2;
cout << player1.name << "'s hand:\n" << player1.hand.card[1].face << " of " <<player1.hand.card[1].suit << endl
<< player1.hand.card[2].face << " of " <<player1.hand.card[2].suit << endl <<player1.hand.card[3].face << " of " <<player1.hand.card[3].suit << endl;
cout << ">> " <<player1.name << " scored " << player1.sum << " points!\n\n";
cout << house.name << "'s hand:\n" << house.hand.card[1].face << " of " <<house.hand.card[1].suit << endl
<< house.hand.card[2].face << " of " <<house.hand.card[2].suit << endl <<house.hand.card[3].face << " of " <<house.hand.card[3].suit << endl;
cout << ">> " << house.name << " scored " << house.sum << " points!\n";
int win;
win = winner(player1, house);
if (win == 1){
cout << "\n" << player1.name << " wins the round!!" << endl;
player1.money = (player1.money + player1.bet); house.money = (house.money - player1.bet);
}
else if (win == -1){
cout << "\n\n" << house.name << " wins the round!!";
house.money = (house.money + player1.bet);
player1.money = (player1.money - player1.bet);
}
else if (win == 0){
cout << "\n\n" << house.name << " wins the round!!";
house.money = (house.money + player1.bet);
player1.money = (player1.money - player1.bet);
}
cout << endl << "House money: " << house.money << endl << "Player money: " << player1.money << endl << endl;
}while (player1.money > 0 && house.money > 0 && player1.bet != 0);
cout << "Game over!";
return 0;
}
hand drawhand(int deckarray[52])
{
string tsuit, tface;
int tvalue;
int suitvalue, facevalue;
bool tfacebool;
hand thand;
for (int i = 0; i < 4; i++) {
for (int i = 0; i < 52; i++) {
if (deckarray[i] == 1)
thand.antiduplicate[i] = 1;
}
int index;
do {
index = rand()%52;
} while (thand.antiduplicate[i] == 1);
thand.antiduplicate[i] = 1;
facevalue = (index%13 + 1);
suitvalue = (index / 13);
switch (suitvalue)
{
case 0: tsuit = "Hearts";
break;
case 1: tsuit = "Diamonds";
break;
case 2: tsuit = "Clubs";
break;
case 3: tsuit = "Spades";
break;
}
switch (facevalue)
{
case 1: tface = "Ace";
tvalue = 1;
break;
case 11: tface = "Jack";
tvalue = 10;
tfacebool = true;
break;
case 12: tface = "Queen";
tvalue = 10;
tfacebool = true;
break;
case 13: tface = "King";
tvalue = 10;
tfacebool = true;
break;
}
if ((facevalue > 1) && (facevalue < 11))
tface = to_string(facevalue);
if (facevalue < 11) tvalue = facevalue;
thand.card[i].suit = tsuit;
thand.card[i].face = tface;
thand.card[i].value = tvalue;
thand.card[i].facebool = tfacebool;
}
return thand;
}
points calcvalue(player player1, player house)
{
points tpoints;
player1.sum = ((player1.hand.card[1].value + player1.hand.card[2].value + player1.hand.card[3].value) % 10);
if (player1.hand.card[1].facebool == true && player1.hand.card[2].facebool == true && player1.hand.card[3].facebool == true)
player1.sum = 10;
house.sum = ((house.hand.card[1].value + house.hand.card[2].value + house.hand.card[3].value) % 10);
if (house.hand.card[1].facebool == true && house.hand.card[2].facebool == true && house.hand.card[3].facebool == true)
house.sum = 10;
tpoints.player1 = player1.sum;
tpoints.player2 = house.sum;
return tpoints;
}
int winner(player player1, player house){
int winorlose;
if (player1.sum > house.sum)
{winorlose = 1;}
else if (player1.sum < house.sum)
{winorlose = -1;;}
else if (player1.sum == house.sum)
{winorlose = 0;}
return winorlose;
}
I see a lot of problems at first glance actually. eg: You've called a hand object 'hand' (thus redefining it), if you're going to call your object names the same as their class/struct, at least differentiate them with capitalization. eg: call your hand struct "Hand" instead of "hand"
cout << "What do you want to bet? ";
cin >> player1.bet; // <==== problem
cout << "check";
if (player1.bet < player1.money){
}
else if (player1.bet > player1.money){
cout << "\nYou cannot go into debt!!\n";
}
else if (player1.bet == 0){
cout << "\nYou ended the game!";
return 0;
}
That being said the issue you are having specifically relating to this section of code probably has something to do with a "\n" staying in the buffer and not getting cleared. Try using cin.ignore(numeric_limits::max(),'\n'); to solve this problem (don't forget to #include limits as well).
Similar problem here: c++ cin input not working?
Unfortunately I'm pressed for time and don't have time for a thorough look. :(
while (deckSize > 2)
{
one_Card = card_deck.back();
card_deck.pop_back();
two_Card = card_deck.back();
card_deck.pop_back();
three_Card = card_deck.back();
card_deck.pop_back();
oneCard_Name = card_name(one_Card);
twoCard_Name = card_name(two_Card);
threeCard_Name = card_name(three_Card);
oneCard_Suit = card_suit(one_Card);
twoCard_Suit = card_suit(two_Card);
threeCard_Suit = card_suit(three_Card);
oneCard_Rank = card_rank(one_Card);
twoCard_Rank = card_rank(two_Card);
threeCard_Rank = card_rank(three_Card);
bool between1 = (oneCard_Rank < threeCard_Rank && threeCard_Rank < twoCard_Rank);
bool between2 = (twoCard_Rank < threeCard_Rank && threeCard_Rank < oneCard_Rank);
cout << "Here are your two cards: " << endl;
cout << setw(10) << oneCard_Name << " of " << oneCard_Suit << setw(20) << twoCard_Name << " of " << twoCard_Suit << endl;
cout << "Do you think the next card will lie between these? (y/n): ";
cin >> user_input;
cout << endl << endl;
cout << "Here is your next card: " << endl;
cout << setw(10) << threeCard_Name << " of " << threeCard_Suit << endl << endl << endl;
count++;
if(user_input == "y" || user_input == "yes" || user_input == "Yes" || user_input == "Y")
{
if(between1 || between2)
{
cout << "You win!" << endl;
win++;
}
else
{
cout << "You lose!" << endl;
lose++;
}
}
else
{
if(between1 || between2)
{
cout << "You lose!" << endl;
lose++;
}
else
{
cout << "You win!" << endl;
win++;
}
}
}
cout << "You have played this game " << count << " times and you have won: " << win << " and lost " << lose << endl;
return 0;
}
These are the two subprograms that shuffle and initialize the deck
void initDeck(vector<int> &card_deck)
{
int i;
for(i = 0; i <= 51; i++)
{
card_deck[i] = i;
}
}
void shuffleDeck(vector<int> & card_deck)
{
int n;
for(n = 51; n >= 0; n--)
{
int i = randomize();
int temp = card_deck[i];
int temp2= card_deck[n];
card_deck[n] = temp;
card_deck[i] = temp2;
}
}
After when I run the program it allows me to run it, but when I reach to the number less than the condition in the while loop it just gives me an error, and does not finish the program. I had this error earlier and fixed it, so I have a basic understanding of what the error means. From my knowledge it is trying to collect numbers past the vector length. However this time I don't see my error at all.
deckSize is not being set/updated anywhere. It should rather be card_deck.size()
You should use push_back and emplace for the type vector like this:
void initDeck(vector<int> &card_deck){
int i;
for(i = 0; i <= 51; i++)
{
card_deck.push_back(i);
}
}
Take a see to this link
Try this:
void initDeck(vector<int> &card_deck)
{
int i;
for(i = 0; i <= 51; i++)
{
card_deck.push_back(i);
}
}
void shuffleDeck(vector<int> & card_deck)
{
int n;
for(n = 51; n >= 0; n--)
{
int i = randomize();
int temp = card_deck[i];
int temp2= card_deck[n];
card_deck[n] = temp;
card_deck[i] = temp2;
}
}
For generating random number see this and this. Or you can find other solution that is more reliable.