Error: expected ';' before '{' token - c

I was recently coding a short little fallout shelter survival game and I came across the error "expected ';' before '{' token". The error is around line 28, character 23:
#include <stdio.h>
int main(int argc, char *argv[])
{//setup changing variables
int X = 0;
int money = 300;
int resources[10];
resources[0] = 100;
resources[1] = 200;
printf("Welcome!\nFirst we will choose the extra supplies for your shelter!");
while(X < 1)
{
//Setup the input
char input[15];
//Introduction:
printf("You have $%d dollars\n1: Cards ($4)\n2: Ounce of Gold ($20)\n3: 10 Pounds of Spare Parts($20)\n4: Ham Radio ($75)\n5: Calendar ($5)\n6: Book of Blueprints ($10)\n7: (2 Gallons) Extra Water Storage ($6)\n8: (1 gallon) Fuel ($10)\n9: Check Current Supplies\n10:Start\n", money);
//Have player buy Items
if(input == 1){
if(money > 3){
money = money - 4;
resources[2] = resources[2] + 1;
printf("You have purchased a deck of cards...\n");
}
else{
printf("You can not afford this item\n");
}
}
elif(input == 2)
{
if(money > 19)
{
money = money - 20;
resources[3] = resources[3] + 1;
printf("You have purchased an Ounce of Gold...\n");
}
else
{
printf("You do not have enough money\n");
}
}
elif(input == 3){
if(money > 19){
money = money - 20;
resources[4] = resources[4] + 10;
printf("You have purchased 10 pounds of Scrap Parts...\n");
}
else{
printf("You do not have enough money\n");
}
}
elif(input == 4){
if(money > 74){
money = money - 75;
resources[5] = resources[5] + 1;
printf("You have purchased 1 Ham Radio...\n");
}
else{
printf("You do not have enough money.\n");
}
}
elif(input == 5)
{
if(money > 4)
{
money = money - 5;
recources[6] = resources[6] + 1;
printf("You have just purchased a calendar...\n");
}
else
{
printf("You can not afford the calendar.\n");
}
}
elif(input == 6)
{
if(money > 9){
money = money - 10;
resources[7] = resources[7] + 1;
printf("You have just purchased a Book filled with Blueprints...\n");
}
else{
printf("You can not afford the Book of Blueprints.\n");
}
}
elif(input == 7){
if(money > 5){
int money = money - 6;
int resources[8] = resources[8] + 2;
printf("You have purchased 2 extra gallons of water storage...");
}
else{
printf("You do not have enough money...");
}
}
elif(input == 8){
if(money > 9){
money = money - 10;
resources[9] = resources[9] + 1;
printf("You have purchased one gallon of fuel...");
}
else{
printf("You can not afford the fuel...");
}
}
elif(input == 9){
printf("You currently have: \n%s Pounds of Food\n%s Gallons of Water\n%s Decks of Cards\n%s Ounces of Gold\n%s Pounds of Spare Parts\n%s Ham Radios\n%s Calendars\n%s Books of Blueprints\n%s Gallons of Fuel", resources[0], resources[8] + resources[1], resources[2], resources[3], resources[4], resources[5], resources[6], resources[7], resources[9]);
fgets(input, sizeof(input), stdin);
}
elif(input == 10){
break;
}
else
{
printf("Input Invalid");
fgets(input, sizeof(input), stdin);
}
}
int h = 100;
while(int h > 0)
{
printf("%d", h);
h = h - 1;
}
}

