I have been writing a blackjack program using structures for my C class. The structures in my code are based off of what we have written in class. But I can't figure out how to add the values of the cards after they are dealt to the dealer and the user to further progress my program. I have tried incrementing "i" and printing the values, I have also tried using "i + 1".
How can I get this to work?
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct card {
char * face;
char * suit;
int value;
}Card;
void clearBuffer(){
char ch;
while ((ch = getchar()) != '\n' && ch != EOF);
}
int deal(){
int deck[] ={2,3,4,5,6,7,8,9,10,10,10,10,11};
return deck[rand() % 13];
}
int check4ace(int * hand){
for(int i = 0; i < 10; i++){
if(hand[i] == 11){
puts("found 11");
hand[i] = 1;
return 1;
}
}
return 0;
}
void printDeck(Card deck[]){
for(int i = 0; i < 52; ++i){
printf("%s of %s\n",deck[i].face,deck[i].suit);
}
}
Card * dealCard(Card * deck){
Card * delt = &deck[0];
return delt;
}
void shuffle(Card deck[]){
Card temp;
int rIndex;
for(int i = 0; i < 52; i++){
rIndex = rand() % 52;
temp = deck[i];
deck[i] = deck[rIndex];
deck[rIndex] = temp;
}
}
int main(){
unsigned long timenow;
timenow = time(NULL);
srand(timenow);
char hit;
char game;
int answer = (rand() % 100) + 1;
int total = 0;
Card deck[52];
char * faces[] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
char * suits[] = {"Clubs","Hearts","Diamonds","Spades"};
int values[] = {11,2,3,4,5,6,7,8,9,10,10,10,10};
Card * deckIdx = deck;
for(int i = 0; i < 52; i++){
deck[i].face = faces[i%13];
deck[i].value = values[i%13];
deck[i].suit = suits[i/13];
}
shuffle(deck);
Card * cardHand[10];
Card * dealerHand[10];
int hand[10];
while(1){
printf("Play a game? (y/n)\n");
scanf("%c",&game);
shuffle(deck);
clearBuffer();
if(game == 'y' || game == 'Y'){
cardHand[0] = dealCard(deckIdx++);
cardHand[1] = dealCard(deckIdx++);
dealerHand[0] = dealCard(deckIdx++);
dealerHand[1] = dealCard(deckIdx++);
printf("\nDealer:\nFACE DOWN\n",dealerHand[0]->face, dealerHand[0]->suit);
printf("%s of %s\n",dealerHand[1]->face, dealerHand[1]->suit);
printf("You:\n%s of %s\n",cardHand[0]->face, cardHand[0]->suit);
printf("%s of %s\n",cardHand[1]->face, cardHand[1]->suit);
}
else if(game == 'n' || game == 'N'){
printf("Bye!\n");
break;
}
else{
printf("No valid answer, Bye!\n");
break;
}
while(game == 'y' || game == 'Y'){
int i = 0;
printf("\nHit or Stand? (h/s)\n");
scanf("%c",&hit);
clearBuffer();
while(hit == 'h' || hit == 'H'){
cardHand[i] = dealCard(deckIdx++);
dealerHand[i] = dealCard(deckIdx++);
printf("\nDealer:\n%s of %s\n",dealerHand[i]->face, cardHand[i]->suit);
printf("You:\n%s of %s\n",cardHand[i]->face, cardHand[i]->suit);
break;
}
if(hit == 's' || hit == 'S'){
printf("You ended with <value>\n");
printf("Dealer ended with <dealerValue>\n");
break;
}
}
}
return 0;
}
Related
I'm very new to the linked list and trying to add a new node to the end of one. I'm using a header file and function for this and here is my functions. I'm trying to use customerAdd and printAllCustomers.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "salesautomation.h"
int categoryFilter(char category[]){
int a;
if(strcmp(category,"1")==0 || strcmp(category,"Sale") == 0 || strcmp(category,"sale") == 0){
a = 1;
}
else if((strcmp(category,"2")==0) || (strcmp(category,"Customer Informations") == 0) || (strcmp(category,"Customer informations") == 0) || (strcmp(category, "customer informations") == 0) || strcmp(category,"customer Informations") == 0){
a = 2;
}
else if(strcmp(category,"3")==0 || strcmp(category,"Product Informations") == 0 || strcmp(category,"Product informations") == 0 || strcmp(category, "product informations") == 0 || strcmp(category,"product Informations") == 0){
a = 3;
}
else if(strcmp(category,"4")==0 || strcmp(category,"Customer Analysis") == 0 || strcmp(category,"Customer analysis") == 0 || strcmp(category, "customer analysis") == 0 || strcmp(category,"customer Analysis") == 0){
a = 4;
}
else if(strcmp(category,"5")==0 || strcmp(category,"Product Analysis") == 0 || strcmp(category,"Product analysis") == 0 || strcmp(category, "product analysis") == 0 || strcmp(category,"product Analysis") == 0){
a = 5;
}
else if(strcmp(category,"6")==0 || strcmp(category,"Exit") == 0 || strcmp(category,"exit") == 0){
a = 6;
}
else{
a = 0;
}
return a;
}
int customerChecker(char name[]){
return 0;
}
void customerAdd(Customer **N01){
Customer *newCustomer;
newCustomer = malloc(sizeof(Customer));
Customer *last = *N01;
printf("Customer ID: ");
scanf("%d", &newCustomer->ID);
printf("%d\n", newCustomer->ID);
printf("Customer Name: ");
getchar();// If i don't put this here, gets scans \n when user hits enter. So i put this here to save \n to nowhere
gets(newCustomer->name);
printf("%s\n",newCustomer->name);
printf("Customer Type (1 for corporate, 2 for individual): ");
scanf("%d",&newCustomer->type);
printf("%d\n",newCustomer->type);
printf("Customer x coordinate: ");
scanf("%lf",&newCustomer->coordX);
printf("%lf\n",newCustomer->coordX);
printf("Customer y coordinate: ");
scanf("%lf",&newCustomer->coordY);
printf("%lf\n",newCustomer->coordY);
newCustomer->nextCustomer = NULL;
if(*N01 == NULL){
*N01 = newCustomer;
}
else{
while(last->nextCustomer != NULL){
last = last->nextCustomer;
}
last->nextCustomer = newCustomer;
}
}
void printAllCustomers(Customer *N01){
int i=1;
printf("\n-All customers in the system-\n\n");
while(N01 != NULL){
printf("%d-)%s\n",i,N01->name);
N01 = N01->nextCustomer;
i = i + 1;
}
printf("-------------------\n\n");
}
void printCustomerType(Customer *N01, int type){
int i=1;
if(type == 1){
printf("\n-All corporate customers in the system-\n\n");
while(N01 != NULL){
if(N01->type == 1){
printf("%d-%s\n",i,N01->name);
i = i + 1;
}
N01 = N01->nextCustomer;
}
}
else if(type == 2){
printf("\n-All individual customers in the system-\n\n");
while(N01 != NULL){
if(N01->type == 2){
printf("%d-)%s\n",i,N01->name);
i = i + 1;
}
N01 = N01->nextCustomer;
}
}
}
void printCustomer(Customer *N01, char name[]){
while(N01 != NULL){
if(strcmp(N01->name,name) == 0){
printf("\nCustomer ID: %d\nCustomer name: %s\nCustomer x coordinate: %lf\nCustomer y coordinate: %lf\n\n",N01->ID,N01->name,N01->coordX,N01->coordY);
}
N01 = N01->nextCustomer;
}
}
And here is where and how i call the function. It's in Category 2 and subcategory 4. After i run the function, i'm trying to print all of them in Category 2 and subcategory 1. It doesn't appear there
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "salesautomation.h"
int main(){
int exit = 0;
while(exit == 0){
// PREDEFINED CUSTOMERS
Customer *N01;
Customer *N02;
Customer *N03;
N01 = malloc(sizeof(Customer));
N02 = malloc(sizeof(Customer));
N03 = malloc(sizeof(Customer));
N01->ID = 1;
strcpy(N01->name,"Kutay Sahlankaya");
N01->type = 2;
N01->coordX = 3;
N01->coordY = 4;
N01->nextCustomer = N02;
N02->ID = 2;
strcpy(N02->name,"Servet Sahlankaya");
N02->type = 1;
N02->coordX = 6;
N02->coordY = 8;
N02->nextCustomer = N03;
N03->ID = 3;
strcpy(N03->name,"Tuba Gozel");
N03->type = 1;
N03->coordX = 20;
N03->coordY = 15;
N03->nextCustomer = NULL;
////////////////////////////
printf("----------- SALES AUTOMATION PROGRAM -----------\n\n");
char category[25];
int categoryNum=0, check1=0;
categoryNum = 0;
check1 = 0;
strcpy(category,"");
while(check1 == 0){
printf("Please choose the category that you want to use from below\n\n");
printf("1- Sale\n2- Customer Informations\n3- Product Informations\n4- Customer Analysis\n5- Product Analysis\n6- Exit\n\n");
scanf("%s", &category);
categoryNum = categoryFilter(category);
if(categoryNum == 0){
printf("\n[ERROR]- Please read and type in carefully!\n\n");
}
else{
check1 = 1;
}
}
// CATEGORY 1
if(categoryNum == 1){
char cat1_name[20];
printf("\nCustomer name : ");
scanf("%s",&cat1_name);
if(customerChecker(cat1_name) == 0){
int check2 = 0;
while(check2 == 0){
char checker;
printf("\nEntered customer name is not recognized, do you want to add a new customer?(Y\\N)\n");
scanf("%c", &checker);
if(checker == 'Y'){
customerAdd(&N01);
check2 = 1;
}
else if(checker == 'N'){
printf("\nTerminating sale process...\n");
check2 = 1;
}
else{
printf("[ERROR]- Please enter your answer as given.\n");
}
}
}
}
//CATEGORY 2
else if(categoryNum == 2){
int subcategory2;
printf("\n-Customer Informations-\n\nPlease choose the category that you want to use from below. Please use category numbers.\n\n1-All Customers\n2-According to the customer type\n3-Single Customer\n4-Add Customer\n\n");
scanf("%d", &subcategory2);
if(subcategory2 == 1){
printAllCustomers(N01);
}
else if(subcategory2 == 2){
int cust_type;
printf("Which customer type do you want to see? Please use numbers\n1-Individual\n2-Corporate\n\n");
scanf("%d",&cust_type);
printCustomerType(N01,cust_type);
}
else if(subcategory2 == 3){
char junk;
char cat2_name[30];
printf("\nPlease enter the customer name in Fffff Sssss format : ");
getchar(); // If i don't put this here, gets scans \n when user hits enter. So i put this here to save \n to nowhere
gets(cat2_name);
printCustomer(N01,cat2_name);
}
else if(subcategory2 == 4){
customerAdd(&N01);
}
}
else if(categoryNum == 3){
}
else if(categoryNum == 4){
}
else if(categoryNum == 5){
}
else if(categoryNum == 6){
printf("\n\nTerminating program...");
return 0;
}
}
}
And here is the header file.
#ifndef saleautomation_h_
#define saleautomation_h_
struct Customer{
int ID;
char name[30];
int type;
double coordX;
double coordY;
struct Customer *nextCustomer;
};
typedef struct Customer Customer;
typedef Customer * CustomerPtr;
struct product{
int ID;
char name[20];
int type;
double price;
struct product *nextproduct;
};
typedef struct product product;
typedef product * productptr;
struct sold{
int ID;
int billID;
int customerID;
int productID;
int quantity;
double cost;
struct sold *nextsold;
};
typedef struct sold sold;
typedef sold * soldptr;
int categoryFilter(char category[]);
int customerChecker(char name[]);
void customerAdd(Customer **N01);
void printAllCustomers(Customer *N01);
void printCustomerType(Customer *N01, int type);
void printCustomer(Customer *N01, char name[]);
#endif
The function works and i write values that i want it to hold. The problem is, I can't add a new node globally. When I print the whole linked list, it doesn't appear at the and it prints only predefined nodes. I think it just adds it in this function but i want to make it globally in code and access it in main code. I've tried to use pointers and etc. but i couldn't manage to do it. Can you help me please?
All of this:
// PREDEFINED CUSTOMERS
Customer *N01;
Customer *N02;
Customer *N03;
N01 = malloc(sizeof(Customer));
N02 = malloc(sizeof(Customer));
N03 = malloc(sizeof(Customer));
N01->ID = 1;
strcpy(N01->name, "Kutay Sahlankaya");
N01->type = 2;
N01->coordX = 3;
N01->coordY = 4;
N01->nextCustomer = N02;
N02->ID = 2;
strcpy(N02->name, "Servet Sahlankaya");
N02->type = 1;
N02->coordX = 6;
N02->coordY = 8;
N02->nextCustomer = N03;
N03->ID = 3;
strcpy(N03->name, "Tuba Gozel");
N03->type = 1;
N03->coordX = 20;
N03->coordY = 15;
N03->nextCustomer = NULL;
is in the wrong place. You have it inside your while loop. As a result, after each operation two things happen:
The memory they point to (and anything you did on the last iteration) is leaked.
They're reset to their starting values (with new allocations) that will be summarily leaked after the next iteration.
Move all of that above the entrance to the while-loop in main:
int main()
{
// PREDEFINED CUSTOMERS
Customer *N01;
Customer *N02;
Customer *N03;
N01 = malloc(sizeof(Customer));
N02 = malloc(sizeof(Customer));
N03 = malloc(sizeof(Customer));
N01->ID = 1;
strcpy(N01->name, "Kutay Sahlankaya");
N01->type = 2;
N01->coordX = 3;
N01->coordY = 4;
N01->nextCustomer = N02;
N02->ID = 2;
strcpy(N02->name, "Servet Sahlankaya");
N02->type = 1;
N02->coordX = 6;
N02->coordY = 8;
N02->nextCustomer = N03;
N03->ID = 3;
strcpy(N03->name, "Tuba Gozel");
N03->type = 1;
N03->coordX = 20;
N03->coordY = 15;
N03->nextCustomer = NULL;
int exit = 0;
while (exit == 0)
{
////////////////////////////
printf("----------- SALES AUTOMATION PROGRAM -----------\n\n");
...
Below is a program called scrabble.c that is being used to play a simplified version of the game. The user is dealt 7 random characters and then told to enter a word using those characters. The program should check if the user then uses the letters given in order to create a valid word. I am having a problem with the function that checks to see if the user enters a valid word.
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#define SIZE_LETTER_SET 100
#define SIZE_CHOOSEN_LETTER 7
void generate_letter_set(int letter_set[7], int size_letter_set, int
num_letters)
{
//num_letters should be 7
//size_letter_set should be 100
bool choosen[SIZE_LETTER_SET] = {false};
int group[SIZE_LETTER_SET] =
{'A','A','A','A','A','A','A','A','A','B','B','C','C','D','D','D','D','E','E','E','E','E','E','E','E','E','E','E','E','F','F','G','G','G','H','H',
'I','I','I','I','I','I','I','I','I','J','K','L','L','L','L','M','M','N','N','N','N','N','N','O','O','O','O','O','O','O','O','P','P','Q','R','R','R','R','R','R','S','S',
'S','S','T','T','T','T','T','T','U','U','U','U','V','V','W','W','X','Y','Y','Z',' ',' '};
int c, letter, letterfound;
srand((unsigned) time(NULL));
for(c = 0; c < num_letters; c++)
{
letterfound = 1;
while(letterfound % 2 != 0)
{
letter = rand() % 100;
if(choosen[letter] == false)
{
letter_set[c] = group[letter];
choosen[letter] = true;
letterfound++;
}
}
}
printf("Your letters are: ");
for(c = 0; c < num_letters; c++)
{
printf("%c ", letter_set[c]);
}
printf("\n");
return;
}
int read_word(char word[], int max_size_word)
{
int c = 0, input_count = 0;
printf("Please enter your word : ");
char user_input = getchar();
for(c = 0; c < max_size_word; c++)
{
if(user_input != '\n')
{
word[c] = user_input;
input_count++;
//printf("input_count = %d, letter entered = %c\n", input_count,
user_input);
}
else if(user_input == '\n')
{
return input_count;
}
user_input = getchar();
}
return input_count;
}
This function is where the error occurs, the function should check if the user is using their 7 letters to create a valid word.
_Bool check_word (char word[], int size_word, int letter_set[], int
size_letter_set)
{
printf("beginning word check : \n");
int c = 0;
int b = 0;
int dup_count = 0;
int pair_found = 0;
for(c = 0; c < size_word; c++);
pair_found = 1;
for(b = 0; b < size_letter_set; b++)
{
printf("Checking if %c is equal to %c", word[c], letter_set[b]);
if((toupper(word[c])) == letter_set[b])
{
letter_set[b] = 0;
dup_count++;
pair_found++;
}
}
printf("Total duplicates = %d : check word is ", dup_count);
if((dup_count >= size_word))
{
printf("true");
return true;
}
else
{
printf("false");
return false;
}
}
int main(void)
{
int letter_set[7] = {0};
char word[7] = {0};
int size_letter_set = 100;
int num_letters = 7;
generate_letter_set(letter_set, size_letter_set, num_letters);
int size_word = read_word(word, num_letters);
check_word(word, num_letters, letter_set, size_letter_set);
return 0;
}
I have a school assignment to make a hangman game. The game works how I want it to except for one small glitch. If the user entered word is 4 letters or less, the hidden word is displayed with an extra "_\377" at the end. When the user entered word is 5 letters or more, then there is no glitch. I am hoping that someone would be kind enough to help me trouble shoot the problem. Thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int letterfinder(char string[], char a, int vari)
{
int length = strlen(string);
int i = vari;
int val = 0;
while( i <= length && val != 1)
{
if( string[i] == a)
{
val = 1;
}
i++;
}
if( val == 0)
{
return 100;
}
else
{
return i;
}
}
int main()
{
char inWord[] = "1111111111111111111111111111";
char outWord2[] = "1111111111111111111111111111";
char guess;
int gameover = 0;
int trys = 10;
int vari = 0;
printf("Please enter a word: ");
gets(inWord);
printf("%s\n", inWord);
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
int i2 = 0;
int j2 = 0;
int i3 = 0;
i2 = strcspn(inWord, outWord2);
char outWord[80];
while(i3 < i2)
{
outWord[i3] = '1';
i3++;
}
while(j2 < i2)
{
outWord[j2] = '-';
j2++;
}
puts(outWord);
while(gameover != 1 )
{
printf("What is your guess: ");
scanf("%s", &guess);
vari = 0;
if(letterfinder(inWord, guess, vari) == 100)
{
printf("Wrong!");
trys--;
printf("You have %d attempts left\n", trys);
if(trys == 0)
{
gameover = 1;
printf("You ran out of attempts. Game over\n");
}
}
else
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = (letterfinder(inWord, guess, vari));
while(letterfinder(inWord, guess, vari) != 100)
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = letterfinder(inWord, guess, vari);
}
puts(outWord);
}
int value = 0;
i3 = 0;
while( i3 <= i2)
{
if( outWord[i3] == '-')
{
value = 1;
}
i3++;
}
if(value != 1)
{
printf("Congratulations, you have guessed the word!\n");
gameover = 1;
}
}
return 0;
}
Your code has Undefined Behaviour. In the cases it "works" it is only by chance/luck. char guess; scanf("%s", &guess); That causes memory corruption as you are writing a string to a variable that can only hold a single char. Even a single letter guess will require two characters to store as all C strings are NUL terminated.
– kaylum
I'm trying to figure out why I cannot delete elements from the dict array. Could somebody help me out? The function removeWord is working as it is supposed to when removing the last added word, but not when trying to remove some other word.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUMBER_OF_WORDS 11
int clear(){
while(getchar()^'\n');
return 0;
}
int numberOfWordsInDict(char **dict){
int i = 0;
int c1 = 0;
for (i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
if (dict[i] != 0){
c1++;
}
}
return c1;
}
void addWord(char **dict, char *word){
int c1 = numberOfWordsInDict(dict);
char *word1;
if (c1 >= 0 && c1 < 10){
word1 = (char*) malloc(sizeof(char)*(strlen(word)+1));
dict[c1] = word1;
strncpy(dict[c1], word, strlen(word));
dict[c1][strlen(word)] = '\0';
} else if (c1 >= 10){
printf("Dictionary is already full!\n");
}
}
void printDict(char **dict){
int i = 0;
int c1 = numberOfWordsInDict(dict);
printf("Dictionary:\n");
if (c1 == 0){
printf("The dictionary is empty.\n");
} else if (c1 > 0 && c1 <= 10){
while (dict[i] != NULL){
printf("- %s\n", dict[i]);
i++;
}
}
}
void removeWord(char **dict, char *word){
int i = 0;
for (i = 0; i < MAX_NUMBER_OF_WORDS; i++){
if (strncmp(dict[i], word, strlen(word)+1) == 0){
dict[i] = 0;// can only delete the last element of dict properly.
break;
}
}
}
int main(){
char *dict[MAX_NUMBER_OF_WORDS] = {};
char word[1024] = {};
char command;
while(1){
printf("Command (a/p/r/q): ");
while(scanf("%s", &command) == 1){
break;
}
;
clear();
if (command == 'a'){ // add word
scanf("%[^\n]s", &word);
clear();
addWord(dict, word);
} else if (command == 'p'){ // print dict
printDict(dict);
} else if (command == 'r'){ // remove word
printf("Remove a word: ");
scanf("%[^\n]s", &word);
clear();
removeWord(dict, word);
} else if (command == 'q'){ // quit
break;
}
}
int i = 0;
for (i = 0; i < MAX_NUMBER_OF_WORDS; i++){
free(dict[i]);
}
return 0;
}
example input:
a
dog
a
cat
a
apple
case 1:
r
apple
p
// output =
Dictionary:
- dog
- cat
a
uniform
p
// output =
Dictionary:
- dog
- cat
- uniform
// works fine
case 2
r
cat
p
// output =
Dictionary:
- dog
a
book
p
// output =
Dictionary:
- dog
// doesn't work as expected
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUMBER_OF_WORDS 11
int clear(){
while(getchar()^'\n');
return 0;
}
int numberOfWordsInDict(char **dict){
int c1 = 0;
for(int i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
if (dict[i] != 0){
c1++;
}
}
return c1;
}
int vacancy(char **dict){
for(int i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
if (dict[i] == 0){
return i;
}
}
return -1;
}
void addWord(char **dict, char *word){
int c1 = vacancy(dict);//It is not possible to use the registration number as an additional index.
if (-1 != c1){
dict[c1] = malloc(strlen(word)+1);
strcpy(dict[c1], word);
} else {
printf("Dictionary is already full!\n");
}
}
void printDict(char **dict){
int c1 = numberOfWordsInDict(dict);
printf("Dictionary:\n");
if (c1 == 0){
printf("The dictionary is empty.\n");
} else {
for(int i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
if(dict[i])
printf("- %s\n", dict[i]);
}
}
}
void removeWord(char **dict, char *word){
for(int i = 0; i < MAX_NUMBER_OF_WORDS; i++){
if (strcmp(dict[i], word) == 0){
free(dict[i]);//need free
dict[i] = 0;// can only delete the last element of dict properly.
break;
}
}
}
int main(){
char *dict[MAX_NUMBER_OF_WORDS] = { NULL };
char word[1024] = { 0 };//forbids empty initializer braces
char command;
while(1){
printf("Command (a/p/r/q): ");
scanf("%c", &command);//%s buffer over run
clear();
if (command == 'a'){ // add word
scanf("%[^\n]", word);
clear();
addWord(dict, word);
} else if (command == 'p'){ // print dict
printDict(dict);
} else if (command == 'r'){ // remove word
printf("Remove a word: ");
scanf("%[^\n]", word);
clear();
removeWord(dict, word);
} else if (command == 'q'){ // quit
break;
}
}
int i = 0;
for (i = 0; i < MAX_NUMBER_OF_WORDS; i++){
free(dict[i]);
}
return 0;
}
Inside printDict() use
while (i<MAX_NUMBER_OF_WORDS){
if(dict[i] != 0)
printf("- %s\n", dict[i]);
i++;
}
instead of
while (dict[i] != NULL){
printf("- %s\n", dict[i]);
i++;
}
because even if you know if there are c1 number of words, you don't know their location as in where those c1 words are present.
Also change
while(scanf("%s", &command) == 1)
to
while(scanf("%c", &command) == 1)
since command is of char type.
So I'm trying to get this code to draw a card, and then each time printcards in called it prints the previous card and the new card. Right now it it prints off one card and then another while I want it to constantly update the hand every time it's called.
example:
first call
prints: Ace of Spades
Second call:
Prints: Ace of Spades, 2 of Hearts
etc...
for sake of agrument assume the hand will never grow bigger than 10 cards.
Any help will be appreciated.
Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 52
enum faces{Ace = 0, Jack = 10, Queen, King};
char * facecheck(int d);
void shuffle( int deck[]);
void draw(int deck[SIZE]);
void printcards(int hand[], int numCards);
void players(int deck[]);
int question();
int i, //numCards = 1;
int top = 52;
int main()
{
int deck[SIZE], i, a;
char suits[4][9] =
{
"Hearts",
"Diamonds",
"Clubs",
"Spades"};
srand( time( NULL ) ) ;
for(i = 0; i<SIZE; i++)
{
deck[i] = i;
};
shuffle(deck);
players(deck);
return 0;
}
char * facecheck(int d)
{
static char * face[] =
{
"Ace",
"Jack",
"Queen",
"King" };
if(d == Ace)
return face[0];
else
{
if(d == Jack)
return face[1];
else
{
if(d == Queen)
return face[2];
else
{
if(d == King)
return face[3];
}
}
}
}
void shuffle( int deck[])
{
int i, j, temp;
for(i = 0; i < SIZE; i++)
{
j = rand() % SIZE;
temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
printf("The deck has been shuffled \n");
}
void draw(int deck[SIZE])
{
int numCards = 1;
int i;
int hand[numCards];
int card;
for(i = 0; i < numCards && top > 0; i++)
{
card = deck[top-1];
hand[i] = card;
top--;
}
printcards(hand, numCards);
//numCards++;
}
void printcards(int hand[], int numCard)
{
char suits[4][9] =
{
"Hearts",
"Diamonds",
"Clubs",
"Spades"};
for(i = 0; i < numCard; i++)
{
int card = hand[i];
if(card%13 == 0 || card%13 == 10 || card%13 == 11 || card%13 == 12)
printf("%s ", facecheck(card%13) );
else
printf("%d ", card%13+1);
printf("of %s \n", suits[card/13]);
}
}
void players(int deck[])
{
int x;
int a;
int yourhand[10];
a = 1;
printf("Player 1 \n");
printf("Your Hand is: \n");
draw(deck);
while(a == 1)
{
printf("What would you like to do: Press 1 to Draw. 2 to Stay. \n");
scanf("%d" , &x);
if(x == 1)
{
draw(deck);
}
else
{
a--;
}
}
}
First, in general, your functions should perform one distinct, well-defined piece of work. In this case, you probably don't want your card drawing function to also be responsible for printing output. In addition, it's not clear what the players function logically represents. I'd recommend you refactor the code with these points in mind.
Second, you'll need to persist your hand state between card draws. You hinted at this with int yourhand[10]; in the players function, but you never used it.
I adjusted the draw function to return the card it pulled, and to update your total hand between draws:
Draw Function
int draw(int deck[SIZE])
{
int numCards = 1;
int i;
int hand[numCards];
int card;
for(i = 0; i < numCards && top > 0; i++)
{
card = deck[top-1];
hand[i] = card;
top--;
}
return card;
//numCards++;
}
Players Function
void players(int deck[])
{
int x;
int a;
int yourhand[10];
int handIndex = 0;
a = 1;
printf("Player 1 \n");
printf("Your Hand is: \n");
while(a == 1)
{
printf("What would you like to do: Press 1 to Draw. 2 to Stay. \n");
scanf("%d" , &x);
if(handIndex == 9)
{
break;
}
else if(x == 1)
{
yourhand[handIndex] = draw(deck);
}
else
{
a--;
}
printcards(yourhand, handIndex+1);
handIndex++;
}
}