static declaration of ‘DisplayMenu’ follows non-static declaration - c

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.

Related

how to remove implicit function and conflicting error declarations on a various functions calculator using C program?

I am somewhat done with making this function calculator on C program, but I have one issue, and it's really making me go nuts.
I can go through the code and everything, but I keep getting implicit declaration function and conflicting errors when I compile the code.
How can I fix my code to get rid of them?
I'm new to C, and programming in general. Thank you :)
#include <stdio.h>
int main(){
int response;
do{
displayWelcomeMessage();
scanf("%d",&response);
selectOption(response);
if(response<-1||response>8){
printf("Please enter a number between 1 and 8\n");
}
}while(response!=-1);
printf("Thanks for using the program!");
return 0;
}
void selectOption(int response){
int adder(int x,int y);
int subtract(int x,int y);
int multiplicate(int x,int y);
double divisor(int x,int y);
int modeOperator(int x,int y);
int factorialOperator(int x);
double powerOperator(int x,int y);
int FibonacciOperator(int x);
int x,y;
switch(response){
case 1 :
printf("the result is: %d\n",adder(x,y));
break;
case 2 :
printf("the result is: %d\n",subtract(x,y));
break;
case 3 :
printf("the result is: %d\n",multiplicate(x,y));
break;
case 4 :
printf("the result is: %lf\n",divisor(x,y));
break;
case 5 :
printf("the result is: %d\n",modeOperator(x,y));
break;
case 6 :
printf("Please enter a variable to calculate the factorial:\n");
scanf("%d",&x);
printf("the result is: %d\n",factorialOperator(x));
break;
case 7 :
printf("Enter base:\n");
scanf("%d", &x);
printf("Enter exponent:\n");
scanf("%d", &y);
printf("the result is: %lf\n",powerOperator(x,y));
break;
case 8 :
printf("enter a value:\n");
scanf("%d",&x);
for(int i=1;i<=x;i++){
printf("%d\n",FibonacciOperator(i));
}
break;
}
}
void displayWelcomeMessage(){
printf("welcome to final programming homework!\n");
printf("please enter 1 for the addition operation\n");
printf("please enter 2 for the subtraction operation\n");
printf("please enter 3 for the multiplication operation\n");
printf("please enter 4 for the division operation\n");
printf("please enter 5 for the mode operation\n");
printf("please enter 6 for the factorial operation\n");
printf("please enter 7 for the power operation\n");
printf("please enter 8 for the Fibonacci operation\n");
printf("please enter -1 to exit\n");
}
int adder(int x,int y){
printf("please enter two variables to be added:\n");
scanf("%d %d",&x,&y);
return x+y;
}
int subtract(int x,int y){
printf("please enter two variables to be subtracted:\n");
scanf("%d %d",&x,&y);
return x-y;
}
int multiplicate(int x,int y){
printf("please add two variables to be multiplied:\n");
scanf("%d %d",&x,&y);
return x*y;
}
double divisor(int x,int y){
printf("please add two variables to be divided:\n");
scanf("%d %d",&x,&y);
return (double)x/(double)y;
}
int modeOperator(int x,int y){
printf("please add two variables to find the reminder:\n");
scanf("%d %d",&x,&y);
return x%y;
}
int factorialOperator(int x){
if (x==1)
return 1;
else
return x*factorialOperator(x-1);
}
double powerOperator(int x,int y){
if(y == 0)
return 1;
else if(y > 0)
return (double)x * powerOperator(x,y-1);
else
return 1 / powerOperator(x,-y);
}
int FibonacciOperator(int x){
if (x==1 || x==2)
return 1;
else
return FibonacciOperator(x-1)+FibonacciOperator(x-2);
}
You're calling functions before they are defined or declared.
The C compiler runs in a single pass, so if you call a function before it's declared you'll get a warning. In that case the compiler will assume the function returns int. When the compiler then finds the actual definition, if it doesn't match the implicit definition you'll get an error.
You can fix this by either rearranging the functions so that they're defined before they're used, or by adding declarations for those functions at the top of the file.
So instead of:
int main(){
...
displayWelcomeMessage();
...
}
void displayWelcomeMessage(){
...
}
You want:
void displayWelcomeMessage(){
...
}
int main(){
...
displayWelcomeMessage();
...
}
Or:
void displayWelcomeMessage();
int main(){
...
displayWelcomeMessage();
...
}
void displayWelcomeMessage(){
...
}
Do the same for all functions other than main.
You need to include the header file for the functions that you use before defining. Either because they come from the standard library such as scanf or because you define them later.