You cannot use elif in C. You should use else if instead.
In printf, %s is not for printing integers. You should use %d instead.
With char input[15];, comparations like input == 1 make no sense. You may use int char;.
input is uninitialized. You should read some data to that.
recources is undefined. It should be typo of resources.
I changed the type of input to int and put scanf before the branches, so remove fgets(input, sizeof(input), stdin);
Remove excess ints in int money = money - 6;, int resources[8] = resources[8] + 2; and while(int h > 0)
The arguments aren't used, so change int main(int argc, char *argv[]) into int main(void). (optional to avoid warnings)
OK, now compile errors are gone.
#include <stdio.h>
int main(void)
{//setup changing variables
int X = 0;
int money = 300;
int resources[10];
resources[0] = 100;
resources[1] = 200;
printf("Welcome!\nFirst we will choose the extra supplies for your shelter!");
while(X < 1)
{
//Setup the input
int input;
//Introduction:
printf("You have $%d dollars\n1: Cards ($4)\n2: Ounce of Gold ($20)\n3: 10 Pounds of Spare Parts($20)\n4: Ham Radio ($75)\n5: Calendar ($5)\n6: Book of Blueprints ($10)\n7: (2 Gallons) Extra Water Storage ($6)\n8: (1 gallon) Fuel ($10)\n9: Check Current Supplies\n10:Start\n", money);
//Have player buy Items
if(scanf("%d", &input) != 1) return 1;
if(input == 1){
if(money > 3){
money = money - 4;
resources[2] = resources[2] + 1;
printf("You have purchased a deck of cards...\n");
}
else{
printf("You can not afford this item\n");
}
}
else if(input == 2)
{
if(money > 19)
{
money = money - 20;
resources[3] = resources[3] + 1;
printf("You have purchased an Ounce of Gold...\n");
}
else
{
printf("You do not have enough money\n");
}
}
else if(input == 3){
if(money > 19){
money = money - 20;
resources[4] = resources[4] + 10;
printf("You have purchased 10 pounds of Scrap Parts...\n");
}
else{
printf("You do not have enough money\n");
}
}
else if(input == 4){
if(money > 74){
money = money - 75;
resources[5] = resources[5] + 1;
printf("You have purchased 1 Ham Radio...\n");
}
else{
printf("You do not have enough money.\n");
}
}
else if(input == 5)
{
if(money > 4)
{
money = money - 5;
resources[6] = resources[6] + 1;
printf("You have just purchased a calendar...\n");
}
else
{
printf("You can not afford the calendar.\n");
}
}
else if(input == 6)
{
if(money > 9){
money = money - 10;
resources[7] = resources[7] + 1;
printf("You have just purchased a Book filled with Blueprints...\n");
}
else{
printf("You can not afford the Book of Blueprints.\n");
}
}
else if(input == 7){
if(money > 5){
money = money - 6;
resources[8] = resources[8] + 2;
printf("You have purchased 2 extra gallons of water storage...");
}
else{
printf("You do not have enough money...");
}
}
else if(input == 8){
if(money > 9){
money = money - 10;
resources[9] = resources[9] + 1;
printf("You have purchased one gallon of fuel...");
}
else{
printf("You can not afford the fuel...");
}
}
else if(input == 9){
printf("You currently have: \n%d Pounds of Food\n%d Gallons of Water\n%d Decks of Cards\n%d Ounces of Gold\n%d Pounds of Spare Parts\n%d Ham Radios\n%d Calendars\n%d Books of Blueprints\n%d Gallons of Fuel", resources[0], resources[8] + resources[1], resources[2], resources[3], resources[4], resources[5], resources[6], resources[7], resources[9]);
}
else if(input == 10){
break;
}
else
{
printf("Input Invalid");
}
}
int h = 100;
while(h > 0)
{
printf("%d", h);
h = h - 1;
}
}
Enjoy!
Sorry, I didn't enjoy but got annoyed.

In c elif is not there. So, it gives that error.
Change the elif to else if and do validations.

Related

Vending Machine - Logic Error

// My issue is a rather specific one, the code compiles "Insert Coins" "Coin
//not accepted " indefinitely and doesn't allow input at all. I've tried this
//program with "While" only loops and "do" loops and it always compiles
//indefinitely without allowing input.I'm trying to figure out where my logic
//error is and possibly a simpler solution if possible. Thanks.
#include <stdio.h>
int main(void){
int money, drink_selection, coins;
money = 0;
do{ //ISSUE HERE??
do{ //OR HERE??
printf("Insert Coins: "); //REPEATS THIS
scanf("%d" ,&coins); //DOESNT ALLOW THIS
if(coins == 0 || coins == 5 || coins == 10 || coins == 25)
{
money +=coins;
}
else {
printf("Coin not accepted \n");//REPEATS INDEFINITELY
}
}while(coins != 0); // looping here?
printf("Please select from the following menu: 1 - Coffee or 2 - Tea ) \n");
printf("Enter your choice: \n");
scanf("%d", &drink_selection);
switch(drink_selection){
case 1:
printf("You have selected coffee as your choice. \n");
money-=25;
if (money >=0){
printf("Please take your coffee. \n");
}
break;
case 2:
money-=15;
if (money >= 0){
printf("Tea dispensing \n");
}
break;
default:
printf("Ivalid input");
break;
}
if (money > 0){
printf("Your change is %d cents ", &coins);
}
else if (money < 0){
printf("Insufficient amount, your change is: %d", &coins);
}
}while(money == 0); //POSSIBLY ISSUE IS HERE?
return 0;
}

Deposit gives negative number?

