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++;
}
}
Related
i have written this program (its not finished, there are more functions to be added) however, I want to know why my withinBudget function is sometimes producing correct results and other times producing inaccurate results. The withinBudget works out whether or not the customer has sufficient balance or not. if it returns true it prints purchase has been successful or prints insufficient balance.
#include <stdio.h>
int isItemExist(char itemPrefixes[], char itemPrefix);
void displayMenu(char itemPrefixes[], int itemPrices[], int n);
int withinBudget(int budget, char itemPurchased, char itemPrefixes[], int itemPrices[]);
int n;
int main() {
char itemPrefixes[5];
int itemPrices[5];
int budget = 0;
char itemPurchased;
printf("***ItemPrefixes***");
printf("\nA: Apple\n");
printf("O: Orange\n");
printf("M: Mango\n");
printf("P: Pear\n");
printf("G: Grapes\n");
printf("\n***ShopKeeperPanel***");
printf("\nHow many fruit items do you want to add to the shop?: ");
scanf_s("%d", &n);
int chosenFruitItem = 0;
while (chosenFruitItem < n) {
printf("\n(%d) Enter the item prefix: ", chosenFruitItem + 1);
char itemPrefix = ' ';
scanf_s(" %c", &itemPrefix, 1);
if (isItemExist(itemPrefixes, itemPrefix) == 1) {
printf("Error Item already exist");
continue;
}
else {
itemPrefixes[chosenFruitItem] = itemPrefix;
printf("Enter price for item (%c): ", itemPrefix);
scanf_s("%d", &itemPrices[chosenFruitItem]);
chosenFruitItem++;
}
}
displayMenu(itemPrefixes, itemPrices, n);
printf("\n**CUSTOMER PANEL***");
printf("\nWhat is your budget for today?: ");
scanf_s("%d", &budget);
printf("\nPlease enter Item Prefix from the menu to purchase: ");
scanf_s(" %c", &itemPurchased, 1);
if (withinBudget(budget, itemPurchased, itemPrefixes, itemPrices) == 1) {
printf("PURCHASS SUCCESS");
}
else
{
printf("INSUFFICENT BUDGET");
}
}
int isItemExist(char itemPrefixes[], char itemPrefix) {
for (int i = 0; i < n; i++) {
if (itemPrefixes[i] == itemPrefix) {
return 1;
}
}
return 0;
}
void displayMenu(char itemPrefixes[], int itemPrices[], int n) {
printf("\n*** ShopMenu ***");
printf("\nItem: \t Price: ");
for (int i = 0; i < n; i++) {
printf("\n%c:\t %d", itemPrefixes[i], itemPrices[i]);
}}
int withinBudget(int budget, char itemPurchased, char itemPrefixes[], int itemPrices[]) {
for (int i = 0; i < n; i++) {
if (itemPurchased == itemPrefixes[i] && itemPrices[i] < budget)
{
return 1;
}
return 0;
}
}
the output:
Please ident your code. Either simplest mistakes could go unnoticed because of this.
Here you have an auto one.
After doing this to your code, here's the last function:
int withinBudget(int budget, char itemPurchased, char itemPrefixes[], int itemPrices[]) {
for (int i = 0; i < n; i++) {
if (itemPurchased == itemPrefixes[i] && itemPrices[i] < budget) {
return 1;
}
return 0;
}
}
As you can see, return 0 is inside loop, it's working only if itemPurchased == itemPrefixes[0] && itemPrices[0] < budget as i will never be incremented.
So your function should be:
int withinBudget(int budget, char itemPurchased, char itemPrefixes[], int itemPrices[]) {
for (int i = 0; i < n; i++) {
if (itemPurchased == itemPrefixes[i] && itemPrices[i] < budget) {
return 1;
}
}
return 0;
}
How to delete an element from the array of type structure? Let's say if I register an item and then want to delete it how can I do that? The delete function is at the end of the code. I want to delete the item by giving the varunummer (id number). Any one know how to do it?
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define WORDLENGTH 30
#define MAX 5
struct varor {
int varunummer;
char namn[WORDLENGTH];
int lagersaldo;
};
int readLine(char s[], int length);
int ifVarunummerExist(int varunummer, const struct varor reg[], int nrOfGoods);
void registerVaror(struct varor reg[], int *nrOfGoods);
void getPrint(const struct varor reg[], int nrOfGoods);
void avregristreraVaror(struct varor reg[], int nrOfGoods);
int main(void) {
struct varor vRegister[WORDLENGTH];
int nrOfGoods = 0;
int run = 1;
while (run) {
char choice;
printf("\n\t\tMeny - Lager Program\n\n\
(1) Register\n\b\b\b\b\
(2) Print\n\
(3) Delete\n\
(4) Quit\n");
scanf(" %c%*c", &choice);
if (choice=='1')
registerVaror(vRegister, &nrOfGoods);
if (choice=='2')
getPrint(vRegister, nrOfGoods);
if (choice=='3')
avregristreraVaror(vRegister, nrOfGoods);
else if (choice=='4')
run = 0;
}
return 0;
}
int ifVarunummerExist(int varunummer, const struct varor reg[], int nrOfGoods) {
int i;
for (i = 0; i < nrOfGoods; i++)
if(reg[i].varunummer == varunummer)
return i;
return -1;
}
int readLine(char s[], int length) {
int ch, i=0;
while (isspace(ch=getchar()));
while (ch != '\n' && ch != EOF) {
if (i < length)
s[i++] = ch;
ch = getchar();
}
s[i] = '\0';
return i;
}
void registerVaror(struct varor reg[], int *nrOfGoods) {
char namn[WORDLENGTH], tmp[WORDLENGTH];
int varunummer, lagersaldo;
if (*nrOfGoods == MAX) {
printf("\nError! Finns inte plats kvar!\n");
return;
}
printf("Ange varunummer: ");
scanf("%d", &varunummer);
if (ifVarunummerExist(varunummer, reg, *nrOfGoods) >= 0) {
printf("\nVarunummer finns redan!\n");
return;
}
reg[*nrOfGoods].varunummer = varunummer;
printf("Ange namn: ");
readLine(reg[*nrOfGoods].namn, WORDLENGTH);
printf("Ange lagersaldo :");
scanf("%d", ®[*nrOfGoods].lagersaldo);
//reg[*nrOfGoods]=createVara(varunummer,namn,lagersaldo);
(*nrOfGoods)++;
}
void getPrint(const struct varor reg[], int nrOfGoods) {
int i;
printf("\nVarunummer \t Namn \t\t\t Lagersaldo\n");
for (i = 0; i < nrOfGoods; i++) {
printf(" %d \t\t %s \t\t\t %d\n",reg[i].varunummer,reg[i].namn,reg[i].lagersaldo);
}
}
void avregristreraVaror(struct varor reg[], int nrOfGoods) {
int run = 1;
while (run) {
char choice;
printf("\n (1) Delete \n (2) Exit");
scanf(" %c", &choice);
//DELETE IF CHOICE 1---------
if (choice == '1') {
int i, varunummer;
printf("Ange varunummer: ");
scanf("%d", &varunummer);
for (i = varunummer + 1; i < MAX; i++) {
reg[i - 1] = reg[i];
}
reg[i] = 0;
}
}
//QUIT TO MY MENU CHOICE 2--------
if (choice == '2')
run = 0;
}
You can try iterating through the array in a for loop UNTIL your varunummer is matched with the struct's property. Something along these lines (let's say you are searching for the member with varunummer = varunummerToLookFor), this shift all the elements in the array from the point onwards of your deletion by 1, hence, producing an array with the same sequence as before but with your wanted element removed. Hope that helps!
for(int i = 0, i < varorArraySize, i++){
if(varunummerToLookFor == varorArray[i].varunummer){
for (i = pos; i < varorArraySize - 1; i++)
{
varorArray[i] = varorArray[i + 1];
}
}
}
first of all, I understand mostly everything in this program that I copied from this book.
second, i just wanted to see if it worked ,
the only problem is that it says 'expected declaration specifiers before 'main''
and i don't know what it means
ps this is a really long program (300+ lines)
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
void printGreeting();
int getBet();
char getSuit(int suit);
char getRank(int rank);
void getFirstHand(int cardRank[], int cardSuit[]);
void getFinalHand
(int cardRank[], int cardSuit[], int finalRank[], int finalSuit[], int
ranksinHand[], int suitsinHand[])
int analyzeHand(int ranksinHand[], int suitsinHand[]);
main()
{
int bet;
int bank = 100;
int i;
int cardRank [5];
int cardSuit [5];
int finalRank[5];
int finalSuit[5];
int ranksinhand[13];
int suitsinhand[4];
int winnings;
time_t t;
char suit, rank, stillPlay;
printGreeting();
do{
bet = getBet();
srand(time(&t));
getFirstHand(cardRank, cardSuit);
printf("Your five cards: \n\n");
for (i = 0; i < 5; i++)
{
suit = getSuit(cardsSuit[i]);
rank = getRank(cardRank[i]);
printf("Card #%d: %c%c\n\n", i+1, rank, suit);
}
for (i=0; i < 4; i++)
{
suitsinHand[i] = 0;
}
for (i=0; i < 13; i++)
{
ranksinHand[i] = 0;
}
getFinalHand(cardRank, cardSuit, finalRank, finalSuit, ranksinHand,
suitsinHand);
printf("Your five final cards:\n\n");
for (i = 0; i < 5; i++)
{
suit = getSuit(finalSuit[i]);
rank = getRank(finalRank[i]);
printf("Card #%d: %c%c\n\n", i+1, rank, suit);
}
winnings = analyzeHand(ranksinHand, suitsinHand);
printf("You won %d!\n\n", bet*winnings);
bank = bank - bet + (bet*winnings)
printf("\n\nYour bank is now %d.\n\n", bank);
printf("Want to play again? ");
scanf(" %c", &stillPlay);
}while (toupper(stillPlay) == 'Y');
return;
}
/*************************************************************************/
void printGreeting();
{
printf("**********************************************************\n\n");
printf("\n\n\tWelcome to the Absolute Beginner's Casino\n\n");
printf("\tHome of the Video Draw Poker");
printf("**********************************************************\n\n");
printf("Here are the rules\n");
printf("You start with 100 credits, and you make a bet from");
printf("1 to 5 credits.\n");
printf("You are dealt 5 cards, and then you choose which ");
printf("cards to keep");
printf("or discard\n");
printf("You want to make the best possible hand.\n");
printf("\nHere is the table for winnings (assuming a ");
printf("bet of 1 credit):");
printf("\nPair \t\t\t\t1 credit");
printf("\nTwo pairs\t\t\t2 credits");
printf("\nThree of a kind\t\t\t3 credits");
printf("\nStraight \t\t\t4 credits");
printf("Flush\t\t\t\t5 credits");
printf("Full House\t\t\t8 credits");
printf("Four of a Kind\t\t\t10 credits");
printf("Straight Flush\t\t\t20 credits");
printf("\n\nHave fun!!\n\n");
}
void getFirstHand(int cardRank[], int cardSuit[]);
{
int i,j;
int carDup;
for(i=0; i < 5; i++)
{
carDup = 0;
do{
cardRank[i] = (rand() % 13);
cardSuit[i] = (rand() % 4);
for (j=0; j < i; j++)
{
if ((cardRank[i] == cardRank[j] &&
cardSuit[i] == cardSuit[j]))
{
carDup = 1;
}
}
}while (carDup == 1;);
}
}
char getSuit(int suit)
{
switch
{
case 0:
return('C');
case 1:
return('D');
case 2:
return('H');
case 3:
return('S');
}
}
char getRank(int rank)
{
switch (rank)
{
case 0:
return('A');
case 1:
return('2');
case 2:
return('3');
case 3:
return('4');
case 4:
return('5');
case 5:
return('6');
case 6:
return('7');
case 7;
return('8');
case 8:
return('9');
case 9:
return('T');
case 10:
return('J');
case 11:
return('Q');
case 12:
return('K');
}
}
int getBet()
{
int bet;
do
{
printf("How much do you want to bet?(Enter a number");
printf("from 1 to 5, or 0 to quit the game): ");
scanf(" %d", &bet);
if (bet >= 1 && bet <= 5)
{
return(bet);
}
else if (bet == 0)
{
exit(1);
}
else
{
printf("\n\nPlease enter a bet from 1-5 or ");
printf("0 to quit the game\n\n");
}
}while ((bet < 0) || (bet > 5));
}
int analyzeHand(int ranksinHand[], int suitsinHand[])
{
int num_consec = 0;
int i, rank, suit;
int straight = FALSE;
int flush = FALSE;
int four = FALSE;
int three = FALSE;
int pairs = 0;
for (suit = 0; suit < 4; suit++)
if (suitsinHand[suit] == 5)
flush = TRUE;
rank = 0;
while (ranksinHand[rank] == 0)
rank++;
for (; rank < 13 && ranksinHand[rank]; rank++)
num_consec++;
if(num_consec == 5) {
straight = TRUE;
}
for (rank = 0; rank < 13; rank++){
if (ranksinHand[rank] == 4)
four == TRUE;
if (ranksinHand[rank] == 3)
three == TRUE;
if (ranksinHand[rank] == 2)
pairs++;
}
if (straight && flush){
printf("Straight Flush\n\n");
return(20);
}
else if (four){
printf("Four of a kind\n\n");
return (10);
}
else if (three && pairs == 1){
printf("Full House\n\n");
return (8);
}
else if (flush){
printf("Flush\n\n");
return (5);
}
else if (straight){
printf("Straight\n\n");
return (4);
}
else if (three){
printf("Three of a Kind\n\n");
return (3);
}
else if (pairs == 2){
printf("Two Pairs\n\n");
return (2);
}
else if (pairs == 1){
printf("Pair\n\n");
return (1);
}
else{
printf("High Card\n\n");
return (0);
}
}
void getFinalHand
(int cardRank[], int cardSuit[], int finalRank[], int finalSuit[], int
ranksinHand[], int suitsinHand[])
{
int i, j, carDup;
char suit, rank, ans;
for (i=0; i < 5; i++)
{
suit = getSuit(cardSuit[i]);
rank = getRank(cardRank[i]);
printf("Do you want to keep card #%d: %c%c", i+1, rank, suit);
printf("\nPlease answer (Y/N):");
scanf(" %c", &ans);
if (toupper(ans) == 'Y')
{
finalRank[i] = cardRank[i];
finalSuit[i] = cardSuit[i];
ranksinHand[finalRank[i]]++;
suitsinHand[finalSuit[i]]++;
continue;
}
else if (toupper(ans) == 'N')
{
carDup = 0;
do{
carDup = 0;
finalRank[i] = (rand() % 13);
finalSuit[i] = (rand() % 4);
for (j=0; j < 5; j++)
{
if((finalRank[i] == finalRank[j]) && (finalSuit[i] ==
finalSuit[j]))
{
carDup = 1;
}
}
for (j=0; j < i; j++)
{
if((finalRank[i] == finalRank[j]) && (finalSuit[i] ==
finalSuit[j]))
{
carDup = 1;
}
}
}while (carDup == 1);
ranksinHand[finalRank[i]]++;
suitsinHand[finalSuit[i]]++;
}
}
}
By convention main() must return an integer. You have to declare it like that:
int main()
{
// Your method
return 0;
}
int mainor
void main
would do the work. Here, int main is shown to be the C standard. Refer to
Return type of main() too.
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;
}
Everything seems to working fine except for my SPOILED votes int is staying at 0 and not being updates by the function. I have tried adding a pointer thinking it would point to the object outside of the function which it does but the ++ function is not updating
#include <stdio.h>
struct candidates
{
char name[20];
int votes;
};
struct candidates electionCandidates[7];
void Initialize(struct candidates EC[]);
void Processvotes(struct candidates EC[], int *BadVote);
void printResults(struct candidates EC[], int *BadVote);
int main()
{
int i, SPOIL = 0;
Initialize(electionCandidates);
Processvotes(electionCandidates, &SPOIL);
printResults(electionCandidates, &SPOIL);
for(i = 0; i < 7; i++)
{
printf("%s",electionCandidates[i].name);
printf("%d\n\n",electionCandidates[i].votes);
}
printf("%d", SPOIL);
}
void Initialize(struct candidates EC[])
{
int i;
FILE *fp;
fp = fopen("elections.txt", "r");
for (i = 0; i < 7; i++)
{
fgets(EC[i].name, 20, (FILE*)fp);
}
fclose(fp);
}
void Processvotes(struct candidates EC[], int *BadVote)
{
int TVOTE, i;
FILE *fp;
fp = fopen("elections.txt", "r");
for (i = 0; i < 7; i++)
{
fgets(EC[i].name, 20, (FILE*)fp);
}
for (i = 0; i < 365; i++)
{
fscanf(fp, "%d", &TVOTE);
if (TVOTE == 1)
EC[0].votes++;
if (TVOTE == 2)
EC[1].votes++;
if (TVOTE == 3)
EC[2].votes++;
if (TVOTE == 4)
EC[3].votes++;
if (TVOTE == 5)
EC[4].votes++;
if (TVOTE == 6)
EC[5].votes++;
if (TVOTE == 7)
EC[6].votes++;
if (TVOTE < 1 || TVOTE > 7)
*BadVote++;
}
fclose(fp);
}
void printResults(struct candidates EC[], int *BadVote)
{
int i, Win = 0, WinSCORE = 0, Runner = 0, RunnerSCORE = 0;
for (i = 0; i < 7; i++)
{
if (EC[i].votes > WinSCORE)
{
WinSCORE = EC[i].votes;
Win = i;
}
if (EC[i].votes == WinSCORE)
{
RunnerSCORE = EC[i].votes;
Runner = i;
}
}
if (WinSCORE == RunnerSCORE)
{
printf("There was a tie between %s and %s who both got a total of %d votes each. There were %d spoiled votes\n", EC[Win].name, EC[Runner].name, WinSCORE, *BadVote);
}
else
printf("%s won the election with a total of %d votes. There was a total of %d spoiled votes.\n", EC[Win].name, WinSCORE, *BadVote);
}
Any help will be appreciated.
The postfix ++ operator takes precedence over the *; when you write *BadVote++, you're actually incrementing the pointer, not the value being pointed to. You should instead write (*BadVote)++. See this question for more.
I'm pretty sure it's a problem with the precedence of the operators. Try this:
if (TVOTE < 1 || TVOTE > 7)
(*BadVote)++;