Using do loop and functions to make a menu. C

What seems to be the problem in my code? I wish to make a program that makes allows the user to choose from a list of choices/menu. The user chooses one and a function runs. After the function executes the menu appears again until the user decides to quit.
My code is as follows:
#include <stdio.h>
#include <stdlib.h>
int evaluate(int num);
int binaryPrinter(int dec);
int hexaPrinter(int dec);
int militaryTime(int hh, int mm);
int main(void)
{
int choice, x, y;
do {
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d, &choice");
(void) getchar();
switch (choice){
case 1:
printf("Enter number to be evaluated:");
scanf("%d, &x");
int evaluate(int x);
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d, &x");
int binaryPrinter(int x);
break;
case 3:
printf("Enter a decimal number: ");
scanf("&d, &x");
int hexaPrinter(int x);
break;
case 4:
printf("Enter time in standard format: ");
scanf("%d:%d, &x, &y");
int militaryTime(int x, int y);
break;
default:
printf("Invalid choice. Please choose only among the choices.");
}
}while(choice != 5);
return 0;
}
This is what is displayed after inputing the number of choice: Process returned -1073741819 <0xC0000005>
-EDIT-
I have recognized the mistake I have made. But the result's still the same.
here's my new code, but same error: Process returned -1073741819 <0xC0000005>
#include <stdio.h>
#include <stdlib.h>
int evaluate(int num);
int binaryPrinter(int dec);
int hexaPrinter(int dec);
int militaryTime(int hh, int mm);
int main(void)
{
int choice, x, y;
do {
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d", &choice);
(void) getchar();
switch (choice){
case 1:
printf("Enter number to be evaluated:");
scanf("%d", &x);
evaluate(x);
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d", &x);
binaryPrinter(x);
break;
case 3:
printf("Enter a decimal number: ");
scanf("%d", &x);
hexaPrinter(x);
break;
case 4:
printf("Enter time in standard format: ");
scanf("%d:%d", &x, &y);
militaryTime(x, y);
default:
printf("Invalid choice. Please choose only among the choices.");
}
}while(choice != 5);
return 0;
}
Try this code
#include <stdio.h>
#include <stdlib.h>
int evaluate(int num);
int binaryPrinter(int dec);
int hexaPrinter(int dec);
int militaryTime(int hh, int mm);
int main(void)
{
int choice, x, y;
do
{
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d", &choice);
(void) getchar();
switch (choice){
case 1:
printf("Enter number to be evaluated:");
scanf("%d", &x);
evaluate(x);
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d", &x);
binaryPrinter(x);
break;
case 3:
printf("Enter a decimal number: ");
scanf("&d", &x);
hexaPrinter( x);
break;
case 4:
printf("Enter time in standard format: ");
scanf("%d:%d", &x, &y);
militaryTime( x, y);
break;
default:
printf("Invalid choice. Please choose only among the choices.");
}
}while(choice != 5);
return 0;
}
You have used wrong syntax of the scanaf()
it's not like scanf("%d,&choice");
it is like scanf("%d",&choice);
And also missed the syntax of function call
it's not like int evaluate(int x); it is like evaluate(x)
Your scanf statements are all wrong. It usually takes to parameters. A format string containing format specifiers to determine what kind of value and how many values to read and a pointer to the variable were to store the read value (multiple pointers in case of multiple specifiers).
// What you got so far
scanf("%d, &choice"); // <-- format string and pointer to variables are combined
// How it should look like
scanf("%d", &choice); // format string "%d" containing 1 format specifier to read an int
// and a pointer to the variable choice to store the read value
Ok now to the next mistake. When declaring a function you need to write down the whole function header (returnType functionName(ParameterType1 Parameter1, ParameterType2 Parameter2, ...)) however when you want to call that funtion all it needs is its name and the parameters BUT whithout theire type.
So declaring a function like this int evaluate(int num) like you did at the very beginning of your code is fine but when calling it in your switch all it needs is the name (evaluate) and the values, or variables you want to pass to the function as parameters (evaluate(x)).
So now all together:
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d", &choice); // scan an integer value and store it in choice
switch (choice)
{
case 1:
printf("Enter number to be evaluated:");
scanf("%d", &x);
evaluate(x);
break;
case 2:
...
}