Good morning, guys! I'm currently a newly started programming learner. Down here is my code for a mini app store. However, there is a problem going on, yet I couldnt locate the problem.
The problem happen when I tried buying an app for $89.99 and I chose to redeem $10 9 times so I would have enough money to purchase the app (I didnt choose the $100 option because it would work just fine). However, the remained amount became $-79.99 instead of $0.01. Like I said, if I chose to deposit $100, the remained balance would be $10.01, which is normal. I don't get where I did wrong. Here is my code!
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
int Compare(double deposit, double choiceCost);
void DisplayApps(char *selectionPtr);
void SetCost(char selection, double *costPtr);
void PaymentOptions(double *depositPtr,double cost);
void DoItAgain(char *quitPtr);
//void Pay(double *depositPtr, double choiceCost);
void GetChange(double *depositPtr, double choiceCost);
void DoItAgain(char *quitPtr);
int main()
{
char selectionPtr;
char selection;
char quitPtr;
double costPtr;
double deposit = 0.0;
double choiceCost;
double depositPtr = 0.0;
double cost = 0.0;
printf("Welcome to the App Store\n");
printf("***************************\n\n");
printf("Your deposit is: %.2f\n", deposit);
printf("\n");
while (1)
{
do {
DisplayApps(&selectionPtr);
selection = selectionPtr;
SetCost(selection, &costPtr);
choiceCost = costPtr;
Compare(deposit, choiceCost);
while (Compare(deposit, choiceCost) == 0)
{
printf("Your balance isn't enough.\n");
printf("In order to purchase this item, you have to redeem more money.\n");
PaymentOptions(&depositPtr, cost);
deposit += depositPtr;
printf("You have redeemed $%.2f\n", depositPtr);
printf("Your balance now is: $%.2f\n", deposit);
printf("\n");
}
deposit = depositPtr;
GetChange(&depositPtr, choiceCost);
DoItAgain(&quitPtr);
} while (quitPtr == 'Y' || quitPtr == 'y');
return 1;
}
return 0;
}
void DisplayApps(char *selectionPtr)
{
printf("-------------------------\n");
printf("HERE ARE THE SLECTIONS\n");
printf("C -- Clown Punching $299.99\n");
printf("V -- Virtual Snow Globe $349.99\n");
printf("R -- Remote PC $999.99\n");
printf("G -- Grocery List Helper $2.99\n");
printf("M -- Mobile Cam Viewer $89.99\n");
printf("\n");
printf("Please enter a selection: ");
scanf(" %c", &*selectionPtr);
printf("\n");
}
void SetCost(char selection, double *costPtr)
{
if (selection == 'C' || selection == 'c')
{
*costPtr = 299.99;
}
else if (selection == 'V' || selection == 'v')
{
*costPtr = 349.99;
}
else if (selection == 'R' || selection == 'r')
{
*costPtr = 999.99;
}
else if (selection == 'G' || selection == 'g')
{
*costPtr = 2.99;
}
else if (selection == 'M' || selection == 'm')
{
*costPtr = 89.99;
}
}
int Compare(double deposit, double choiceCost)
{
if (deposit < choiceCost)
{
return 0;
}
else
{
return 1;
}
}
void PaymentOptions(double *depositPtr, double cost)
{
printf("You have (4) options to choose:\n");
printf("1 - $1000.00\n");
printf("2 - $500.00\n");
printf("3 - $100.00\n");
printf("4 - $10.00\n");
printf("How much do you want to redeem?");
printf(">>>>> ");
scanf("%lf", &cost);
printf("\n");
printf("-------------------------------------\n");
if (cost == 1)
{
*depositPtr = 1000.00;
}
else if (cost == 2)
{
*depositPtr = 500.00;
}
else if (cost == 3)
{
*depositPtr = 100.00;
}
else if (cost == 4)
{
*depositPtr = 10.00;
}
}
void GetChange(double *depositPtr, double choiceCost)
{
*depositPtr = *depositPtr - choiceCost;
printf("You have purchased this item successfully.\n");
printf("You still have $%.2f remained in you balance.\n", *depositPtr);
}
void DoItAgain(char *quitPtr)
{
printf("Do you want to continue purchase another item? [Y/N]\n");
scanf(" %c", &*quitPtr);
}
In this code : GetChange(&depositPtr, choiceCost);
You shold pass deposit (total deposit) and not &depositPtr (last deposit, only 10)

Check for validation if Input exceeds the available stocks

