I have multiple errors with this project. The first and most important one would have to be one I answer a question in a case. Rather than it going back to choosing a case, it instead goes towards the difficulty. This won't let me exit the program and to get the average questions correct.
Another error is the end average questions correct. My program crashes whenever I try to just skip questions and go to the end. All it would do is divide 0/0 unless that's not possible. I'm supposed to use a switch function which is something I've seen has a problem with loops. Sadly, I can't find a way to fix it on my end, or every answer has just gone right over my head.
#include <stdio.h>
int main()
{
int option = 0;
int answer = 0;
int remainder = 0;
int ttlProblems = 0;
int correctAnswers = 0;
double averageCorrect = 0;
char difficulty;
printf("Math Practice Program Menu");
printf("\n\n1. Addition\n");
printf("2. Subtraction \n");
printf("3. Multiplication \n");
printf("4. Division \n");
printf("5. Exit\n\n");
printf("Select an option: ");
scanf_s("%d", &option);
while (option != 5)
{
switch (option)
{
case 1:
printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
scanf_s(" %c", &difficulty, 1);
if (difficulty == 'e')
{
printf("3 + 1 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 4)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'm')
{
printf("34 + 19 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 53)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'h')
{
printf("134 + 119 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 253)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else
{
printf("Invalid difficulty set\n");
}
break;
case 2:
printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
scanf_s(" %c", &difficulty, 1);
if (difficulty == 'e')
{
printf("3 - 1 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 2)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'm')
{
printf("34 - 19 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 15)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'h')
{
printf("134 - 119 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 15)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else
{
printf("Invalid difficulty s\n");
}
break;
case 3:
printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
scanf_s(" %c", &difficulty, 1);
if (difficulty == 'e')
{
printf("3 * 1 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 3)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'm')
{
printf("34 * 19 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 646)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'h')
{
printf("134 * 119 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 15946)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else
{
printf("Invalid difficulty set\n");
}
break;
case 4:
printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
scanf_s(" %c", &difficulty, 1);
if (difficulty == 'e')
{
printf("9 / 2 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
printf("Enter any remainder: ");
scanf_s("%d", &remainder, 1);
if (answer == 4 && remainder == 1)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'm')
{
printf("34 / 19 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
printf("Enter any remainder: ");
scanf_s("%d", &remainder, 1);
if (answer == 1 && remainder == 15)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'h')
{
printf("134 / 19 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
printf("Enter any remainder: ");
scanf_s("%d", &remainder, 1);
if (answer == 7 && remainder == 1)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else
{
printf("Invalid difficulty set\n");
break;
}
break;
case 5:
break;
default:
printf("Not a valid option.\n");
}
}
averageCorrect = ttlProblems / correctAnswers;
printf("You attempted %d problems and got %d correct\nFor a percentage of %.2f correct.", ttlProblems, correctAnswers, averageCorrect);
}
Currently, your code asks the user to enter the option type before the while loop, so it will only ask once and use that value for the option every loop iteration. Moving your option input scanf_s("%d", &option); into the beginning of the while loop should solve your first problem.
In regards to dividing by zero, this will cause the program to crash because it is not possble. You might want to check whether correctAnswers is zero or not before performing the division.
Related
I'm writing my first C program and there's not a single way I can figure out how I can reduce the size of my if statements, for some reason I think that its a little too long. I have tried reducing the lines of code, playing with it but when I change something some messages when I test it don't appear such as Purchase was successful or it failed. I know that this is a little of a beginner question. Could you help me out and if possible give any tips on what to use and research to make it more concise?
if (input == 'A') {
calculation = budget - apple;
if (calculation >= 0) {
printf("Purchase was a success!\n");
printf("Purchase details\n");
printf("----------------------\n");
printf("Item: %c\n", input);
printf("Price: \x9C%.2f\n", apple);
printf("Remaining budget: \x9C%.2f\n\n", calculation);
printf("Thanks for shopping with us!");
}
else {
printf("Purchase FAILED!\n");
printf("Low budget or invalid item!\n\n");
printf("Thanks for shopping with us!");
}
} else if (input == 'O') {
calculation = budget - orange;
if (calculation >= 0) {
printf("Purchase was a success!\n");
printf("Purchase details\n");
printf("----------------------\n");
printf("Item: %c\n", input);
printf("Price: \x9C%.2f\n", orange);
printf("Remaining budget: \x9C%.2f\n\n", calculation);
printf("Thanks for shopping with us!");
}
else {
printf("Purchase FAILED!\n");
printf("Low budget or invalid item!\n\n");
printf("Thanks for shopping with us!");
}
} else if (input == 'P') {
calculation = budget - pear;
if (calculation >= 0) {
printf("Purchase was a success!\n");
printf("Purchase details\n");
printf("----------------------\n");
printf("Item: %c\n", input);
printf("Price: \x9C%.2f\n", pear);
printf("Remaining budget: \x9C%.2f\n\n", calculation);
printf("Thanks for shopping with us!");
}
else {
printf("Purchase FAILED!\n");
printf("Low budget or invalid item!\n\n");
printf("Thanks for shopping with us!");
}
} else {
printf("Purchase FAILED!\n");
printf("Low budget or invalid item!\n\n");
printf("Thanks for shopping with us!");
};
Create a temporary variable (e.g. fruits), and use input to determine what it should be set to, using an otherwise impossible value for "invalid item", like:
if (input == 'A') {
fruits = apple;
} else if (input == 'O')
fruits = orange;
} else if (input == 'P') {
fruits = pear;
} else {
fruits = -1; // Invalid item
}
Once that's done the rest can become:
if(fruits != -1) {
calculation = budget - fruit;
if (calculation >= 0) {
printf("Purchase was a success!\n");
printf("Purchase details\n");
printf("----------------------\n");
printf("Item: %c\n", input);
printf("Price: \x9C%.2f\n", fruits);
printf("Remaining budget: \x9C%.2f\n\n", calculation);
printf("Thanks for shopping with us!");
} else {
fruits = -1; // Low budget or invalid item!
}
}
if(fruits == -1) {
printf("Purchase FAILED!\n");
printf("Low budget or invalid item!\n\n");
printf("Thanks for shopping with us!");
}
Expanding upon the concepts of the posted solution, one might consider that "insufficient funds" and "item does not exist" to require different messages. It is also possible to determine based on the chosen fruit, if the user has the funds needed, or if the user indeed has insufficient funds to purchase ANY fruit at all.
To allow for additional fruit items, we can use arrays:
#include <stdio.h>
#define MAXLINE 80
#define WALLET 10.00
typedef struct {
int id;
double cost;
} Product;
int main() {
char buffer[MAXLINE];
double balance = WALLET;
Product pricelist[] = {
{0, 0.00}, {1, 1.45}, {2, 1.16}, {3, 1.23}
};
char* fruit[] = {"", "Apple", "Orange", "Pear"};
size_t itemcount = (sizeof(pricelist)/sizeof(Product));
double low_price = pricelist[1].cost;
while (1) {
int choice = 0;
printf("Price List:\n");
for (size_t i = 1; i < itemcount; ++i) {
double c = pricelist[i].cost;
printf("%d: %s\t\%.2f\n", i, fruit[i], c);
low_price = c < low_price ? c : low_price;
}
printf("Your balance is: %.2f. Enter Selction: ", balance);
if (fgets(buffer, MAXLINE, stdin) != NULL) {
sscanf(buffer, "%d", &choice);
}
if (choice > 0 && choice < itemcount) {
Product p = pricelist[choice];
if (balance - p.cost >= 0.00) {
balance -= p.cost;
printf("\nPurchase was a success!\n");
printf("Purchase details\n");
printf("----------------------\n");
printf("Item: %s\n", fruit[choice]);
printf("Price: \x9C%.2f\n", p.cost);
printf("Remaining budget: \x9C%.2f\n\n", balance);
}
else {
printf("\nPurchase FAILED!\n");
printf("Insufficient funds, sorry.\n\n");
}
}
else {
printf("Choice not recognized, please try again\n\n", choice);
}
if (balance <= low_price ) {
printf("You can make no further purchases\n");
break;
}
}
printf("Thanks for shopping with us!\n");
}
Is it possible to create yes or no function to be called to exit a switch statement. if (y)es is hit it would exit. If (n)o is hit it would loop back to the switch's options. if so how would this be done. This is what I have so far.
I hope this helps to clarify what I am trying to do
int yes_no(void) {
scanf("%d", &option);
while (option == 'y' || option == 'Y' || option == 'n' || option == 'N') {
if (option == 'y' || option == 'Y') {
return 1;
} else
if (option == 'n' || option == 'N') {
return 0;
} else {
printf("Only (Y)es or (N)o are acceptable");
}
}
}
int main(void) {
int select;
do {
printf("0 exit");
printf("1 hello");
scanf("%d", &select);
switch (select) {
case 0:
printf("Exit the program? (Y)es or (N)o :");
yes_no(); // if (n)o is hit it will loop back to the menu
break;
case 1:
printf("hello how r u doing");
break;
default:
printf("not accepted number");
}
} while (select != 0);
}
Consider the following yes_no function.
int yes_no() {
char option;
while (1) {
printf("(y/n): ");
if (scanf(" %c", &option) != 1) {
printf("Error occurred while reading option.\n");
continue;
}
if (option == 'y' || option == 'Y') {
return 1;
} else if (option == 'n' || option == 'N') {
return 0;
} else {
printf("Only (y)es or (n)o are acceptable.\n");
}
}
}
You have a return value of 1 if given a yes input, and 0 in case of no. With this in mind, in the case 0 code block of the switch statement, the return value of this function can be captured, which can then be used to either break the loop or do nothing. For example:
int main(void) {
int select;
int continue_loop = 1;
printf("Press 0 to exit\n");
printf("Press 1 for hello\n\n");
while (continue_loop) {
printf("(0/1): ");
if (scanf(" %d", &select) != 1) {
printf("Error occurred while reading number.\n");
continue;
}
switch (select) {
case 0:
printf("Exit the program? (y)es or (n)o;\n");
int make_sure = yes_no(); // yes_no() returns 1 if 'yes', and 0 if 'no'
// If 'yes', break while loop by setting continue_loop to 0.
// Do nothing in case of 'no'
if (make_sure == 1) {
continue_loop = 0;
}
break;
case 1:
printf("Hello, how are you doing?\n");
break;
default:
printf("Number not accepted.\n");
}
}
return 0;
}
This should do the job, if I get your question correctly:
int yes_or_no()
{
do
{
char option = 0;
scanf(" %c", &option);
if (option == 'y' || option == 'Y')
return 1;
if (option == 'n' || option == 'N')
return 0;
printf("Only (Y)es or (N)o are acceptable");
}
while( 1 );
}
with switch you can perform your code better:
int yes_no(void) {
scanf("%d", &option);
while (1){
switch(option){
case 'y':
case 'Y':
return 1;
case 'n':
case 'N':
return 0;
default:
printf("invalid input.try again");
scanf("%d", &option);
}
}
}
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')
I want to return to the start of the loop and make the user enter another input.
This is what I have but I keep getting the error message repeating over and over again.
How would I return so the user can enter a argument
printf("Enter a option\n");
scanf("%d", &option);
while (option != 1 || option != 2 || option != 3 || option != 4)
{
if (option == 1)
{
option1(...);
break;
}
else if (option == 2)
{
option2(...);
break;
}
else if (option == 3)
{
option3(...);
break;
}
else if (option == 4)
{
option4(...);
break;
}
else
{
printf("\nPlease enter a correct option\n");
continue;
}
}
Just rearrange the logic, for example like:
do {
printf("Enter a option\n");
scanf("%d",&option);
if(option == 1){
option1(...);
break;
}
else if(option == 2){
option2(...);
break;
}
else if(option == 3){
option3(...);
break;
}
else if(option == 4){
option4(...);
break;
}
else{
printf("\nPlease enter a correct option\n");
continue;
}
}
while(true);
Now your code does the scanf only once and then iterates over the same result, instead you must read the value each time you begin the loop.
int option;
while(1)
{
printf("Enter a option\n");
scanf("%d",&option);
if(option == 1){
printf("option1\n");
break;
}
else if(option == 2){
printf("option2\n");
break;
}
else if(option == 3){
printf("option3\n");
break;
}
else if(option == 4){
printf("option4\n");
break;
}
else{
printf("\nPlease enter a correct option\n");
continue;
}
}
use a do..while with printf/scanf inside.
do {
printf("Enter a option\n");
scanf("%d",&option);
your if/else statements here...
} while(true);
you should Change your loop to do while.
do
{
printf("Enter a option\n");
scanf("%d",&option);
if(option == 1){
option1(...);
}
else if(option == 2){
option2(...);
}
else if(option == 3){
option3(...);
}
else if(option == 4){
option4(...);
}
else{
printf("\nPlease enter a correct option\n");
continue;
}
}while(option != 1 || option != 2 || option != 3 || option != 4);
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.
}
...
}