Returning to start of loop C - c

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);

Related

C Case in loop problems

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.

My increment doesn't work (c)

So, I have this code, at end I have an increment for exit when it reaches 4 (4 times wrong value entered), but it doesn't work:
int main() {
int nej;
printf("1-Triangulo \n");
printf("2-Division \n");
printf("3-Menu \n");
printf("4-Bosque \n");
printf("0-Salir \n");
scanf("%d", &nej);
if (nej == 1) {
// Ejercicio 1-Triangulo:
triangulo();
}
if (nej == 2) {
// Ejercicio 2-Division:
division();
}
if (nej == 3) {
menu();
}
if (nej == 4) {
arboles();
}
if (nej == 0) {
// Salida
exit(1);
}
int nerr = 0;
while (nej < 0 || nej > 4) {
nerr++;
// Contador de error
if (nerr == 4) {
exit(1);
}
}
}
How to fix this?
I believe you have a typo inside the while condition
while (nej < 0 || nej > 4) {
...
should be:
while (nerr < 0 || nerr > 4) {
..
nej being the input, and the input being invalid if less than 0 or larger than 4, the correct statement would be:
if (nej < 0 || nej > 4) {
(and not while as nej is not changed in the code block).
If you initialize the int nerr = 0; every time when you read,the flow will never reach the exit condition
int main()
{
int nej;
int nerr = 0; // Keep the counter outside the loop
while (true){//Start a infinite loop here
printf("1-Triangulo \n");
printf("2-Division \n");
printf("3-Menu \n");
printf("4-Bosque \n");
printf("0-Salir \n");
scanf("%d", &nej);
if (nej == 1) {
// Ejercicio 1-Triangulo:
triangulo();
}
if (nej == 2) {
// Ejercicio 2-Division:
division();
}
if (nej == 3) {
menu();
}
if (nej == 4) {
arboles();
}
if (nej == 0) {
// Salida
exit(1);
}
while (nej < 0 || nej > 4) {
nerr++;
// Contador de error
if (nerr == 4) {
exit(1);
}
}
}
It have following errors:
Code should be in the while loop
Variable nerr should be declared and initialize outside the while loop
Variable nerr should control the while loop
Note: It is Ok to use multiple if. But use of switch is much convenient and advisable.
Corrected code is as following:
int main() {
int nej;
int nerr = 0;
while (nerr < 4) {
printf("1-Triangulo \n");
printf("2-Division \n");
printf("3-Menu \n");
printf("4-Bosque \n");
printf("0-Salir \n");
scanf("%d", &nej);
switch(nej){
case 1:// Ejercicio 1-Triangulo:
triangulo();
break;
case 2:// Ejercicio 2-Division:
division();
break;
case 3:
menu();
break;
case 4:
arboles();
break;
case 0:// Salida
exit(1);
break;
default:
nerr++;
}
// Reset 'nerr' if any valid vaue is entered by user
if ((nej >= 0) && (nej <= 4)) {
nerr = 0;
}
}
}

yes or no to exit a switch statement

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);
}
}
}

C, if statements and strcmp

I don't understand why it does not pick up on the inputs. It always displays "Invalid Input"... Help!
while(1)
{
fgets(input, MAX, stdin);
printf("%s", input);
if(strcmp(input, "1") == 0)
{
add();
}
else if(strcmp(input, "2") == 0)
{
delete();
}
else if(strcmp(input, "3") == 0)
{
view();
}
else if(strcmp(input, "4") == 0)
{
break;
}
else
{
printf("Invalid Input!\n");
}
}
Because the value stored by fgets() contains a trailing '\n'.
Try this
int stop = 0;
while (stop == 0)
{
fgets(input, MAX, stdin);
printf("%s", input);
if (strcmp(input, "1\n") == 0)
add();
else if (strcmp(input, "2\n") == 0)
delete();
else if (strcmp(input, "3\n") == 0)
view();
else if (strcmp(input, "4\n") == 0)
stop = 1;
else
printf("Invalid Input!\n");
}
did it work?
So you need to remove it from input or adding to the comparison string.
In addition to what #iharob said, I would suggest using strncmp to check your inputs. That function lets you explicitly specify how many characters to compare. See here for a function definition.
int stop = 0;
while (stop == 0)
{
fgets(input, MAX, stdin);
printf("%s", input);
if (strncmp(input, "1", 1) == 0)
add();
else if (strncmp(input, "2", 1) == 0)
delete();
else if (strncmp(input, "3", 1) == 0)
view();
else if (strncmp(input, "4", 1) == 0)
stop = 1;
else
printf("Invalid Input!\n");
}

How can I make a function that counts the total orders I have made and adds the price up?

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.
}
...
}

Resources