Hey guys I am constructing an ATM Program, and I have everything ok
I have the menu it pulls up you can select an option and it runs the function HOWEVER, I cannot for the life of me
set a balance and
get it to stay until its changed
and I need it to save once it has changed in one of the two options (deposit, withdrawl) since this is a post test loop it will keep going until exit is selected and every time I need this to update the balance.
Here is my C Code for it, if anyone could help that would be amazing.
#include <stdio.h>
#include <stdlib.h>
// Function Declarations
int getChoice ();
double withdraw (int Choice, int Balance);
double deposit (int Choice, int Balance);
int VBalance (int Choice, int Balance);
double process (int Choice, int Balance);
int main (void)
{
// Local Declarations
int Choice;
int Balance;
// Statements
do
{
Balance = 2500.00;
Choice = getChoice ();
process (Choice, Balance);
}
while (Choice != 0);
return 0;
} // Main
/*============================process=*/
double process (int Choice, int Balance)
{
// Declarations
// Statements
switch(Choice)
{
case 1: withdraw (Choice, Balance);
break;
case 2: deposit (Choice, Balance);
break;
case 3: VBalance (Choice, Balance);
break;
case 0: exit;
break;
deafult: printf("Sorry Option Not Offered");
} // switch
return 0;
}
/*============================getChoice=*/
int getChoice (void)
{
// Local Declarations
char Choice;
// Statements
printf("\n\n**********************************");
printf("\n MENU ");
printf("\n\t1.Withdrawl Money ");
printf("\n\t2.Deposit Money ");
printf("\n\t3.View Balance ");
printf("\n\t0.Exit ");
printf("\n**********************************");
printf("\nPlease Type Your Choice Using 0-3");
printf("\nThen Hit Enter: ");
scanf("%d", &Choice);
return Choice;
} //getchoice
/*============================withdraw=*/
double withdraw (int Choice, int Balance)
{
// Local Declarations
double amount;
// Statements
printf("Funds:%d", &Balance);
printf("\nPlease Enter How Much You Would Like To Withdraw: ");
scanf("%f", &amount);
Balance = Balance - amount;
return Balance;
} //withdraw
/*============================Deposit=*/
double deposit (int Choice, int Balance)
{
// Local Declarations
double amount;
// Statements
printf("Funds:%d", &Balance);
printf("\nPlease Enter How Much You Would Like To Deposit: ");
scanf("%f", &amount);
Balance = Balance + amount;
return Balance;
} //Deposit
/*============================VBalance=*/
int VBalance (int Choice, int Balance)
{
// Statements
printf("\nYour Current Funds:%d", &Balance);
printf("\nThank Your For Viewing");
return 0;
}
First: Enable compiler warnings. If you use gcc, add -Wall to the command line. If you use an IDE the option to turn on warnings should be in the compiler settings.
When you compile with warnings you will find that you have some problems with your use of printf and scanf. Fix those!
Next you have the problem with Balance in main not getting updated. C uses call by value which means that the changes you make to a function argument are not visible in the calling function. To work around that you could declare the balance parameters as pointers and pass the address of Balance. Or you could return the new Balance, just like you do in your code. The only thing you forgot is to store the new value to Balance in main.
Changed line in main:
Balance = process(Choice, Balance);
Changed process:
double process(int Choice, int Balance)
{
// Declarations
// Statements
switch (Choice)
{
case 1: Balance = withdraw(Choice, Balance); // Changed line
break;
case 2: Balance = deposit(Choice, Balance); // Changed line
break;
case 3: Balance = VBalance(Choice, Balance); // Changed line
break;
case 0: exit;
break;
deafult: printf("Sorry Option Not Offered");
} // switch
return Balance; // Changed line
}
Third: Balance is declared an int, but you sometimes use double. You should choose one type and stick to it. Due to the nature of floating point numbers it is actually recommended that an integral type is used for money.
Related
I might be giving more than enough but long story short I am working on an ATM machine program and I am trying to put the "switch" statement in the main function inside a loop so the user can get more transactions.
I am running into a problem where I would deposit 100 but then when I check the balance it is still at 0. I know everything else works fine but that loop is killing me, I would appreciate any help!
Don't mind all of the extra stuff it is just there to give an idea on what i am working on
int main ()
{
char option;
float balance ;
int count = 1;
option = displayMenu();
do
{
switch (option)
{
case 'D':
getDeposit(balance);
main();
count++;
break;
case 'W':
getWithdrawal(balance);
main();
count++;
break;
case 'B':
displayBalance(balance);
main();
count++;
break;
case 'Q':
printf("Thank you!");
break;
main();
}
} while ( count <= 5);
return 0;
}
char displayMenu()
{
char option;
printf("\n Welcome to HFC Federal Credit Union \n");
printf("\n Please select from the following menu: \n ");
printf("\n D: Make a deposit \n ");
printf("\n W: Make a withdrawal \n ");
printf("\n B: Check your account balance \n ");
printf("\n Q: To quit \n ");
scanf("\n%c" , &option);
return option;
}
float getDeposit(float balance)
{
float deposit;
printf("\n Please enter the amount you want to deposit! ");
scanf("%f" , &deposit);
balance += deposit;
return balance;
}
float getWithdrawal(float balance)
{
float withdrawal;
printf("\n Please enter the amount you want to withdraw! ");
scanf("%f" , &withdrawal);
balance -= withdrawal;
return balance;
}
void displayBalance(float balance)
{
printf("\n Your current balance is %f " , balance);
}
You're recursively calling main() on every iteration of the loop. Just remove this call, and you should be good to go.
You'll also need to assign the return values of your functions to balance, otherwise they won't be able to affect its value.
There are a number of issues with this code... Here are my main pointers (but not all of them, I'm just answering the question):
You're calling main over and over again, for simplicity, you could consider this as restarting the application every time (except for stack issues, that I'm ignoring and other nasty side effects).
You didn't initialize the balance (and friends) variables. They might contain "junk" data.
You're ignoring the return values from the functions you use. If you're not using pointer, you should use assignment.
Your menu printing function is out of the loop... I doubt if that's what you wanted.
Here's a quick dirty fix (untested):
int main() {
char option;
float balance = 0;
int count = 1;
do {
option = displayMenu(); // moved into the loop.
switch (option) {
case 'D':
balance = getDeposit(balance);
count++;
break;
case 'W':
balance = getWithdrawal(balance);
count++;
break;
case 'B':
balance = displayBalance(balance);
count++;
break;
case 'Q':
printf("Thank you!");
break;
}
} while (count <= 5);
return 0;
}
char displayMenu(void) {
char option;
printf("\n Welcome to HFC Federal Credit Union \n");
printf("\n Please select from the following menu: \n ");
printf("\n D: Make a deposit \n ");
printf("\n W: Make a withdrawal \n ");
printf("\n B: Check your account balance \n ");
printf("\n Q: To quit \n ");
scanf("\n%c", &option);
return option;
}
float getDeposit(float balance) {
float deposit;
printf("\n Please enter the amount you want to deposit! ");
scanf("%f", &deposit);
balance += deposit;
return balance;
}
float getWithdrawal(float balance) {
float withdrawal;
printf("\n Please enter the amount you want to withdraw! ");
scanf("%f", &withdrawal);
balance -= withdrawal;
return balance;
}
void displayBalance(float balance) {
printf("\n Your current balance is %f ", balance);
}
Good Luck!
I think the main problem is the update of the switch control variable outside the loop.
Reacting to "Q" with ending is somewhat necessary... then only allowing 5 becomes unneeded.
I fixed several other things, too; and provided comments on them.
And I improved testability a little (5->6). I kept the counting, just extended to 6, in order to allow a complete test "D 100 , B, W 50, B ,Q".
Nice design by the way, to return the balance from the functions, instead of using pointers or global variable. But you need to use the return value instead of ignoring it.
/* include necessary headers, do not skip this when making your MCVE */
#include <stdio.h>
/* prototypes of your functions,
necessary to avoid the "immplicitly declared" warnigns
when compiling "gcc -Wall -Wextra"; which you should
*/
char displayMenu(void);
float getDeposit(float balance);
float getWithdrawal(float balance);
void displayBalance(float balance);
/* slightly better header of main, with "void" */
int main (void)
{
char option;
float balance=0.0; /* initialise your main variable */
int count = 1;
/* your very important update of the switch control variable has been moved ... */
do
{
option = displayMenu(); /* ...here */
/* If you do not update your switch variable inside the loop,
then it will forever think about the very first command,
this explains most of your problem.
*/
switch (option)
{
case 'D':
balance=getDeposit(balance); /* update balance */
/* removed the recursive call to main(),
it is not needed as a solution to the problem that the program
always uses the first command (when updating inside the loop)
and otherwise just makes everything much more complicated and
risky.
*/
count++;
break;
case 'W':
balance=getWithdrawal(balance); /* update balance */
count++;
break;
case 'B':
displayBalance(balance);
count++;
break;
case 'Q':
printf("Thank you!");
/* adding a way to get out of the loop,
using a magic value for the count,
this is a mehtod frowned upon by most,
but it minimises the changes needed to your
own coding attempt.
*/
count=0;
break;
}
} while ( (count <= 6)&&(count>0) ); /* additionally check for the magic "Q" value
check against count<=6, to allow testing D,B,W,B,Q */
return 0;
}
/* use explicitly empty parameter list for functions */
char displayMenu(void)
{
char option;
printf("\n Welcome to HFC Federal Credit Union \n");
printf("\n Please select from the following menu: \n ");
printf("\n D: Make a deposit \n ");
printf("\n W: Make a withdrawal \n ");
printf("\n B: Check your account balance \n ");
printf("\n Q: To quit \n ");
scanf("\n%c" , &option);
return option;
}
float getDeposit(float balance)
{
float deposit;
printf("\n Please enter the amount you want to deposit! ");
scanf("%f" , &deposit);
balance += deposit;
return balance;
}
float getWithdrawal(float balance)
{
float withdrawal;
printf("\n Please enter the amount you want to withdraw! ");
scanf("%f" , &withdrawal);
balance -= withdrawal;
return balance;
}
void displayBalance(float balance)
{
printf("\n Your current balance is %f " , balance);
}
you haven't changed the variable in main().
you can change the loop to this:
do
{
switch (option)
{
case 'D':
balance = getDeposit(balance);
count++;
break;
case 'W':
balance = getWithdrawal(balance);
count++;
break;
case 'B':
displayBalance(balance);
count++;
break;
case 'Q':
printf("Thank you!");
break;
}
} while (count <= 5);
I wrote this piece of code, but cases 2 and 3 seem to have a problem. As noted in the title I think it has to do with unsigned long operations, but I can't understand what it is exactly.
*Edited version (scanf changes).
#include <stdio.h>
int main()
{
int pin, inp, count=0;
unsigned dep=100, add, withdraw;
start:;
if (count>0)
{
printf("\n");
}
printf("Please, input your PIN number:");
scanf("%i", &pin);
while (5)
{
if (pin==2014)
{
printf("To view your deposit, press 1.\n");
printf("To add money to your deposit, press 2.\n");
printf("To withdraw money from your deposit, press 3.\n");
printf("To log off, press 4.\n");
scanf("%i", &inp);
switch(inp)
{
case 1:
printf("The remainder of your deposit is %i.\n\n", dep);
break;
case 2:
printf("Enter the amount of money you want to add: ");
scanf("%u", add);
dep+=add;
break;
case 3:
printf("Enter the amount of money you want to withdraw. We would like to remind you that it should be multiple of 20.\n");
scanf("%u", withdraw);
if(((withdraw)%20==0)&&(withdraw<dep))
{
dep-=withdraw;
}
else
{
printf("We are sorry, but you either chose an invalid withdraw amount or you tried to withdrw more money than you have deposited.\n");
}
break;
case 4:
printf("Logging off.\n");
goto end;
break;
}
}
else
{
printf("You entered an invalid PIN.");
count++;
goto start;
}
}
end:;
}
You're not using scanf correctly.
scanf("%lu", add);
For "%lu" it expects a pointer to an unsigned long int, but what you're passing is not a pointer and not an unsigned long int.
Try:
scanf("%u", &add);
Or change add's type.
I'd also recommend checking scanf's returned value.
See: Value returned by scanf function in c
I've been writing the code for an ATM inter face where i include a pin that must be imputed by the user to access the options. Once thats done, I get 5 options to choose from such as fast cash, withdraw, deposit and check balance. everything seems to be running good the only thing is that the account balance is not be updated to the correct amount when the user deposits, withdraws, or gets fast cash. Can someone help me fix this. I'll post my code down below
#include <stdio.h>
int fastCash(int amount);
int deposit(int deposit);
int withdraw(int balence);
void checkBalence(int balence);
int main()
{
int pin;
int pins = 9999;
int pinTries = 1;
int reciept;
int options;
int options2;
int balence = 300;
int fastCashChoice;
printf("Enter your pin:");// the pin is 9999
scanf("%d", &pin);
while (pinTries <= 3)
{
if (pin == pins)
{
printf("Would you like a reciept:");
//1 is equal to yes and 2 is equal to no
scanf("%d", &reciept);
printf("Choose from the following:\n");
printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
scanf("%d", &options);
while (options <= 5)
{
switch (options)
{
case 1:
fastCash(fastCashChoice);
balence = balence - fastCashChoice;
break;
case 2:
withdraw(balence);
break;
case 3:
deposit(balence);
break;
case 4:
checkBalence(balence);
break;
case 5:
options2 == 2;
break;
}
printf("Would you like anohter transaction: ");// 1 is equal to yes and 2 is equal to no
scanf("%d", &options2);
if (options2 == 1)
{
printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
scanf("%d", &options);
}
else
{
options = 5;
pinTries = 4;
printf("Thank you for useing this ATM, GoodBye\n");
}
}
}
else if (pin != pins)
{
printf("Invalid pin, try again:");
scanf("%d", &pin);
pinTries++;
}
if (pinTries == 3)
{
printf("Sorry, you cant continue, please contact your bank");
}
}
return 0;
}
int fastCash(int amount)
{
int choice;
printf("1. $20.00\n2. 40.00\n3. 80.00\n4. 100.00\n5. Exit");
scanf("%d", &choice);
switch (choice)
{
case 1:
amount = 20;
case 2:
amount = 40;
case 3:
amount = 80;
case 4:
amount = 100;
case 5:
break;
}
return amount;
}
int withdraw(int balence)
{
int withdrawAmount;
printf("Enter the amount you would like to withdraw: ");
scanf("%d", &withdrawAmount);
balence = -withdrawAmount;
return balence;
}
int deposit(int balence)
{
int depositAmount;
printf("Enter an amount you would like to deposit: ");
scanf("%d", &depositAmount);
balence += depositAmount;
return balence;
}
void checkBalence(int balence)
{
printf("Your current balence is: %d\n", balence);
return;
}
When you pass an int (or any other non-pointer variable, for that matter) to a function, you only pass a copy of it. If the function then changes it (as deposit, e.g., does), it will only change the passed copy, and won't affect the original variable. Instead, you need to pass a pointer to the original value. E.g.:
int deposit(int* balance)
{
int depositAmount;
printf("Enter an amount you would like to deposit: ");
scanf("%d", &depositAmount);
*balance += depositAmount;
}
And the calling function should pass the pointer to this variable instead of the variable itself:
case 3:
deposit(&balance);
/* Here-^ */
break;
In your code, you seem to just pass in the deposit to the function, but you don't reference it back to the original balence variable, so the balance stays at 300.
For example, in the function:
int deposit(int balence)
{
int depositAmount;
printf("Enter an amount you would like to deposit: ");
scanf("%d", &depositAmount);
balence += depositAmount;
return balence;
}
You just sent in your balence in, but like how Mureinik said, you just passed in the original value without changing its value (it only changed the balence inside of deposit().
Instead, you can pass it in by reference, or you can move balence to the top of the code as a global variable so that all the functions can see it:
//code here.....
void checkBalence();
int balence = 300;
//more code here...
Also make sure to remove the balence call in the deposit() function to avoid ambiguity between the local and global variables..
int deposit()
{
/*..original code here..*/
}
...and now, in the deposit() function, your balence variable now points to the global balence.
Here is the final, corrected code:
#include <stdio.h>
int fastCash(int amount);
int deposit();
int withdraw();
void checkBalence();
int balence = 300;
int main()
{
int pin;
int pins = 9999;
int pinTries = 1;
int reciept;
int options;
int options2;
int fastCashChoice;
printf("Enter your pin:");// the pin is 9999
scanf("%d", &pin);
while(pinTries <= 3)
{
if(pin == pins)
{
printf("Would you like a reciept:");
//1 is equal to yes and 2 is equal to no
scanf("%d", &reciept);
printf("Choose from the following:\n");
printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
scanf("%d", &options);
while(options <= 5)
{
switch(options)
{
case 1:
fastCash(fastCashChoice);
balence = balence - fastCashChoice;
break;
case 2:
withdraw(balence);
break;
case 3:
deposit(balence);
break;
case 4:
checkBalence(balence);
break;
case 5:
options2 = 2;
break;
}
printf("Would you like anohter transaction: ");// 1 is equal to yes and 2 is equal to no
scanf("%d", &options2);
if(options2 == 1)
{
printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
scanf("%d", &options);
}
else
{
options = 5;
pinTries = 4;
printf("Thank you for useing this ATM, GoodBye\n");
break;
}
}
}
else if(pin != pins)
{
printf("Invalid pin, try again:");
scanf("%d", &pin);
pinTries++;
}
if(pinTries == 3)
{
printf("Sorry, you cant continue, please contact your bank");
}
}
return 0;
}
int fastCash(int amount)
{
int choice;
printf("1. $20.00\n2. 40.00\n3. 80.00\n4. 100.00\n5. Exit");
scanf("%d", &choice);
switch(choice)
{
case 1:
amount = 20;
case 2:
amount = 40;
case 3:
amount = 80;
case 4:
amount = 100;
case 5:
break;
}
return amount;
}
int withdraw()
{
int withdrawAmount;
printf("Enter the amount you would like to withdraw: ");
scanf("%d", &withdrawAmount);
balence -= withdrawAmount;
return balence;
}
int deposit()
{
int depositAmount;
printf("Enter an amount you would like to deposit: ");
scanf("%d", &depositAmount);
balence += depositAmount;
return balence;
}
void checkBalence(int balence)
{
printf("Your current balence is: %d\n", balence);
return;
}
Now, it should run as expected, here producing a final balance of $176:
Enter your pin:9999
Would you like a reciept:1
Choose from the following:
1. Fast cash
2. Withdraw
3. Deposit
4. Check balence
5. Get card back2
Enter the amount you would like to withdraw: 124
Would you like anohter transaction: 1
1. Fast cash
2. Withdraw
3. Deposit
4. Check balence
5. Get card back4
Your current balence is: 176
Well, an easy solution to your problem is to declare your int balence as global. Instead of declaring it inside main() function, you can declare it above the main() function.
This will solve your current problem.
Both deposit and withdraw returns the updated balance but you don't use the return value. Try changing the calls to:
case 2:
balence = withdraw(balence);
break;
case 3:
balence = deposit(balence);
break;
fastCash returns the amount of cash to withdraw, so you need to update the balance in main:
case 1:
balence = balence - fastCash(fastCashChoice);
break;
This avoids both pointers (for which you would want additional error handling, i.e. check for NULL) and global variables (which makes your program more complex*).
There are also a few more problems in your code. The argument sent to fastCash isn't really used at all, since you return the amount to withdraw.
* Are global variables bad?
I am trying to compile a small bank program in C in visual studio 2012 express. It shows me this error "undeclared identifier" for almost all variables and this one too "syntax error: missing ';' before 'type'".Please tell me the correct syntax.Thank you.
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Welcome to skybank\n");
int deposit,withdraw,kbalance;
char option;
printf("Press 1 to deposit cash\n");
printf("Press 2 to Withdraw Cash\n");
printf("Press 3 to Know Your Balance\n");
scanf_s("%c",option);
int decash,wicash;
switch(option)
{
int balance;
printf("Enter your current Balance\n");
scanf_s("%d",&balance);
case 1:
printf("Enter the amount you want to deposit\n");
scanf_s("%d",&decash);
printf("Thank You\n");
printf("%d have been deposited in your account\n",decash);
break;
case 2:
printf("Enter the amount you want to withdraw\n");
scanf_s("%d",&wicash);
int wibal;
wibal=balance-wicash;
printf("Thank You\n");
printf("%d have been withdrawed from your account\n",wicash);
printf("Your balance is %d\n",wibal);
break;
case 3:
printf("Your balance is Rs.%d\n",balance);
break;
default:
printf("Invalid Input\n");
break;
}
getchar();
}
The Microsoft C compiler only supports a 25 year old version of the language. And one of the limitations is that all variables must be declared before any other statements. So move all your variable declarations to the top of the function.
The next error I can see is the use of scanf_s with the %c format string. You must pass a pointer to the variable, and pass the number of characters to read.
scanf_s("%c", &option, 1);
And likewise you need to pass an address for the read of balance.
You also need to change the switch statement so that it just contains cases. Move the bare instructions outside.
Your reading of option won't work. Because when you check for 1 you are checking for the character with ASCII code 1. Change option to be an int and read using %d.
Perhaps you are looking for something like this:
#include<stdio.h>
#include<conio.h>
int main(void)
{
int deposit,withdraw,kbalance;
int option;
int decash,wicash;
int balance;
int wibal;
printf("Welcome to skybank\n");
printf("Press 1 to deposit cash\n");
printf("Press 2 to Withdraw Cash\n");
printf("Press 3 to Know Your Balance\n");
scanf_s("%d", &option);
printf("Enter your current Balance\n");
scanf_s("%d", &balance);
switch(option)
{
case 1:
printf("Enter the amount you want to deposit\n");
scanf_s("%d", &decash);
printf("Thank You\n");
printf("%d have been deposited in your account\n", decash);
break;
case 2:
printf("Enter the amount you want to withdraw\n");
scanf_s("%d", &wicash);
wibal=balance-wicash;
printf("Thank You\n");
printf("%d have been withdrawed from your account\n", wicash);
printf("Your balance is %d\n", wibal);
break;
case 3:
printf("Your balance is Rs.%d\n", balance);
break;
default:
printf("Invalid Input\n");
break;
}
getchar();
}
Regarding the unidentified variables, try putting all declarations of variables at the top of the main block, something like:
int main()
{
int deposit, withdraw, kbalance, decash, wicash, wibal;
char option;
printf("Welcome to skybank\n");
Older variants of C frown upon mixing variable declarations with code. To my knowledge the C standard of Microsoft's C implementation is pre-C99 so perhaps this could be the issue.
A few other issues that you should look into:
scanf_s("%c",option); - option should be &option as you are taking a pointer to that variable.
Also here: case 1:
You want '1' (as in case '1') instead of plain 1 as it is a char, not an int you want.
Same for the other case checks.
With regards to the scanf_s problems, try compiling with warnings, it should have been pointed out by the compiler.
Finally, you might want to rid your code of the variables you're not using such as kbalance, withdraw and deposit.
do at the beginning of the block in the declaration of the variable for visual c.
E.g.
int main()
{
int deposit,withdraw,kbalance;
char option;
int decash,wicash
int balance;
int wibal;
...
try this code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Welcome to skybank\n");
int deposit,withdraw,kbalance;
char option;
printf("Press 1 to deposit cash\n");
printf("Press 2 to Withdraw Cash\n");
printf("Press 3 to Know Your Balance\n");
scanf("%c",&option);
int decash,wicash;
switch(option)
{
int balance;
printf("Enter your current Balance\n");
scanf("%d",&balance);
case 1:
printf("Enter the amount you want to deposit\n");
scanf("%d",&decash);
printf("Thank You\n");
printf("%d have been deposited in your account\n",decash);
break;
case 2:
printf("Enter the amount you want to withdraw\n");
scanf("%d",&wicash);
int wibal;
wibal=balance-wicash;
printf("Thank You\n");
printf("%d have been withdrawed from your account\n",wicash);
printf("Your balance is %d\n",wibal);
break;
case 3:
printf("Your balance is Rs.%d\n",balance);
break;
default:
printf("Invalid Input\n");
break;
}
getchar();
}
Move this:
int balance;
printf("Enter your current Balance\n");
scanf_s("%d",&balance);
Before the switch statement.
Why do I keep getting this error? HELP ME this is homeowrk. I'm obiously new to programming help.
$ gcc homework.c
homework.c: In function ‘main’:
homework.c:32:6: error: static declaration of ‘DisplayMenu’ follows non-static declaration
homework.c:11:7: note: previous declaration of ‘DisplayMenu’ was here
#include <stdio.h>
void DisplayMenu();
void numberPlus10();
void numberTimes2();
void numberMinus1();
void numberTimesnumber();
int main (void)
{
int choice;
void DisplayMenu();
scanf("%i", &choice);
switch (choice)
{
case 1:
numberPlus10();
break;
case 2:
numberTimes2();
break;
case 3:
numberMinus1();
break;
case 4:
numberTimesnumber();
break;
default:
break;
}
void DisplayMenu()
{
printf("1. Number + 10\n");
printf("2. Number * 2\n");
printf("3. Number - 1\n");
printf("4. Number * Number\n");
}
void numberPlus10()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number + 10 is %i\n", x + 10);
}
void numberTimes2()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number * 2 is %i\n", x * 2);
}
void numberMinus1()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number - 1 is %i\n", x - 1);
}
void numberTimesnumber()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number squared is %i\n", x * x);
}
}
Pengyu CHEN is ofcourse right! But! You have another error there.
int choice;
void DisplayMenu(); // You should not declare a function here.
scanf("%i", &choice);
I guess you intend to call this function - so just remove "void" from the beginning of the line.
int choice;
DisplayMenu(); // Call DisplayMenu
scanf("%i", &choice);
And ... please read language specs
In C we don't implement functions inside any blocks. Instead functions shall be implemented in global scope.
Remove the very last right bracket and put it right after end of the switch in int main(void), and there shall be no more errors.
EDITED:
First of all.. I'm sure above is why your source code fails to compile.
Also, please check David's answer, since we all believe that you made a function declaration while you're intending to call it -- although this mistake didn't trigger an compile time error.