I have created a simple Tic Tac Toe program in C. Everything is working OK (still needs a little clean-up) except for my check_for_win function. I'm not sure if I am declaring it in the correct spot, i.e. the main function. I switched it over to the player_input function because logically that would make more sense to me, i.e. checking for a win after each player moves, but still to no avail.
This is my syntax:
#include <stdio.h>
#include <stdlib.h>
void menu()
{
int choice;
do
{
printf("Main Menu\n\n");
printf(" 1. Play Game\n");
printf(" 2. Quit Game\n");
scanf("%d",&choice);
switch(choice)
{
case 1: board();
break;
case 2: printf("Quitting program!\n");
exit(0);
break;
default: printf("Invalid choice!\n");
break;
}
} while (choice != 1);
}
int board() {
printf("\n\n");
printf(" 1 | 2 | 3 \n");
printf("--------------\n");
printf(" 4 | 5 | 6 \n");
printf("--------------\n");
printf(" 7 | 8 | 9 \n");
}
/*int display() {
printf("// 1 | 2 | 3\n");
printf("// ---------\n");
printf("// 4 | 5 | 6\n");
printf("// ---------\n");
printf("// 7 | 8 | 9\n\n\n");
}
int gameboard(int board[9]) {
printf("// %d | %d | %d \n", board[0], board[1], board[2]);
printf("// ---------\n");
printf("// %d | %d | %d \n", board[3], board[4], board[5]);
printf("// ---------\n");
printf("// %d | %d | %d \n\n\n", board[6], board[7], board[8]);
}*/
void player_input(char gameboard[])
{
int i;
int playerX, playerO;
for (i = 0; i <= 9; i++)
{
printf("\nPlayer 1, please select a square by entering a number between [1 - 9]:\n\n");
scanf("%d", &playerX);
///playerX = playerX - 1;
display_board(gameboard, playerX, playerO);
printf("\nPlayer 2, please select a square by entering a number between [1 - 9]:\n");
scanf("%d", &playerO);
///playerO = playerO - 1;
display_board(gameboard, playerX, playerO);
}
return(playerX);
return(playerO);
}
void display_board(char gameboard[], int playerX, int playerO) {
///INPUT FOR PLAYER O
if(playerO == 1)
{
gameboard[0] = 'O';
}
else if(playerO == 2)
{
gameboard[1] = 'O';
}
else if(playerO == 3)
{
gameboard[2] = 'O';
}
else if(playerO == 4)
{
gameboard[3] = 'O';
}
else if(playerO == 5)
{
gameboard[4] = 'O';
}
else if(playerO == 6)
{
gameboard[5] = 'O';
}
else if(playerO == 7)
{
gameboard[6] = 'O';
}
else if(playerO == 8)
{
gameboard[7] = 'O';
}
else if(playerO == 9)
{
gameboard[8] = 'O';
}
///INPUT FOR PLAYER X
if(playerX == 1)
{
gameboard[0] = 'X';
}
else if(playerX == 2)
{
gameboard[1] = 'X';
}
else if(playerX == 3)
{
gameboard[2] = 'X';
}
else if(playerX == 4)
{
gameboard[3] = 'X';
}
else if(playerX == 5)
{
gameboard[4] = 'X';
}
else if(playerX == 6)
{
gameboard[5] = 'X';
}
else if(playerX == 7)
{
gameboard[6] = 'X';
}
else if(playerX == 8)
{
gameboard[7] = 'X';
}
else if(playerX == 9)
{
gameboard[8] = 'X';
}
printf("\n\n");
printf(" %c | %c | %c \n", gameboard[0], gameboard[1], gameboard[2]);
printf("--------------\n");
printf(" %c | %c | %c \n", gameboard[3], gameboard[4], gameboard[5]);
printf("--------------\n");
printf(" %c | %c | %c \n", gameboard[6], gameboard[7], gameboard[8]);
}
void check_for_win(char gameboard[])
{
if (gameboard[0] == gameboard[1] == gameboard[2] == 'X')
{
printf("Player X has WON!");
}
else if (gameboard[3]==gameboard[4]==gameboard[5]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[6]==gameboard[7]==gameboard[8]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[0]==gameboard[3]==gameboard[6]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[1]==gameboard[4]==gameboard[7]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[2]==gameboard[5]==gameboard[8]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[0]==gameboard[4]==gameboard[8]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[2]==gameboard[4]==gameboard[6]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[0]==gameboard[1]==gameboard[2]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[3]==gameboard[4]==gameboard[5]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[6]==gameboard[7]==gameboard[8]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[0]==gameboard[3]==gameboard[6]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[1]==gameboard[4]==gameboard[7]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[2]==gameboard[5]==gameboard[8]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[0]==gameboard[4]==gameboard[8]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[2]==gameboard[4]==gameboard[6]=='O')
{
printf("Player O has WON!");
}
else
{
printf("THE GAME HAS COME TO A DRAW.");
}
}
int welcome() {
printf("ELEC 1520 Programming Assignment 1\n");
printf("Programmer: Anonymous\n\n");
printf("Press the enter key to start playing....\n");
char enter = 0;
while (enter != '\r' && enter != '\n') {
enter = getchar();
}
}
int main () {
char gameboard[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
welcome();
menu();
///player_input();
///process_input(board);
int playerX=0;
int playerO=0;
display_board(gameboard, playerX, playerO);
printf("\n\nRULES\n");
printf("Player 1 is X and goes first.\n");
printf("Player 2 is O.\n");
printf("Please select a square by choosing a number between [1 - 9]:\n\n");
player_input(gameboard);
check_for_win(gameboard);
return 0;
}
if (gameboard[0] == gameboard[1] == gameboard[2] == 'X')
doesn't do what you are hoping to. Due to operator precedence, it is evaluated as
if ( ((gameboard[0] == gameboard[1]) == gameboard[2]) == 'X')
Regardless of what the values of gameboard[0], gameboard[1], and gameboard[2] are, the expression ((gameboard[0] == gameboard[1]) == gameboard[2]) == 'X' will always evaluate to false since ((gameboard[0] == gameboard[1]) == gameboard[2]) will be either true (1) or false (0).
In other words, the above if is equivalent to:
if (0)
To fix your logic, use
if (gameboard[0] == 'X' &&
gameboard[1] == 'X' &&
gameboard[2] == 'X')
Update the other if/else if statements similarly.
instead of double equal to(==) use double ampersand(&&) in (gameboard[0]==gameboard[1]==gameboard[2]=='O')
Related
I was working on learning c code and was making a tic-tac-toe game. The Boolean issue was fixed. Now the issue is that it is looping the printf("There is no empty space!"); and prinf("Invalid !!!"); after it take the player1 name. I also wanted to know if the line where I printed the array with the grid is correct or not.
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
char space[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
int row;
int column;
char token = 'x';
bool tie = false;
char n1[256];
char n2[256];
void functionboard()
{
char space[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
printf(" | | \n");
printf(" ", space[0][0], "| ", space[0][1], "| ", space[0][2], " \n");
printf("______|________|_____\n");
printf(" | | \n");
printf(" ", space[1][0], " | ", space[1][1], " | ", space[1][2], " \n");
printf("______|________|_____\n");
printf(" | | \n");
printf(" ", space[2][0], " | ", space[2][1], " | ", space[2][2], " \n");
printf(" | | \n");
}
void functionOne()
{
int dight;
if (token == 'x')
{
printf(n1, "please enter");
scanf("&d", &dight);
}
if (token == '0')
{
printf(n2, "please enter");
scanf("&d", &dight);
}
if (dight == 1)
{
row = 0;
column = 0;
}
if (dight == 2)
{
row = 0;
column = 1;
}
if (dight == 3)
{
row = 0;
column = 2;
}
if (dight == 4)
{
row = 1;
column = 0;
}
if (dight == 5)
{
row = 1;
column = 1;
}
if (dight == 6)
{
row = 1;
column = 2;
}
if (dight == 7)
{
row = 2;
column = 0;
}
if (dight == 8)
{
row = 2;
column = 1;
}
if (dight == 9)
{
row = 2;
column = 2;
}
else if (dight < 1 || dight > 9)
{
prinf("Invalid !!!");
}
if (token == 'x' && space[row][column] != 'x' && space[row][column] != '0')
{
space[row][column] = 'x';
token = '0';
}
else if (token == '0' && space[row][column] != 'x' && space[row][column] != '0')
{
space[row][column] = '0';
token = 'x';
}
else
{
printf("There is no empty space!");
functionboard();
}
functionOne();
}
bool functionDraw()
{
for (int i = 0; i < 3; i++)
{
if (space[i][0] == space[i][1] && space[i][0] == space[i][2] || space[0][i] == space[1][i] && space[0][i] == space[2][i])
return true;
}
if (space[0][0] == space[1][1] && space[1][1] == space[2][2] || space[0][2] == space[1][1] && space[1][1] == space[2][0])
{
return true;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (space[i][j] != 'x' && space[i][j] != '0')
{
return false;
}
}
}
tie = true;
return false;
}
int main()
{
printf("Enter the name of the first player : \n");
scanf("%c", n1);
printf("Enter the name of the second player : \n");
scanf("%c", n2);
printf("%c is player1 so he/she will play first \n", n1);
printf("%c is player2 so he/she will play first \n", n2);
while (!functionDraw())
{
functionboard();
functionOne();
functionDraw();
}
if (token == 'x' && tie == false)
{
printf("%c Wins!!\n", n2);
}
else if (token == '0' && tie == false)
{
printf("%c Wins!!\n", n1);
}
else
{
printf("its a draw!!");
}
}
If the error you're getting is about the type bool and not the variable itself, see this question: you need to also include <stdbool.h> to be able to use the bool type.
There's no built-in bool type in classic C. You can use it though by using #include <stdbool.h>. The other solution is replacing it with an int since it can be used like a bool
I have this problem and I don´t know what´s wrong. The code doesn´t have any errors or warnings but it always take the first if statement of the checkVictory function as correct.
I've tried comparing my code with another one that actually works and it looks identical to me. I´m pretty sure the mistake is in the checkVictory function because I used a variable called flag to check every if statement of the function and my returnValue (which is used as an indicator of my game status) always get triggered by the first if.
Here´s my code... (sorry for the indentation this is my first time using the website)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void board();
void markBoard(char mark);
int checkVictory();
int player, choice, flag;
char list[10] = {'-', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
void main() {
int gameStatus;
char mark;
player = 1;
do{
board();
// change turns
player = (player % 2) ? 1 : 2;
//input
printf("Player %d, enter a number: ", player);
scanf("%i", &choice);
// set the correct character based on player turn
mark = (player == 1) ? 'X' : 'O';
markBoard(mark);
gameStatus = checkVictory();
player++;
} while (gameStatus == -1);
if (gameStatus == 1) {
printf("Player %d WIN", --player);
printf("\nflag: %i", flag);
}
else{
printf("Draw");
}
}
void board() {
system("cls");
printf("\n\n\t tic tac toe\n\n");
printf("Player 1 (X) - Player 2 (O)\n\n");
printf(" | | \n");
printf(" %c | %c | %c \n", list[1], list[2], list[3]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", list[4], list[5], list[6]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", list[7], list[8], list[9]);
printf(" | | \n");
printf("\n");
}
void markBoard(char mark) {
if (choice == 1 && list[1] == '1')
list[1] = mark;
else if (choice == 2 && list[2] == '2')
list[2] = mark;
else if (choice == 3 && list[3] == '3')
list[3] = mark;
else if (choice == 4 && list[4] == '4')
list[4] = mark;
else if (choice == 5 && list[5] == '5')
list[5] = mark;
else if (choice == 6 && list[6] == '6')
list[6] = mark;
else if (choice == 7 && list[7] == '7')
list[7] = mark;
else if (choice == 8 && list[8] == '8')
list[8] = mark;
else if (choice == 9 && list[9] == '9')
list[9] = mark;
else {
printf("Invalid move\n");
player--;
getch();
}
}
/*********************************************FUNCTION TO RETURN GAME STATUS1 FOR GAME IS OVER WITH RESULT-1 FOR GAME IS IN PROGRESSO GAME IS OVER AND NO RESULT**********************************************/
int checkVictory() {
int returnValue = 0;
if (list[1] && list[2] == list[2] && list[3]) {
returnValue = 1;
flag = 1;
}
else if (list[4] && list[5] == list[5] && list[6]) {
returnValue = 1;
flag = 2;
}
else if (list[7] && list[8] == list[8] && list[9]) {
returnValue = 1;
flag = 3;
}
else if (list[1] && list[4] == list[4] && list[7]) {
returnValue = 1;
flag = 4;
}
else if (list[2] && list[5] == list[5] && list[8]) {
returnValue = 1;
flag = 5;
}
else if (list[3] && list[6] == list[6] && list[9]) {
returnValue = 1;
flag = 6;
}
else if (list[1] && list[5] == list[5] && list[9]) {
returnValue = 1;
flag = 7;
}
else if (list[3] && list[5] == list[5] && list[7]) {
returnValue = 1;
flag = 8;
}
else if (list[1] != '1' && list[2] != '2' && list[3] != '3' &&
list[4] != '4' && list[5] != '5' && list[6] != '6' &&
list[7] != '7' && list[8] != '8' && list[9] != '9') {
returnValue = 0;
}
else
returnValue = -1;
return returnValue;
}
The problem lies with the code below.
if (list[1] && list[2] == list[2] && list[3]) {
returnValue = 1;
Your condition:
list[1] && list[2] == list[2] && list[3]
Parses as:
list[1] && (list[2] == list[2]) && list[3]
Because no element of list is 0, they all test as true. Likewise, list[2] will always be equal to itself. Consequently this condition is always true. checkVictory will always return 1. When this is called, and gameSttatus is set to 1, the do-while loop ends.
Issue: I'm trying to utilize recTest to check and return the winner of a simple Tic-Tac-Toe game.
I'm just unsure of what I need to pass in the call (line I've highlighted), and how to integrate my public square array.
I apologize if this is overly simple, I'm just having a rough time connecting the dots.
#include<stdio.h>
#include<conio.h>
char square[10] = {'o', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
// returns -1 if no v[i] satisfies p
// returns i if v[i] satisfies p (picks largest i)
// (i>=0) and (i<n)
const int numwinpos = 8;
const int winpos[8][3] = {{1,2,3},
{4,5,6},
{7,8,9},
{1,4,7},
{2,5,8},
{3,6,9},
{1,5,9},
{3,5,7}};
//Function below, and variables above<--------------------------------------
int recTest(const int v[], const int n){
if( (n>0) && (!p(v[n-1])) )
return recTest(v,n-1);
else
return n-1;
}
/*int winnerCheck() {
//not even hard-coded tho...
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1; //above 3 check across
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1; //above 3 check down
else if (square[1] == square[5] && square[5] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1; //above 2 check diagonal
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4]
!= '4' && square[5] != '5' &&
square[6] != '6' && square[7] != '7' && square[8]
!= '8' && square[9] != '9')
return 0;
else
//exit
return -1;
}
*/
void board() {
printf("\n\n\tTic Tac Toe\n\n");
printf("Player 1 (X) - Player 2 (O)\n\n\n");
//prints the board after every input
printf(" | | \n");
printf(" %c | %c | %c \n", square[1], square[2],square[3]);
printf("____|_____|____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[4], square[5],square[6]);
printf("____|_____|____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[7], square[8],square[9]);
printf(" | | \n");
}
int main() {
/*
char board[3][3] = {
};
printf("\t|\t|\t\n");
printf("\t|\t|\t\n");
printf("________|_______|________\n");
printf("\t|\t|\t\n");
printf("\t|\t|\t\n");
printf("________|_______|________\n");
printf("\t|\t|\t\n");
printf("\t|\t|\t\n");
printf("\t|\t|\t\n");
*/
int player = 1, i, choice;
char mark;
do {
board();
player = player % 2 ? 1 : 2;
printf("Player %d, enter a number: ", player);
scanf("%d", &choice);
//mark = (player == 1) ? 'X' : 'O';
if (player == 1) {
mark = 'X';
} else {
mark = 'O';
}
if (choice == 1)
square[1] = mark;
else if (choice == 2)
square[2] = mark;
else if (choice == 3)
square[3] = mark;
else if (choice == 4)
square[4] = mark;
else if (choice == 5)
square[5] = mark;
else if (choice == 6)
square[6] = mark;
else if (choice == 7)
square[7] = mark;
else if (choice == 8)
square[8] = mark;
else if (choice == 9)
square[9] = mark;
i = recTest(square, numwinpos); //HERE <--------------------------------------
player++;
} while (i == -1);
board();//call board
if (i == 1)
printf("----->\aPlayer %d WINS!<-----", --player);//nice alert sound when printed
else
printf("----->\aC-could it be...? Game draw!<-----");//nice alert sound when printed
//getch();//waits for user input before ending
return 0;
}
A quick and dirty way to use recursion would be to do something like below... and yes this can be optimized..
int winnerCheck(int x)
{
if(x < numwinpos)
{
if((square[winpos[x][0]] == 'x') && (square[winpos[x][1]] == 'x') && (square[winpos[x][2]] == 'x') ||
(square[winpos[x][0]] == 'o') && (square[winpos[x][1]] == 'o') && (square[winpos[x][2]] == 'o'))
return 1;
else
{
return winnerCheck(++x);
}
}
return 0;
}
I just started learning coding for about 2 months because of the course im taking.
My code works (kinda) but after the 1st loop it wont show the 1st line of the main function("You have been given 20 pokeballs, embrak on you quest on becoming a Pokeman Master!!!:), and i dont know why!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int pokeball = 20;
int rand_num;
int PokemonCaught[5] = { 0, 0, 0 ,0, 0 };
int poke_captured = 0;
int rungame = 1;
int stopgame = 1;
int total;
int randomnum();
int encounter_rate();
int pokemon_met();
void BallThrow();
void CaughtPokemon();
void checkball();
void PokeSummary();
char exitno();
int clear();
int main()
{
printf("You have been given 20 pokeballs, embrak on you quest on becoming a Pokeman Master!!!\n");
getchar();
do
{
checkball();
encounter_rate();
pokemon_met();
} while (stopgame == 0);
PokeSummary();
return 0;
}
int randomnum()
{
srand(time(NULL));
}
int encounter_rate()
{
randomnum();
rand_num = rand() % 100;
}
int pokemon_met()
{
if (rand_num <= 30)
{
printf("A wild Margikarp appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else if (rand_num <= 50)
{
printf("A wild Charmander appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else if (rand_num <= 70)
{
printf("A wild Jigglypuff appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else if (rand_num <= 85)
{
printf("A wild Pikachu appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else
{
printf("A wild Dragonite appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
}
void checkball()
{
if (pokeball > 0)
{
stopgame = 0;
}
else
{
stopgame = 1;
}
}
void BallThrow()
{
randomnum();
int BallChance;
int PokeRun;
BallChance = rand() % 2;
pokeball = pokeball - 1;
if (BallChance == 1)
{
printf("Gotcha!\n");
printf("Number of Pokeball left: %d\n", pokeball);
printf("Press ENTER to continue your journey!\n\n");
poke_captured = poke_captured + 1;
CaughtPokemon();
getchar();
if (pokeball == 0)
{
printf("Your pokeball has used up!\n");
printf("Press ENTER to check the summary of your journey\n");
getchar();
PokeSummary();
}
}
else if (BallChance == 0)
{
PokeRun = rand() % 2;
printf("The pokemon broke free!!!\n");
if (PokeRun == 0)
{
if (pokeball == 0)
{
printf("Your pokeball has used up!\n");
printf("Press ENTER to check the summary of your journey\n");
getchar();
PokeSummary();
}
else
{
printf("Number of Pokeball left: %d\n", pokeball);
printf("Press ENTER to throw pokeball!\n\n");
getchar();
BallThrow();
}
}
else if (PokeRun == 1)
{
printf("Oh no! The pokemon ran away!\n");
printf("Number of Pokeball left: %d\n", pokeball);
printf("Press ENTER to continue your journey!\n\n");
getchar();
if (pokeball == 0)
{
printf("Your pokeball has used up!\n");
printf("Press ENTER to check the summary of your journey\n");
getchar();
PokeSummary();
}
}
}
}
void CaughtPokemon()
{
if (rand_num <= 30)
{
PokemonCaught[0] = PokemonCaught[0] + 1;
}
else if (rand_num <= 50)
{
PokemonCaught[1] = PokemonCaught[1] + 1;
}
else if (rand_num <= 70)
{
PokemonCaught[2] = PokemonCaught[2] + 1;
}
else if (rand_num <= 85)
{
PokemonCaught[3] = PokemonCaught[3] + 1;
}
else if (rand_num <= 95)
{
PokemonCaught[4] = PokemonCaught[4] + 1;
}
}
void PokeSummary()
{
int point0, point1, point2, point3, point4, total;
point0 = (PokemonCaught[0]) * 10;
point1 = (PokemonCaught[1]) * 30;
point2 = (PokemonCaught[2]) * 30;
point3 = (PokemonCaught[3]) * 50;
point4 = (PokemonCaught[4]) * 70;
total = point0 + point1 + point2 + point3 + point4;
printf("You have successfully caught %d Pokemon!\n\n", poke_captured);
printf("You have caught:\n");
printf("Margikarp = %d (%dpoints)\n", PokemonCaught[0], point0);
printf("Charmander = %d (%dpoints)\n", PokemonCaught[1], point1);
printf("Jigglypuff = %d (%dpoints)\n", PokemonCaught[2], point2);
printf("Pikachu = %d (%dpoints)\n", PokemonCaught[3], point3);
printf("Dragonite = %d (%dpoints)\n", PokemonCaught[4], point4);
printf("\nTotal points = %d\n", total);
exitno();
}
char exitno()
{
char stay;
printf("Press 'y' to continue, press any other key to quit.");
scanf(" %c", &stay);
if (stay == 'y' || stay == 'Y')
{
clear();
return (main);
}
else
{
printf("Thank you for playing!!");
exit(0);
}
}
int clear()
{
total = total * 0;
poke_captured = poke_captured * 0;
PokemonCaught[0] = PokemonCaught[0] * 0;
PokemonCaught[1] = PokemonCaught[1] * 0;
PokemonCaught[2] = PokemonCaught[2] * 0;
PokemonCaught[3] = PokemonCaught[3] * 0;
PokemonCaught[4] = PokemonCaught[4] * 0;
pokeball = pokeball + 20;
}
I appreciate any help and i know my codes are far from being decent (sigh)
. Thanks
You have made an error when calling main in char exitno() function:
char exitno()
{
char stay;
printf("Press 'y' to continue, press any other key to quit.");
scanf(" %c", &stay);
if (stay == 'y' || stay == 'Y')
{
clear();
return ((char)main()); // main it should be called using parenthesis
}
else
{
printf("Thank you for playing!!");
exit(0);
}
}
That is not inside of your do-while loop.
If you want it to execute every time, then just move it down into the the loop. Otherwise, your program will move past in the first few milliseconds of operation. Alternatively, modify it (likely, put it within a new function) to print out some information that might change during execution of your program--say, the current number of pokeballs remaining.
I prompt the user if they want to order fish, once they are done ordering fish, they are asked to order chips, and then drinks. Once drinks is done, I need a total of all prices and then I need to list them.
Problem is, I don't know how to "tally" up all the totals. How would I go about writing a function to tally up subtotals?
The way my program gets the subTotal is:
subTotal(typeOfFood, foodChoice, foodSize, foodOrders);
In my subtotal function it just has subtotal+= $x.xx based on what type of food they chose.
I tried making a function like this:
float accumTotal(int total) {
float finalTotal;
finalTotal += total;
return finalTotal;
}
but that didn't work.
OUTPUT of my proglram:
Do you order FISH (Y/N)? y
Fish choice (K- Haddock, T- Halibut): t
What size (L-Large, M-Medium, S-Small): l
How many orders do you want? (>=0): 2
You ordered: [fish]: [Halibut], Size: [Large], ordered: [2], subtotal: [8.00]
Do you order FISH (Y/N)? y
Fish choice (K- Haddock, T- Halibut): t
What size (L-Large, M-Medium, S-Small): s
How many orders do you want? (>=0): 2
You ordered: [fish]: [Halibut], Size: [Small], ordered: [2], subtotal: [4.80]
Do you order FISH (Y/N)? n
Do you order CHIPS (Y/N)? y
Chips choice (C- Cut, R- Rings): r
What size (L-Large, M-Medium, S-Small): m
How many orders do you want? (>=0): 5
You ordered: [chips]: [Ring], Size: [Medium], ordered: [5], subtotal: [12.00]
Do you order CHIPS (Y/N)? n
Do you order DRINKS (Y/N)? n
============================
ACCUMULATIVE TOTAL = $x.xx
SOURCE: though not really necessary
#include <stdio.h>
#include <string.h>
int processing(char *typeOfFood, char foodChoice, char foodSize, int foodOrders);
float subTotal(char *typeOfFood, char foodChoice, char foodSize, int foodOrders);
float accumTotal(int total);
float subTotal(char *typeOfFood, char foodChoice, char foodSize, int foodOrders) {
float subTotal = 0;
if ((strcmp(typeOfFood, "fish")) == 0) {
if (foodChoice == 'k' || foodChoice == 'K') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 3.00;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 4.00;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 5.00;
}
}
else if (foodChoice == 't' || foodChoice == 'T') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 2.40;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 3.20;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 4.00;
}
}
}
if ((strcmp(typeOfFood, "chips")) == 0) {
if (foodChoice == 'c' || foodChoice == 'C') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 1.20;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 1.60;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 2.00;
}
}
else if (foodChoice == 'r' || foodChoice == 'R') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 1.80;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 2.40;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 3.00;
}
}
}
if ((strcmp(typeOfFood, "drink")) == 0) {
if (foodChoice == 's' || foodChoice == 'S') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 1.20;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 1.60;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 2.00;
}
}
else if (foodChoice == 'c' || foodChoice == 'C') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 1.05;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 1.40;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 1.75;
}
}
else if (foodChoice == 't' || foodChoice == 'T') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 0.90;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 1.20;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 1.50;
}
}
} // ifs
subTotal = subTotal * foodOrders;
return subTotal;
}
float accumTotal(int total) {
// float finalTotal;
// finalTotal += total;
// printf("[%lf]", finalTotal);
// return finalTotal;
}
int processing(char *typeOfFood, char foodChoice, char foodSize, int foodOrders){
char *foodSizeSelect, *foodChoiceSelect;
float total = subTotal(typeOfFood, foodChoice, foodSize, foodOrders);
if ((strcmp(typeOfFood, "fish")) == 0){
switch (foodChoice) {
case 'k':
foodChoiceSelect = "Haddock";
break;
case 'K':
foodChoiceSelect = "Haddock";
break;
case 't':
foodChoiceSelect = "Halibut";
break;
case 'T':
foodChoiceSelect = "Halibut";
break;
}
}
else if ((strcmp(typeOfFood, "chips")) == 0){
switch (foodChoice) {
case 'c':
foodChoiceSelect = "Cut";
break;
case 'C':
foodChoiceSelect = "Cut";
break;
case 'r':
foodChoiceSelect = "Ring";
break;
case 'R':
foodChoiceSelect = "Ring";
break;
}
}
else if ((strcmp(typeOfFood, "drinks")) == 0){
switch (foodChoice) {
case 's':
foodChoiceSelect = "Softdrink";
break;
case 'S':
foodChoiceSelect = "Softdrink";
break;
case 'c':
foodChoiceSelect = "Coffee";
break;
case 'C':
foodChoiceSelect = "Coffee";
break;
case 't':
foodChoiceSelect = "Tea";
break;
case 'T':
foodChoiceSelect = "Tea";
break;
}
}
switch (foodSize) {
case 's':
foodSizeSelect = "Small";
break;
case 'S':
foodSizeSelect = "Small";
break;
case 'm':
foodSizeSelect = "Medium";
break;
case 'M':
foodSizeSelect = "Medium";
break;
case 'l':
foodSizeSelect = "Large";
break;
case 'L':
foodSizeSelect = "Large";
break;
}
printf("You ordered: [%s]: [%s], Size: [%s], ordered: [%d], subtotal: [%.2lf]\n\n", typeOfFood, foodChoiceSelect, foodSizeSelect, foodOrders, total);
}
int main() {
char fishYesNo, chipsYesNo, drinksYesNo;
char fishChoice, fishSize; int fishOrders;
char chipsChoice, chipsSize; int chipsOrders;
char drinksChoice, drinksSize; int drinksOrders;
char *typeOfFood;
do {
typeOfFood = "fish";
printf("Do you order FISH (Y/N)? ");
scanf(" %c", &fishYesNo);
if (fishYesNo != 'n' || fishYesNo != 'n') {
printf("Fish choice (K- Haddock, T- Halibut): ");
scanf(" %c", &fishChoice);
printf("What size (L-Large, M-Medium, S-Small): ");
scanf(" %c", &fishSize);
printf("How many orders do you want? (>=0): ");
scanf("%d", &fishOrders);
processing(typeOfFood, fishChoice, fishSize, fishOrders);
}
else if (fishYesNo == 'n' || fishYesNo == 'N') {
typeOfFood = "chips";
}
} while ((strcmp(typeOfFood, "fish")) == 0);
do {
typeOfFood = "chips";
printf("Do you order CHIPS (Y/N)? ");
scanf(" %c", &chipsYesNo);
if (chipsYesNo != 'n') {
printf("Chips choice (C- Cut, R- Rings): ");
scanf(" %c", &chipsChoice);
printf("What size (L-Large, M-Medium, S-Small): ");
scanf(" %c", &chipsSize);
printf("How many orders do you want? (>=0): ");
scanf("%d", &chipsOrders);
processing(typeOfFood, chipsChoice, chipsSize, chipsOrders);
}
else if (chipsYesNo == 'n' || chipsYesNo == 'N') {
typeOfFood = "drinks";
}
} while ((strcmp(typeOfFood, "chips")) == 0);
do {
typeOfFood = "drinks";
printf("Do you order DRINKS (Y/N)? ");
scanf(" %c", &drinksYesNo);
if (drinksYesNo != 'n') {
printf("Drinks choice (S- Softdrink, C- Coffee, T- Tea): ");
scanf(" %c", &drinksChoice);
printf("What size (L-Large, M-Medium, S-Small): ");
scanf(" %c", &drinksSize);
printf("How many orders do you want? (>=0): ");
scanf("%d", &drinksOrders);
processing(typeOfFood, drinksChoice, drinksSize, drinksOrders);
}
else if (drinksYesNo == 'n' || drinksYesNo == 'N') {
}
} while ((strcmp(typeOfFood, "drinks")) == 0);
}
What you need is a tracking variable to keep count of the running total:
void processing(char *typeOfFood, char foodChoice, char foodSize, int foodOrders, float *subTotal)
{
...
printf("You ordered: [%s]: [%s], Size: [%s], ordered: [%d], subtotal: [%.2lf]\n\n", typeOfFood, foodChoiceSelect, foodSizeSelect, foodOrders, total);
*subTotal += total; // add to the total.
}
int main () {
...
char *typeOfFood;
float subTotal = 0.0f;
...
...
// for each call to 'processing'
processing(typeOfFood, fishChoice, fishSize, fishOrders, &subTotal);
...
...
// when the drinks are done:
else if (drinksYesNo == 'n' || drinksYesNo == 'N') {
printf("your bill totals to $%.2f", subTotal);
break; // note the added 'break' so that the program exits.
}
...
}