Function to repeat a certain menu in C - c

I am new to C programming language. I want the following code to repeat the main menu after the "Transaction successful" and also store a new balance after an amount is entered.How do i go about it. Here is the code
#include<stdio.h>
int main()
{
int mainmenu, amt, balance = 0;
printf("1.Pay \n");
printf("2.Balance \n");
printf("3.Transaction history \n");
scanf("%d",&mainmenu);
if(mainmenu == 1){
printf("Select Amount \n");
printf("$0.50 \n");
printf("$1.00 \n");
printf("$1.50 \n");
printf("$2.00 \n");
scanf("%d",&amt);
if(amt == 1){
balance = balance + 0.5;
printf("Transaction Successful \n");
}
if(amt == 2){
balance = balance + 1;
printf("Transaction Successful \n");
}
if(amt == 3){
balance = balance + 1.5;
printf("Transaction Successful \n");
}
if(amt == 4){
balance = balance + 2;
printf("Transaction Successful \n");
}
}
if(mainmenu == 2){
printf("\n");
printf("Balance = %d",balance,"\n");
}
if(mainmenu == 3){
printf("No Transaction History At The Moment");
}
}

Simplest way is probably just to use a while loop in this case.
while(1){
//mainmenu here
printf("4. Exit Program");
scanf("%d", &mainmenu);
//if statements/functions here
}
This would require you to add in a fourth option in order to exit out of the while loop, so it doesn't run endlessly (you could also use other methods to know when to exit). A good aspect of this is if they accidentally put in something invalid such as 5 or 7, it doesn't cause any bugs but rather just runs the loop again.
Keep in mind that you'd have to put all your if statements within the while loop, or put them in a different function and call that function after you get the user's input.
P.S. Unrelated but switch statements would probably be a bit easier than if else statements here. They're not necessary but could make your life a little easier.

There are two different methods you can use. A while loop, or a for loop.
The syntax for both are like so:
while(condition) {
statement(s); // execute this section of code until the condition is met
}
and
for (initilization; condition; increment){
statement(s); // execute this section of code until the condition is met
}
if you want an infinite loop, you can use TRUE as the condition in the while loop like:
while(TRUE) {
statement(s); // execute this section of code indefinitely
}
or you can leave the parameters empty in the for loop like so:
for( ; ; ){
statement(s); // execute this section of code indefinitely
}
in your case, it would look like:
#include<stdio.h>
int main()
{
int mainmenu, amt, balance = 0;
while(TRUE){
printf("1.Pay \n");
printf("2.Balance \n");
printf("3.Transaction history \n");
scanf("%d",&mainmenu);
if(mainmenu == 1){
printf("Select Amount \n");
printf("$0.50 \n");
printf("$1.00 \n");
printf("$1.50 \n");
printf("$2.00 \n");
scanf("%d",&amt);
if(amt == 1){
balance = balance + 0.5;
printf("Transaction Successful \n");
}
if(amt == 2){
balance = balance + 1;
printf("Transaction Successful \n");
}
if(amt == 3){
balance = balance + 1.5;
printf("Transaction Successful \n");
}
if(amt == 4){
balance = balance + 2;
printf("Transaction Successful \n");
}
}
if(mainmenu == 2){
printf("\n");
printf("Balance = %d",balance,"\n");
}
if(mainmenu == 3){
printf("No Transaction History At The Moment");
}
}
}
or:
#include<stdio.h>
int main()
{
int mainmenu, amt, balance = 0;
for( ; ; ){
printf("1.Pay \n");
printf("2.Balance \n");
printf("3.Transaction history \n");
scanf("%d",&mainmenu);
if(mainmenu == 1){
printf("Select Amount \n");
printf("$0.50 \n");
printf("$1.00 \n");
printf("$1.50 \n");
printf("$2.00 \n");
scanf("%d",&amt);
if(amt == 1){
balance = balance + 0.5;
printf("Transaction Successful \n");
}
if(amt == 2){
balance = balance + 1;
printf("Transaction Successful \n");
}
if(amt == 3){
balance = balance + 1.5;
printf("Transaction Successful \n");
}
if(amt == 4){
balance = balance + 2;
printf("Transaction Successful \n");
}
}
if(mainmenu == 2){
printf("\n");
printf("Balance = %d",balance,"\n");
}
if(mainmenu == 3){
printf("No Transaction History At The Moment");
}
}
}