Can anybody help me fix the validation of my program
actual output of my program:
choice 1. Import I input 2 times, my 1st input is 100 2nd input is 200
so when viewing the choice 2. Storage output will be this
Year sets
1 100
2 200
total of 300 sets of computer in storage
now in choice 3. Sell Order I input 400 so it will display Sorry we have No enough Stocks !
my problem is when viewing my 3. Storage again all stocks are now empty
Year sets
1 0
2 0
I am expecting that when my Input exceeds the available stocks it will not continue reduced the stocks in my storage
int main(void) {
int choice = 0;
int year = 1, i, com;
int storage[99] = { 0 };
for (;;) {
clrscr();
printf("Year %d\n\n", year);
printf("1. Import\n");
printf("2. Storage\n");
printf("3. Sell Order\n");
printf("\nchoice: ");
scanf("%d", &choice);
if (choice == 1) { // import
clrscr();
printf("Enter sets of computer's imported: ");
scanf("%d", &storage[year]);
year++;
}
if (choice == 2) { // storage
clrscr();
printf("Year sets\n");
for (i = 1; i < year; i++) {
printf("%2d %4d\n", i, storage[i]);
}
getch();
}
if (choice == 3) { //order
printf("Enter Sets of Computer ordered: ");
scanf("%d", &com);
for (i = 0; com && i < 99; i++) {
if (com <= storage[i]) {
storage[i] = storage[i] - com;
com = 0;
} else {
com = com - storage[i];
storage[i] = 0;
}
}
if (com > storage[i]) { // validation
printf("Sorry we have No enough Stocks !");
getch();
}
}
}
}
You are wiping the stocks before you have calculated how much stock you have. You want to first add up how much stock you have, check to see if you have enough, THEN wipe the stocks IF you have enough
if (choice == 3) { //order
printf("Enter Sets of Computer ordered: ");
scanf("%d", &com);
for(i=0;i<99;i++) //calculates the amount of stock you have
{
Total_Stock+=storage[i];
}
for (i = 0; com && i < 99; i++) {
if(Total_Stock<com) //If not enough stock it breaks the loop
//before subtracting the stock
{
printf("Not enough stock.\n");
break;
}
if (com <= storage[i]) {
storage[i] = storage[i] - com;
com = 0;
} else {
com = com - storage[i];
storage[i] = 0;
}
}

My C Blackjack program got stuck [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#define CARD_MAX 52
typedef int Deck[CARD_MAX];
Deck deck;
int cardnumber = 0;
int playerwin = 0, dealerwin = 0, tie = 0;
int game();
void setupDeck(Deck deck);
void shuffleDeck(Deck deck);
void printCard(int card);
int pickCard(Deck deck, int cardnum);
void endProgram();
void playAgain();
main()
{
char yn;
srand(time(NULL));
setupDeck(deck);
shuffleDeck(deck);
again:
printf("Welcome! Do you wanna try your luck in a Blackjack Game? If so type 'y'.\n");
printf("If not type 'n' to close the program: ");
scanf_s("%c", &yn);
getchar();
if (yn == 'y')
game();
else if (yn == 'n')
{
printf("Looks like you aren't feeling lucky today! Comeback tomorrow!\n");
system("pause");
}
else
{
printf("Unknown character! Try again!\n");
goto again;
}
}
int game()
{
int card, cardvalue = 0, cardtype = 0, tcvp = 0, tcvd = 0;
int aceflagp = 0, faceflagp = 0, aceflagd = 0, faceflagd = 0, bjp = 0, bjd = 0;
int loopcounter;
char hitorpass;
system("cls");
printf("Your cards:\n");
for (loopcounter = 0; loopcounter < 2; loopcounter++)
{
card = pickCard(deck, cardnumber);
cardnumber++;
cardvalue = card % 13;
cardtype = card / 13;
printCard(cardvalue, cardtype);
if (cardvalue == 1)
{
aceflagp++;
cardvalue = 11;
}
else if (cardvalue == 0 || cardvalue == 11 || cardvalue == 12)
{
faceflagp = 1;
cardvalue = 10;
}
else
cardvalue = cardvalue;
tcvp += cardvalue;
}
if (aceflagp == 1 && faceflagp == 1)
bjp = 1;
printf("Dealers cards:\n");
card = pickCard(deck, cardnumber);
cardnumber++;
cardvalue = card % 13;
cardtype = card / 13;
printCard(cardvalue, cardtype);
if (cardvalue == 1)
{
aceflagd++;
cardvalue = 11;
}
else if (cardvalue == 0 || cardvalue == 11 || cardvalue == 12)
{
faceflagd = 1;
cardvalue = 10;
}
else
cardvalue = cardvalue;
tcvd += cardvalue;
card = pickCard(deck, cardnumber);
cardnumber++;
cardvalue = card % 13;
cardtype = card / 13;
if (cardvalue == 1)
{
aceflagd++;
cardvalue = 11;
}
else if (cardvalue == 0 || cardvalue == 11 || cardvalue == 12)
{
faceflagd = 1;
cardvalue = 10;
}
else
cardvalue = cardvalue;
tcvd += cardvalue;
if (aceflagd == 1 && faceflagd == 1)
bjd = 1;
if (bjp == 1 && bjd == 1)
{
printf("Both sides has Blackjack! Tie!\n");
tie++;
playAgain();
}
else if (bjp == 1)
{
printf("Player Blackjack!\n");
playerwin++;
playAgain();
}
else if (bjd == 1)
{
printf("Dealer Blackjack!\n");
dealerwin++;
playAgain();
}
printf("Your card total is %d.\n", tcvp);
again:
printf("If you want to hit type 'h'. If you want to pass type 'p'");
scanf_s("%c", &hitorpass);
getchar();
if (hitorpass == 'h')
{
card = pickCard(deck, cardnumber);
cardnumber++;
cardvalue = card % 13;
cardtype = card / 13;
printCard(cardvalue, cardtype);
if (cardvalue == 1)
{
aceflagp++;
cardvalue = 11;
}
else if (cardvalue == 0 || cardvalue == 11 || cardvalue == 12)
{
faceflagp = 1;
cardvalue = 10;
}
else
cardvalue = cardvalue;
tcvp += cardvalue;
if (tcvp > 21)
{
if (aceflagp > 0)
{
tcvp -= 10;
aceflagp--;
printf("Your card total is %d.\n", tcvp);
goto again;
}
else
{
printf("Your hand busted. You lost!\n");
dealerwin++;
playAgain();
}
}
else if (tcvp == 21)
{
printf("You have 21 in your hand can't hit more!\n");
goto dealersturn;
}
else if (tcvp < 21)
{
printf("Your card total is %d.\n", tcvp);
goto again;
}
}
else if (hitorpass == 'p')
{
dealersturn:
printf("Dealers turn:\n");
if (tcvd < 17)
{
card = pickCard(deck, cardnumber);
cardnumber++;
cardvalue = card % 13;
cardtype = card / 13;
printCard(cardvalue, cardtype);
if (cardvalue == 1)
{
aceflagd++;
cardvalue = 11;
}
else if (cardvalue == 0 || cardvalue == 11 || cardvalue == 12)
{
faceflagd = 1;
cardvalue = 10;
}
else
cardvalue = cardvalue;
tcvd += cardvalue;
if (tcvd > 21)
{
if (aceflagd > 0)
{
tcvd -= 10;
aceflagd--;
goto dealersturn;
}
else
{
printf("Dealers has a total of %d. Dealers hand busted!\n", tcvd);
playerwin++;
playAgain();
}
}
else if (tcvd < 17)
goto dealersturn;
else
{
if (tcvp > tcvd)
{
printf("Player has %d, dealer has %d. Player won!", tcvp, tcvd);
playerwin++;
playAgain();
}
else if (tcvp == tcvd)
{
printf("Both player and the dealer has %d. Tie!", tcvd);
tie++;
playAgain();
}
else
{
printf("Player has %d, dealer has %d. Dealer won!", tcvp, tcvd);
dealerwin++;
playAgain();
}
}
}
}
else
{
printf("Unknown character. Try again!");
goto again;
}
system("pause");
}
void endProgram()
{
printf("Thanks for playing my blackjack game! The results are:\n");
printf("Player won %d times!\n", playerwin);
printf("Dealer won %d times!\n", dealerwin);
printf("There have been a tie %d times!\n", tie);
system("pause");
}
void playAgain()
{
char chardummy;
again:
printf("Would you like to play again? Type 'y' for Yes and 'n' for No.\n");
scanf_s("%c", &chardummy);
getchar();
if (chardummy == 'y')
{
if (cardnumber >= 48)
{
printf("There is not enough card left in the deck!");
endProgram();
}
else
game();
}
else if (chardummy == 'n')
endProgram();
else
{
printf("Unknown character. Try again!");
goto again;
}
}
void setupDeck(Deck deck)
{
int i;
for (i = 0; i<CARD_MAX; i++)
{
deck[i] = i;
}
}
void shuffleDeck(Deck deck)
{
for (int i = 52 - 1; i >= 0; --i) {
int r = rand() % (i + 1);
int t = deck[i];
deck[i] = deck[r];
deck[r] = t;
}
}
void printCard(int cardvalue, int cardtype)
{
char cardtype2;
if (cardtype == 0)
cardtype2 = 'C';
else if (cardtype == 1)
cardtype2 = 'D';
else if (cardtype == 2)
cardtype2 = 'H';
else if (cardtype == 3)
cardtype2 = 'S';
switch (cardvalue)
{
case 0:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", cardtype2);
printf("* K *\n", cardvalue);
printf("* *\n");
printf("*******\n");
break;
}
case 1:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", cardtype2);
printf("* A *\n");
printf("* *\n");
printf("*******\n");
break;
}
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", cardtype2);
printf("* %2d *\n", cardvalue);
printf("* *\n");
printf("*******\n");
break;
}
case 11:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", cardtype2);
printf("* J *\n", cardvalue);
printf("* *\n");
printf("*******\n");
break;
}
case 12:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", cardtype2);
printf("* Q *\n", cardvalue);
printf("* *\n");
printf("*******\n");
break;
}
}
}
int pickCard(Deck deck, int cardnum)
{
if (cardnumber == 52)
{
printf("There is not enough cards left to complete the game!");
endProgram();
}
else
return deck[cardnum];
}
Here is my code for a simple blackjack game. For some reason after some rounds Dealers turn does not work. I would be gratefull if you could find the reason as I couldn't. And feel free to give tips about improving my code.
You have the following problems in your code that I could see from compiler warnings/errors.
The declaration of printCard does not match its usage and its implementation. Change the declaration to:
void printCard(int cardvalue, int cardtype);
Make the return type of main explicit. Change it to:
int main () ...
Change the lines that print K, J, and Q. You don't need the additional argument to printf in those lines.
printf("* K *\n");
printf("* J *\n");
printf("* Q *\n");
Of the above, the first one is the most crucial fix. With those changes, I was able to run your program without any problem. My test platform: Linux, gcc 4.8.4.
Increase your warning level so you can detect such warnings/errors and fix them in future programming efforts.
Since you asked... :-)
In addition to what R Sahu called out...
The biggest problem (challenge?) I noticed is that I don't see how your endProgram() function actually 'ends' anything. It calls system("pause") and then resumes:
void endProgram()
{
printf("Thanks for playing my blackjack game! The results are:\n");
printf("Player won %d times!\n", playerwin);
printf("Dealer won %d times!\n", dealerwin);
printf("There have been a tie %d times!\n", tie);
system("pause);
}
Which means code like the following in the pickCard() function has unpredictable behavior:
int pickCard(Deck deck, int cardnum)
{
if (cardnumber == 52)
{
printf("There is not enough cards left to complete the game!");
endProgram(); // once we get here what do we return to the caller?
}
else
return deck[cardnum];
}
By "unpredictable" I specifically mean: What will pickCard() return to for cardnumber = 52 ?
I suspect pickCard() is going to call endProgram(), whch being a void function won't do anything but return, then pickCard will fall out of the if-else statement and try to return an integer to the caller, and it will probably be choosing some garbage off the call stack to send back which may (or may not) be a valid card value.
It turns out you call pickCard() in roughly 5 different places, sometimes for the player, sometimes the dealer.
What happens to that logic if pickCard() returns a value that is way out of bounds?
Since you only call shuffleDeck() once at the beginning of main(), eventually you're going to 'exhaust' the deck and get an undefined card value returned from pickCard().
Which kind of fits your symptoms of "it breaks in Dealer after a while...".
So... for general cleanup...
I would take most the variables you have in the game() method and make them globals.
I would add a LOT of print statements for debugging so you can verify the code is doing what you think it will do.
Your calls to system() are a challenge for me on the linux side of the fence:
system("cls");
system("pause");
Since you're devleoping on Windows I understand why you are using system (reminds me of the ancient days when I used to write dos batch files).
But... I would encourage you to ditch the system() calls and write your own input "helper" function that deals with scanf_s or whatever you want to use.
Maybe something like this that can take a prompt and return whatever character the user types.
char prompt_and_read_char( char *msg );
I would encourage you to rewrite without goto statements.
You do attempt a loop in the game() function for the player to draw cards; the for loop, so bonus points for that.
Your overall style and user interaction goals are sound and suggest you'll eventually make a fun game.
The code in game() is a bit over 200 lines of code, which makes it is harder than it needs to be to read.
I would suggest doing something like this in game() function: the goal is to push all card-drawing into functions just for the player, or the dealer.
// Global Variable definitions...
int gameInProcess = 0; // global, set to false.
int tcvp = -1; // obvious bad
// note for global-var police: yeah, I know globals is not "good style".
// But cut our beginning programmer some slack here... this seems like the easiest way for
// them to get their game running. (Think of it as a first step toward cleaning up the
// game() method if that helps any).
// prototypes here.
// main() here.
...etc...
int game() {
gameInProcess = 1; // set to true.
tcvp = 0;
tcvd = 0;
while( gameInProcess ) {
// add plenty of print statements so you see what is going on; you can clean
// them up later by searching on DEBUG.
printf("DEBUG... game(): tcvp=%d tcvd=%d cardnumber=%d\n", tcvp, tcvd, cardnumber );
game_playerDraws( ); // updates tcvp, may set gameInProcess to 0 if player win or busts.
game_dealerDraws( ); // udpates tcvd, may set gameInProcess to 0 if dealer hits blackjack or goes bust.
...other functions for variations...
}
printf("game over, score: player=%d dealer=%d\n", tcvp, tcvd );
// add logic to decide winner.
// return to caller, maybe let them decide if we'll have another game using the same deck.
}
game_playerDraws( ) {
if( !gameInProcess ) {
printf("game_playerDraws(): gameInProcess=%d, nothing to do.\n", gameInProcess );
return;
}
// draw some cards, update globalcs if player wins or breaks.
}
game_dealerDraws( ) {
... do dealer-approriate things...
}
The upside of this is if you exahust the deck, you can just set gameInProcess = 0; and return and everything gracefully stops.
You might add an option to shuffle a new deck and start another game.
But the big advantage is it will give you a clean way to start and end each game, which is something you're missing in the current version of the code.
Good luck. You clearly enjoy programming and are having fun with the writing game.
Stick with it, I think you'll learn a lot.

Payslip generation program in c

this is the question:
Write an interactive program to generate pay slips for the staff of size 12 employees (2 members are clerks, one computer operator, 6 salesmen, 3 helpers) , working in a small chemist retail shop. Assumptions can be made wherever necessary. The payslip should display the employee no., employee name, no. of days worked during the month, date of generation of the payslip, month for which the salary is being paid, all the details of the payment, deductions, gross-pay and net-pay.
when i run the program, it says invalid pointer, even though i havent used a pointer
i am can anyone let me know what mistake(s) are there in this program?
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
char name[30][30], designation[20[20], empid[12][12];
int i;
int n = 12;
int working_days = 27;
float basic[12], days_absent[12], days_present[12], gross_salary[20], pf[12], allowance[12], net[12];
void enter_details_of_employee();
void display();
void get_time();
void main()
{
int k;
printf("Enter 1 to enter employee details and 2 to display salary\n");
scanf("%d", &k);
if(k == 1)
{
enter_details_of_employee();
}
else if(k == 2)
{
display();
get_time();
}
else
{
printf("invalid choice");
}
}
void enter_details_of_employee ()
{
int choice;
int clerk_counter = 0, operator_counter = 0, salesman_counter = 0, helper_counter = 0, max = 0;
do {
printf("\n enter details of employees\n");
printf("enter employee name\n");
scanf("%c", &name);
printf("enter employee id\n");
scanf("%c", &empid);
printf("enter your choice for employee designation\n 1.clerk \n 2.computer operator\n 3. salesman\n 4.helper\n");
scanf("%d", &choice);
if (choice == 1)
{
if(clerk_counter == 2)
{
printf("sorry, you have already entered the details of all clerks\n");
}
else
{
designation = "clerk";
basic = 8000.00;
printf("enter no of days absent\n");
scanf("%d", &days_absent);
days_present = working_days - days_absent;
gross_salary = basic - ((days_absent / working_days) * basic);
pf = gross_salary*0.1;
allowance = gross_salary*0.55;
net = (gross - pf) + allowance;
clerk_counter++;
}
}
else if (choice == 2)
{
if(operator_counter == 1)
{
printf("sorry, you have already entered the details of all computer operators\n");
}
else
{
designation = "computer operator";
basic = 9000;
printf("enter no of days absent\n);
scanf("%d", &days_absent);
days_present = working_days - days_absent;
gross_salary = basic - ((days_absent / working_days) * basic);
pf = gross_salary*0.12;
allowance = gross_salary*0.75;
net = (gross - pf) + allowance;
operator_counter++;
}
}
else if (choice == 3)
{
if(salesman_counter == 6)
{
printf("sorry, you have already entered the details of all salesman\n");
}
else
{
designation = "salesman";
basic = 10000;
printf("enter no of days absent\n);
scanf("%d", &days_absent);
days_present = working_days - days_absent;
gross_salary = basic - ((days_absent / working_days) * basic);
pf = gross_salary*0.15;
allowance = gross_salary*0.95;
net = (gross - pf) + allowance;
salesman_counter++;
}
}
else if (choice == 4)
{
if(salesman_counter == 3)
{
printf("sorry, you have already entered the details of all helpers\n");
}
else
{
designation = "helper";
basic = 6500;
printf("enter no of days absent\n);
scanf("%d", &days_absent);
days_present = working_days - days_absent;
gross_salary = basic - ((days_absent / working_days) * basic);
pf = gross_salary*0.08;
allowance = gross_salary*0.45;
net = (gross - pf) + allowance;
helper_counter++;
}
}
else
{
printf("invalid choice");
}
}
while (max!=12);
}
void get_time()
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
}
void display()
{
printf("SALARY SLIP OF EMPLOYEES ";
printf("---------------------------------------------------------------------------------------------------");
printf("empid\t name\t days_absent\t days_present\t gross_salary\t PF\t allowance\t net");
printf("---------------------------------------------------------------------------------------------------");
for(i=0;i<n;i++)
{
printf(empid[i][i] name[i][i] basic[i] days_absent[i] days_present[i] gross_salary[i] pf[i] allowance[i] net);
}
}
Errors:
1) Syntax Error 1:
char name[30][30], designation[20[20], empid[12][12]; // Bracket mismatch
2) Syntax Error 2:
char designation[20[20];
designation = "clerk"; // What is this?? ..the invalid pointer error
3) Find rest yourself or try compiling with -Wall option.
here is a modified program, any feedback??
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
typedef struct mystruct{
char name[30];
char designation[20];
char empid[8];
float basic;
float days_absent;
float days_present;
float gross_salary;
float pf;
float allowance;
float net;
int counter;
int clerk_counter;
int operator_counter;
int salesman_counter;
int helper_counter;
char *next;
}mystruct;
char *p;
void enter_details_of_employee();
void display();
void get_time();
void main()
{
do
{
int k, choice;
printf("Enter 1 to enter employee details and 2 to display salary\n");
scanf("%d", &k);
if(k == 1)
{
p = (mystruct*)malloc(sizeOf(mystruct));
enter_details_of_employee();
}
else if(k == 2)
{
display();
get_time();
}
else
{
choice = 0;
printf("invalid choice, press 1 to continue\n");
}
}
while choice == 1;
}
void enter_details_of_employee ()
{
if(p->mystruct.counter!=12)
{
int proceed = 1;
do
{
printf("\n enter details of employees\n");
printf("enter employee name\n");
scanf("%u", &p->mystruct.name);
printf("enter employee id\n");
scanf("%u", &p.empid);
printf("enter your choice for employee designation\n 1.clerk \n 2.computer operator\n 3. salesman\n 4.helper\n");
scanf("%d", &choice);
if (choice == 1)
{
if(p->mystruct.clerk_counter == 2)
{
printf("sorry, you have already entered the details of all clerks\n");
}
else
{
p->mystruct.designation = "clerk";
p->mystruct.basic = 8000.00;
printf("enter no of days absent\n");
scanf("%u", &p->mystruct.days_absent);
p->mystruct.days_present = p->mystruct.working_days - p->mystruct.days_absent;
p->mystruct.gross_salary = p->mystruct.basic - ((p->mystruct.days_absent / p->mystruct.working_days) * p->mystruct.basic);
p->mystruct.pf = p->mystruct.gross_salary*0.1;
p->mystruct.allowance = p->mystruct.gross_salary*0.55;
p->mystruct.net = (p->mystruct.gross - p->mystruct.pf) + p->mystruct.allowance;
p->mystruct.clerk_counter++;
p->mystruct.counter++;
}
}
else if (choice == 2)
{
if(p->mystruct.operator_counter == 1)
{
printf("sorry, you have already entered the details of all computer operators\n");
}
else
{
p->mystruct.designation = "computer operator";
p->mystruct.basic = 9000;
printf("enter no of days absent\n);
scanf("%u", &p->mystruct.days_absent);
p->mystruct.days_present = p->mystruct.working_days - p->mystruct.days_absent;
p->mystruct.gross_salary = p->mystruct.basic - ((p->mystruct.days_absent / p->mystruct.working_days) * p->mystruct.basic);
p->mystruct.pf = p->mystruct.gross_salary*0.12;
p->mystruct.allowance = p->mystruct.gross_salary*0.75;
p->mystruct.net = (p->mystruct.gross - p->mystruct.pf) + p->mystruct.allowance;
p->mystruct.operator_counter++;
p->mystruct.counter++;
}
}
else if (choice == 3)
{
if(p->mystruct.salesman_counter == 6)
{
printf("sorry, you have already entered the details of all salesman\n");
}
else
{
p->mystruct.designation = "salesman";
p->mystruct.basic = 10000;
printf("enter no of days absent\n");
scanf("%u", &p->mystruct.days_absent);
p->mystruct.days_present = p->mystruct.working_days - p->mystruct.days_absent;
p->mystruct.gross_salary = p->mystruct.basic - ((p->mystruct.days_absent / p->mystruct.working_days) * p->mystruct.basic);
p->mystruct.pf = p->mystruct.gross_salary*0.15;
p->mystruct.allowance = p->mystruct.gross_salary*0.95;
p->mystruct.net = (p->mystruct.gross - p->mystruct.pf) + p->mystruct.allowance;
p->mystruct.salesman_counter++;
p->mystruct.oounter++;
}
}
else if (choice == 4)
{
if(p.->mystruct.salesman_counter == 3)
{
printf("sorry, you have already entered the details of all helpers\n");
}
else
{
p->mystruct.designation = "helper";
p->mystruct.basic = 6500;
printf("enter no of days absent\n);
scanf("%u", &p->mystruct.days_absent);
p->mystruct.days_present = p->mystruct.working_days - p->mystruct.days_absent;
p->mystruct.gross_salary = p->mystruct.basic - ((p->mystruct.days_absent / p->mystruct.working_days) * p->mystruct.basic);
p->mystruct.pf = p->mystruct.gross_salary*0.08;
p->mystruct.allowance = p->mystruct.gross_salary*0.45;
p->mystruct.net = (p->mystruct.gross - p->mystruct.pf) + p->mystruct.allowance;
p->mystruct.helper_counter++;
p->mystruct.counter++;
}
}
else
{
printf("invalid choice, press 1 to continue\n");
scanf("%d", &proceed);
}
while (proceed);
}
}
else
{
printf("data entry complete");
}
}
}
void get_time()
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
}
void display()
{
printf("SALARY SLIP OF EMPLOYEES ";
printf("---------------------------------------------------------------------------------------------------");
printf("empid\t name\t days_absent\t days_present\t gross_salary\t PF\t allowance\t net");
printf("---------------------------------------------------------------------------------------------------");
while(p.next!=NULL);
{
print(p.mystruct);
}
}

Resources