invalid operands to binary / (have ‘float *’ and ‘int’)?

I need to pass by reference for my class. I'm getting the error in the title, but only for the functions divideByTweleve and multiplyByEleven. Why is that? What am I doing wrong?
Please help me figure this out.
#include <stdio.h>
void displayMenu();
float addTen (float* number);
float divideByTwelve(float* number);
float subtractSixteen(float* number);
float multiplyByEleven(float* number);
int menu;
float number, finalNumber;
float* ptr_number;
int main (void)
{
float* ptr_number=NULL;
ptr_number=&number;
printf("Please enter a number : ");
scanf("%f", &number);
displayMenu();
scanf("%d", &menu);
do {
printf("\nEnter 5 to see final number\n");
scanf("%d",&menu);
switch (menu)
{
case 1: number=addTen(ptr_number);
break;
case 2: number=divideByTwelve(ptr_number);
break;
case 3: number=subtractSixteen(ptr_number);
break;
case 4: number=multiplyByEleven(ptr_number);
break;
}
printf("Your number is: %.2f \n", number);
}while(menu!=5);//close of the do-while loop
}
void displayMenu()
{
printf("How would you like to manipulate your number\n");
printf("1. Add 10\n");
printf("2. Divide by 12\n");
printf("3. Subtract 16\n");
printf("4. Multiply by 11\n");
}
float addTen (float* number)
{
ptr_number=ptr_number+10;
}
float divideByTwelve (float* number)
{
ptr_number=ptr_number/12;
}
float subtractSixteen(float* number)
{
ptr_number=ptr_number-16;
}
float multiplyByEleven(float* number)
{
ptr_number=ptr_number*11;
}
Here is one of your (many) issues.
You declare addTen as:
float addTen (float* number);
And call it as:
number=addTen(ptr_number);
Which clearly indicates it returns a value.
Now, do you see a return statement in the function definition?
float addTen (float* number)
{
ptr_number=ptr_number+10; // No return statement!!
}
Next up, all your function take a float* as a parameter, which you should read as "Pointer to a Floating point number". Note that a pointer is not the same thing as the value itself.
To get from a pointer to a value, you need to "de-reference" it:
float addTen (float* number)
{
*ptr_number = *ptr_number + 10;
}
The star (*) before each ptr_number means to go from the pointer back to the actual value.
Before, you were trying to add 10 to a pointer.
(that is valid, but not appropriate in the current context)
Now, with the *, you're adding 10 to the value stored at the pointed-to location.
Ultimately, here's my corrected version of your code
void displayMenu();
void addTen (float* number);
void divideByTwelve(float* number);
void subtractSixteen(float* number);
void multiplyByEleven(float* number);
int main (void)
{
int menu;
float number;
printf("Please enter a number : ");
scanf("%f", &number);
displayMenu();
scanf("%d", &menu);
do {
printf("\nEnter 5 to see final number\n");
scanf("%d",&menu);
switch (menu)
{
case 1: addTen(&number); break;
case 2: divideByTwelve(&number); break;
case 3: subtractSixteen(&number); break;
case 4: multiplyByEleven(&number); break;
}
printf("Your number is: %.2f \n", number);
}while(menu!=5);//close of the do-while loop
printf("Your FINAL number is: %.2f \n", number);
}
void displayMenu()
{
printf("How would you like to manipulate your number\n");
printf("1. Add 10\n");
printf("2. Divide by 12\n");
printf("3. Subtract 16\n");
printf("4. Multiply by 11\n");
}
void addTen (float* ptr_number)
{
*ptr_number = *ptr_number+10;
}
void divideByTwelve (float* ptr_number)
{
*ptr_number = *ptr_number/12;
}
void subtractSixteen(float* ptr_number)
{
*ptr_number = *ptr_number-16;
}
void multiplyByEleven(float* ptr_number)
{
*ptr_number = *ptr_number*11;
}
Because you are not returning float values from these functions. If you want to pass a pointer to your functions then you should do this as
float divideByTwelve (float *)
{
*ptr_number= *ptr_number/12;
return *ptr_number;
}
But you have declared number as global variable, so no need to pass a pointer to these function. Just pass the number itself`;
float divideByTwelve (float number)
{
return number / 12;
}

undeclared identifier in C

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.

Switch case missing in C program

#include<stdio.h>
#include<conio.h>
void inversion(void);
void ways(void);
void prime(void);
void power(void);
void fibonacci(void);
void main(void)
{
char choice;
printf("Enter a choice:\n 1.Perform inversion of digits \n 2.calculate all the ways that a positive number can be get by adding \n 3.Calculate prime numbers in a range\n 4.Calculate power of a number. \n 5.generate a particular numebr of fibonacci");
choice==getche();
switch(choice)
{
case 1: inversion();
break;
case 2: ways();
break;
case 3: prime();
break;
case 4:power();
break;
case 5:fibonacci();
break;
}//switch ends
//printf("Do you want to perform this once more? (Y?N):");
//}
//while (choice=='y'||choice=='Y');
getch();
}
void inversion(void)
{
clrscr();
int num,i,x,y;
printf("Enter a 4 digit number:");
scanf("%d",&num);
for(i=1;i<=4;i++)
{
x=num%10;
y=num/10;
printf("%d",x);
num=y;
}// for ends
getch();
}
void ways(void)
{ clrscr();
int num,i,j;
printf("Enter the number:");
scanf("%d",&num);
for(i=1;i<=num;i++)
for(j=1;j<=num;j++)
if(i+j==num)
printf("%d+%d=%d",i,j,num);
getch();
}
void prime(void)
{
int num1,num2,i;
printf("Enter a range separated by space:");
scanf("%d %d",num1,num2);
for(i=2;i<num2;i++)
{ if(num1%i!=0)//is prime
printf("%d",num1);
}//for ends
getch();
}//prime ends
void power(void)
{
int num,index,i,result=1;
printf("Enter the number and its index (eg.2^3)");
scanf("%d %d",&num,index);
for(i=1;i<=index;i++)
result=num*result;
printf("%d",result);
}//power function ends
void fibonacci(void)
{
int num,x=1,y=1,i,z;
printf("Enter the term you want to find in fibonacci series:");
scanf("%d",&num);
if (num==1)
printf("it is the 1st & 2nd term");
for(i=1;i<=num+2;i++)
{
z=x+y;
if (i==num)
printf("%d",z);
x=y;
y=z;
}
printf("%d");
}//fibonacci func ends
There are a lot of problems in your code:
# I would like to suggest you to use getchar() instead of getche() for a character input.
# Now you are using a charcter for numeric options which you could have avoided by defining your choice variable as integer:
int choice;
scanf (%d, &choice);
# also if you were using char choice;
you are having cases as:
case 1: inversion();
break;
while your choice variable was char
so you should have case like:
case '1': inversion();
anyways if you will define choice as int then your code should work fine.
or you can change your cases also with case values in single quotes.

Resources