Related

Morra - Odds and Even

Im writing a code that evolves the computer and I entering numbers and added them together to see who wins.
There are three problems:
Problem 1: When I try to recall the 'main()' function the 'game()' wont appear.
Problem 2: I can't seem to run the code forever until user decides to stop.
Problem 3: The point system isn't accurate enough.
Any help would be grateful.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int human_fingers;
int comp_fingers;
int menu_choice;
int answer;
int count = 1;
int point1 = 0, point2 = 0;
int total;
void intro() {
printf("Welcome to Morra - Odds and Even!\n\n");
printf("The rules of the game are pretty simple\n");
printf("You and the computer will pick a side each round\n");
printf("You must enter a number and the total sum will determine the winner!\n\n");
}
void example() {
printf("You picked even, by default the computer will be odd this round\n");
printf("You entered 3 and the computer entered 5\n");
printf("3 + 5 = 8, so you win because you choose even!\n");
}
void game() {
while(count < 7)
{
count = count + 1;
printf("Enter a number to choose a side:\n");
printf("Even [1] / Odd [2]\n");
scanf("%d", &menu_choice);
while((menu_choice<1) || (menu_choice>2)){
printf("Invalid entry, please Enter 1-2: ");
scanf("%d",&menu_choice);}
if(menu_choice == 1)
{
printf("The computer will be odd this turn\n");
printf("\nPlease enter an number (1-10)\n");
scanf("%d", &human_fingers);
while((human_fingers<1) || (human_fingers>10)){
printf("Invalid entry, please enter 1-10:");
scanf("%d",&human_fingers);}
printf("Computer is choosing a number....\n");
srand(time(NULL));
comp_fingers = rand() % 10 + 1;
printf("You: %d\n", human_fingers);
printf("Computer: %d\n", comp_fingers);
int result;
result = human_fingers + comp_fingers;
printf("Total is %d\n", result);
total = result % 2;
if(total == 0)
{
printf("This turn goes to You!\n\n");
printf("You: %d\n", point1 + 1);
printf("Computer: %d\n\n", point2 + 0 );
point1++;
}
else
{
printf("This turn goes to Computer!\n\n");
printf("You: %d\n", point1 + 0 );
printf("Computer: %d\n\n", point2 + 1);
point2++;
}
}
if(menu_choice == 2)
{
printf("The computer will be even this turn\n");
printf("\nPlease enter an number (1-10)\n");
scanf("%d", &human_fingers);
while((human_fingers<1) || (human_fingers>10)){
printf("Invalid entry, please enter 1-10:");
scanf("%d",&human_fingers);}
printf("Computer is choosing a number...\n");
srand(time(NULL));
comp_fingers = rand() % 10 + 1;
printf("You: %d\n", human_fingers);
printf("Computer: %d\n", comp_fingers);
int result;
result = human_fingers + comp_fingers;
printf("Total is %d\n", result);
total = result % 1;
if(total == 0)
{
printf("This turn goes to Computer!\n\n");
printf("You: %d\n", point1 + 0);
printf("Computer: %d\n\n", point2 + 1);
point1++;
}
else
{
printf("This turn goes to You!\n\n");
printf("You: %d\n", point1 + 1);
printf("Computer: %d\n\n", point2 + 0 );
point2++;
}
}
}
if(point1 > point2)
printf("You won, you beat the computer!\n");
else
printf("Unlucky the computer won!\n");
}
void end(){
printf("Game has ended!\n");
}
int main()
{
intro();
printf("Would you like an example to demostrate?\n");
printf("Yes [1] / No [2]\n");
scanf("%d", &answer);
if( answer == 1 )
{ example();
printf("\n\n");
game();
}
else
{ printf("\n\n");
game();
}
end();
printf("Would you like play another game?\n");
printf("Yes [1] / No [2]\n");
scanf("%d", &answer);
if( answer == 1)
{
main();
}
else {
printf("Thanks for playing MORRA - ODDS AND EVEN.");
}
return 0;
}
Why not:
int main()
{
for(;;) // Infinite loop. -- equivalent of while(1) if you prefer.
{
intro();
printf("Would you like an example to demostrate?\n");
printf("Yes [1] / No [2]\n");
scanf("%d", &answer);
if( answer == 1 )
{ example();
printf("\n\n");
game();
}
else
{ printf("\n\n");
game();
}
end();
printf("Would you like play another game?\n");
printf("Yes [1] / No [2]\n");
scanf("%d", &answer);
if( answer != 1)
{
printf("Thanks for playing MORRA - ODDS AND EVEN.");
break;
//^___ exit the infinite loop.
}
}
return 0;
}
Recalling the main you should definitely not, in almost any case. Just create loops if you want to repeat everything.

Vending Machine - Logic Error

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

Check for validation if Input exceeds the available stocks

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

C programming help - Adding Values together

So I have this code but I'm missing something and I can't quite seem to get it. The point of this is to prompt the user for a selection. They pick the food that they want to order, and it tells them it is added. When the user hits 0 the program ends by saying what the total is of all the items they chose. For some reason when I hit the end, it just states the total of the last item added. Any ideas on how to fix this so that the totals all add up?
#include<stdio.h>
#define SALES_TAX .06
int selection;
double total;
double amount;
int i;
double getPrice(int selection);
void printOptionName(int selection);
void printMenu();
double getPrice(int selection){
if (selection == 1){
amount = 5.99;
} else if (selection == 2){
amount = 6.99;
} else if (selection == 3){
amount = 7.99;
} else if (selection == 4){
amount = 10.50;
} else if (selection == 5){
amount = 3.50;
}
}
void printOptionName(int selection){
if (selection == 0){
printf("\nYour total is: ");
} else if (selection == 1){
printf("\nAdded a Small Pizza\n\n");
} else if (selection == 2){
printf("\nAdded a Medium Pizza\n\n");
} else if (selection == 3){
printf("\nAdded a Large Pizza\n\n");
} else if (selection == 4){
printf("\nAdded an order of Wings\n\n");
} else if (selection == 5){
printf("\nAdded a Drink\n\n");
} else
printf("Not a valid selection. Please select one of the following options: \n");
}
void printMenu(){
printf("0. (Complete Order)\n");
printf("1. Small Pizza ****** 5.99\n");
printf("2. Medium Pizza ****** 6.99\n");
printf("3. Large Pizza ****** 7.99\n");
printf("4. Wings ****** 10.50\n");
printf("5. Drink ****** 3.50\n");
printf("Enter the Number of your selection: \n");
}
int main(){
printMenu();
scanf("%d", &selection);
printOptionName(selection);
getPrice(selection);
while (selection != 0){
printMenu();
scanf("%d", &selection);
printOptionName(selection);
getPrice(selection);
}
for (i = 0; i <= selection; i++){
total = total + amount;
}
printf("%lf\n", total);
return 0;
}
global variables are just not a good way to go. notice that you don't use selection in printmenu(), for example; you may want to pass the necessary data as arguments to a function. Also, you seem to be passing by reference in a function that passes by value. Notice that your program doesn't stop if it goes to the else statement in printOptionName(), or in getPrice(), selection will still be zero...

How do I use a for loop to retrieve from the user the indicated number of items indicated?

int a = 1;
printf("Enter the number of items from 1 and 10: \n");
while (a <= 10)
{
scanf("%d", &a);
if (a >= 1 && a <= 10)
{
printf("Thank You!\n");
break;
}
else
{
printf("Wrong input! Try Again.\n");
continue;
}
}
To be more detailed about what I'm asking lets say that the user enters 3 (for 3 items) how would I use the for loop to retrieve that information so I can further finish the code.
You should keep in mind following points:
Get the no.of choice before starting loop
Check the condition in loop with no. of choices.
Only one loop is enough for your task.
I think you need this:
int a = 1;
bool bFlag = true;
int price[10];
printf("Enter the number of items from 1 and 10: \n");
while(bFlag){
scanf("%d", &a);
if (a >= 1 && a <= 10)
{
printf("Thank You!\n");
break;
}
else
{
printf("Wrong input! Try Again.\n");
continue;
}
}
for (int i = 0; i< a; i++)
{
printf("Enter price for item %d = ", i);
scanf("%d",&price[i]);
}

Resources