8 Replies - 282 Views - Last Post: 03 March 2018 - 12:29 PM Rate Topic: -----

#1 MattC231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 06-April 17

expected 'int function()' but found 'double function(doubl

Posted 02 March 2018 - 02:34 AM

the error is C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(141): error #2120: Redeclaration of 'findAverage', previously declared at C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(43); expected 'int function()' but found 'double function(double, double)'.

I'm still learning, can someone explain to me why i get this?

#include <stdio.h>

	int displayMenu();
	int findSum(int a, int B)/>/>;
	int findProduct(int a, int B)/>/>;
	int findRemainder(int a, int B)/>/>;
	double findAverege(double a, double B)/>/>;
	int calcChange(int numNick, int numDime, int numQuart);
	int calcDistance(int time);
	
	#define QUARTER 25
	#define DIME 10
	#define NICKEL 5
	#define ACCELERATION 32

int main()
{
	int choice, sum, product, remainder, num1, num2, cont, numNick, numDime, numQuart, totChange, time, distance;
	double avgNum1, avgNum2, average;
	
	choice = displayMenu();
	
	if (choice == 1)
	{
		cont = 0;
		
		do
		{
			printf("/n/nEnter the first number to calculate the sum, product, and the remainder from division: ");
			scanf("%d",&num1);
			printf("/n/nEnter the first number to calculate the average: ");
			scanf("%lf",&avgNum1);
			printf("/nEnter a second number to calculate the sum, product, and the remainder from division: ");
			scanf("%d",&num2);
			printf("/n/nEnter a second number to calculate the average: ");
			scanf("%lf",&avgNum2);
		
			sum = findSum(num1, num2);
			product = findProduct(num1, num2);
			remainder = findRemainder(num1, num2);
			average = findAverage(avgNum1, avgNum2);
		
			printf("%d + %d = %d/n",num1, num2, sum);
			printf("%d * %d = %d/n",num1, num2, product);
			printf("%d % %d = %d/n",num1, num2, remainder);
			printf("(%d + %d) / 2 = /n",num1, num2, average);
		
			printf("Would you like to input new numbers?/n1 for Y, 2 for N/n");
			scanf("%d",&cont);
			system("pause/n/n/n");
		}while(cont == 1);
	}
	else if (choice == 2)
	{
		cont = 0;
		
		do
		{
			printf("/n/nEnter number of nickels: ");
			scanf("%d",numNick);
			printf("/nEnter number of dimes: ");
			scanf("%d",numDime);
			printf("/nEnter number of quarters: ");
			scanf("%d",numQuart);
		
			totChange = calcChange(numNick, numDime, numQuart);
			
			printf("/nYou have %d cents./n/n",totChange);
			
			printf("Would you like to input new numbers of coins?/n1 for Y, 2 for N/n");
			scanf("%d",&cont);
			system("pause/n/n/n");
		}while(cont == 1);
	}
	else if (choice == 3)
	{
		cont = 0;
		
		do
		{
			
			printf("/n/nEnter how long, in seconds, that the object is falling: ");
			scanf("%d",&time);
			
			distance = calcDistance(time);
		
			printf("/nThe distance of an object falling for %d seconds is %d feet./n/n",time, distance);
			
			printf("Would you like to input a new time?/n1 for Y, 2 for N/n");
			scanf("%d",&cont);
			system("pause/n/n/n");
		}while(cont == 1);
	}
	else if (choice == 4)
	{
		printf("/n/nThank You and Have A Great Day!!!/n");
		system("pause/n");
		system("cls");
	}

	return 0;	
}

int displayMenu()
{
	int choice;
	
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/n");
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/n");
	printf("@@@@ 1) Find sum, product, remainder, and average of two numbers.    @@@@/n");
	printf("@@@@ 2) Calculate total change.                                      @@@@/n");
	printf("@@@@ 3) Calculate free fall distance of a dropped object.            @@@@/n");
	printf("@@@@ 4) Exit                                                         @@@@/n");
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/n");
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/n/n");
	
	printf("Enter your choice: ");
	scanf("%d",&choice);
	
	return choice;
}

int findSum(int a, int B)/>/>
{	
	return a + b;
}

int findProduct(int a, int B)/>/>
{
	return a * b;
}

int findRemainder(int a, int B)/>/>
{
	return a % b;
}

double findAverage(double a, double B)/>/>
{
	return (a + B)/>/> / 2;
}

int calcChange(int numNick, int numDime, int numQuart)
{	
	return (numNick * NICKEL) + (numDime * DIME) + (numQuart * QUARTER);
}

int calcDistance(int time)
{
	return (ACCELERATION / 2) * (time * time);
}




btw, 'B' is supposed to be 'b' and '/>' is not supposed to be there. when i put the code tags, they appeared like that

This post has been edited by MattC231: 02 March 2018 - 02:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: expected 'int function()' but found 'double function(doubl

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2620
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: expected 'int function()' but found 'double function(doubl

Posted 02 March 2018 - 03:45 AM

The error is telling you that you declared findAverage as int findAverage(int, int) on line 43 and then tried to redeclare it with doubles on line 141.

Declaring it with doubles was what you wanted, so where does that supposed int declaration come from? Let's look at line 43. Turns out that line is a function call, not a declaration. So what gives? Well, old versions of the C languages (and many compilers still allow this with only a warning) allowed you to call functions without declaring them, in which case the call itself would serve as a declaration. In that case the implicitly-declared functions would take only ints and return int. So that's where the int-declaration comes from. Presumably you also have a warning on that line telling you that this is happening (if not, you should increase your warnings level).

But wait, didn't you declare the function at the beginning of the file? Well, you tried, but you misspelled its name.
Was This Post Helpful? 0
  • +
  • -

#3 MattC231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 06-April 17

Re: expected 'int function()' but found 'double function(doubl

Posted 02 March 2018 - 06:16 AM

oh.......thats embarrassing...........thanks for the help though
Was This Post Helpful? 0
  • +
  • -

#4 MattC231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 06-April 17

Re: expected 'int function()' but found 'double function(doubl

Posted 02 March 2018 - 06:54 AM

This is the revised code:

#include <stdio.h>

	int displayMenu();
	int findSum(int a, int B)/>;
	int findProduct(int a, int B)/>;
	int findRemainder(int a, int B)/>;
	double findAverage(double a, double B)/>;
	int calcChange(int numNick, int numDime, int numQuart);
	int calcDistance(int time);
	
	#define QUARTER 25
	#define DIME 10
	#define NICKEL 5
	#define ACCELERATION 32

int main()
{
	int choice = 0, sum = 0, product = 0, remainder = 0, num1 = 0, num2 = 0, cont = 0, numNick = 0, numDime = 0, numQuart = 0, totChange = 0, time = 0, distance = 0;
	double avgNum1 = 0.0, avgNum2 = 0.0, average = 0.0;
	
	choice = displayMenu();
	
	if (choice == 1)
	{
		cont = 0;
		
		do
		{
			printf("\n\nEnter the first number to calculate the sum, product, and the remainder from division: ");
			scanf("%d",&num1);
			printf("\n\nEnter the first number to calculate the average: ");
			scanf("%lf",&avgNum1);
			printf("\n\nEnter a second number to calculate the sum, product, and the remainder from division: ");
			scanf("%d",&num2);
			printf("\n\nEnter a second number to calculate the average: ");
			scanf("%lf",&avgNum2);
		
			sum = findSum(num1, num2);
			product = findProduct(num1, num2);
			remainder = findRemainder(num1, num2);
			average = findAverage(avgNum1, avgNum2);
		
			printf("%d + %d = %d\n",num1, num2, sum);
			printf("%d * %d = %d\n",num1, num2, product);
			printf("%d %% %d = %d\n",num1, num2, remainder);
			printf("(%lf + %lf) / 2 = %lf\n",avgNum1, avgNum2, average);
		
			printf("Would you like to input new numbers?\n1 for Y, 2 for N\n");
			scanf("%d",&cont);
		}while(cont == 1);
	}
	else if (choice == 2)
	{
		cont = 0;
		
		do
		{
			printf("\n\nEnter number of nickels: ");
			scanf("%d",numNick);
			printf("\nEnter number of dimes: ");
			scanf("%d",numDime);
			printf("\nEnter number of quarters: ");
			scanf("%d",numQuart);
		
			totChange = calcChange(numNick, numDime, numQuart);
			
			printf("\nYou have %d cents.\n\n",totChange);
			
			printf("Would you like to input new numbers of coins?\n1 for Y, 2 for N\n");
			scanf("%d",&cont);
		}while(cont == 1);
	}
	else if (choice == 3)
	{
		cont = 0;
		
		do
		{
			
			printf("\n\nEnter how long, in seconds, that the object is falling: ");
			scanf("%d",&time);
			
			distance = calcDistance(time);
		
			printf("\nThe distance of an object falling for %d seconds is %d feet.\n\n",time, distance);
			
			printf("Would you like to input a new time?\n1 for Y, 2 for N\n");
			scanf("%d",&cont);
		}while(cont == 1);
	}
	else if (choice == 4)
	{
		printf("\n\nThank You and Have A Great Day!!!\n");
		system("pause\n");
		system("cls");
	}

	return 0;	
}

int displayMenu()
{
	int choice;
	
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
	printf("@@@@ 1) Find sum, product, remainder, and average of two numbers.    @@@@\n");
	printf("@@@@ 2) Calculate total change.                                      @@@@\n");
	printf("@@@@ 3) Calculate free fall distance of a dropped object.            @@@@\n");
	printf("@@@@ 4) Exit                                                         @@@@\n");
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n");
	
	printf("Enter your choice: ");
	scanf("%d",&choice);
	
	return choice;
}

int findSum(int a, int B)/>
{	
	return a + b;
}

int findProduct(int a, int B)/>
{
	return a * b;
}

int findRemainder(int a, int B)/>
{
	return a % b;
}

double findAverage(double a, double B)/>
{
	return (a + B)/> / 2;
}

int calcChange(int numNick, int numDime, int numQuart)
{	
	return (numNick * NICKEL) + (numDime * DIME) + (numQuart * QUARTER);
}

int calcDistance(int time)
{
	return (ACCELERATION / 2) * (time * time);
}



When I execute after 2 for the second option to input number of coins, after I input a number for nickels and hit enter, I get the message in cmd:
"CRT: unhandled exception (main) -- terminating
*** Process returned 255 ***"

Is it a problem with my code, or is the fault probably with the compiler I'm using: Pelles C IDE?
Can you recommend me a compiler to use?
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2620
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: expected 'int function()' but found 'double function(doubl

Posted 02 March 2018 - 07:11 AM

I don't know Pelles, but hopefully it has a debugger that you can use to find out on which line the error happens and what the stack trace is. I'd expect the problem is with your code, not the compiler.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5488
  • View blog
  • Posts: 17,065
  • Joined: 25-December 09

Re: expected 'int function()' but found 'double function(doubl

Posted 02 March 2018 - 07:16 AM

Quote

Is it a problem with my code,

Yes.

Quote

or is the fault probably with the compiler I'm using: Pelles C IDE?

No. Your compiler should be warning you about several issues. You may need to increase the warning levels, check the documentation for your compiler/IDE for more information.

main.c||In function ‘main’:|
main.c|59|warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]|
main.c|61|warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]|
main.c|63|warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]|
main.c|94|warning: implicit declaration of function ‘system’ [-Wimplicit-function-declaration]|


Using the improper format specifier for the type of variable with scanf() quite often leads to a crash.

Edit: Also with modern C it is usually considered a poor practice to declare all of your variables in one big glob at the beginning of the function, instead declare the variables closer to first use and be sure to initialize variables when you declare them.


Jim

This post has been edited by jimblumberg: 02 March 2018 - 07:19 AM

Was This Post Helpful? 0
  • +
  • -

#7 MattC231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 06-April 17

Re: expected 'int function()' but found 'double function(doubl

Posted 02 March 2018 - 03:17 PM

these are the warnings i'm getting

C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(3): warning #2027: Missing prototype for 'displayMenu', with type 'int function()'.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(17): warning #2117: Old-style function definition for 'main'.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(44): warning #2216: The return value from 'scanf' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(46): warning #2216: The return value from 'scanf' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(48): warning #2216: The return value from 'scanf' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(50): warning #2216: The return value from 'scanf' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(63): warning #2216: The return value from 'scanf' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(73): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'int *' but found 'int'.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(73): warning #2216: The return value from 'scanf' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(75): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'int *' but found 'int'.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(75): warning #2216: The return value from 'scanf' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(77): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'int *' but found 'int'.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(77): warning #2216: The return value from 'scanf' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(84): warning #2216: The return value from 'scanf' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(95): warning #2216: The return value from 'scanf' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(102): warning #2216: The return value from 'scanf' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(108): warning #2018: Undeclared function 'system' (did you mean '(no name)'?); assuming 'extern' returning 'int'.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(108): warning #2216: The return value from 'system' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(109): warning #2216: The return value from 'system' is never used.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(116): warning #2117: Old-style function definition for 'displayMenu'.
C:\Users\Mathew\Desktop\CompStuff\CProg\Project1.c(129): warning #2216: The return value from 'scanf' is never used.

#include <stdio.h>

	int displayMenu();
	int findSum(int a, int B)/>/>;
	int findProduct(int a, int B)/>/>;
	int findRemainder(int a, int B)/>/>;
	double findAverage(double a, double B)/>/>;
	int calcChange(int numNick, int numDime, int numQuart);
	int calcDistance(int time);
	
	#define QUARTER 25
	#define DIME 10
	#define NICKEL 5
	#define ACCELERATION 32

int main()
{
	int choice = 0;
	int sum = 0;
	int product = 0;
	int remainder = 0;
	int num1 = 0;
	int num2 = 0;
	int cont = 0;
	int numNick = 0;
	int numDime = 0;
	int numQuart = 0;
	int totChange = 0;
	int time = 0;
	int distance = 0;
	double avgNum1 = 0.0;
	double avgNum2 = 0.0;
	double average = 0.0;
	
	choice = displayMenu();
	
	if (choice == 1)
	{
		cont = 0;
		
		do
		{
			printf("\n\nEnter the first number to calculate the sum, product, and the remainder from division: ");
			scanf("%d",&num1);
			printf("\n\nEnter the first number to calculate the average: ");
			scanf("%lf",&avgNum1);
			printf("\n\nEnter a second number to calculate the sum, product, and the remainder from division: ");
			scanf("%d",&num2);
			printf("\n\nEnter a second number to calculate the average: ");
			scanf("%lf",&avgNum2);
		
			sum = findSum(num1, num2);
			product = findProduct(num1, num2);
			remainder = findRemainder(num1, num2);
			average = findAverage(avgNum1, avgNum2);
		
			printf("%d + %d = %d\n",num1, num2, sum);
			printf("%d * %d = %d\n",num1, num2, product);
			printf("%d %% %d = %d\n",num1, num2, remainder);
			printf("(%lf + %lf) / 2 = %lf\n",avgNum1, avgNum2, average);
		
			printf("Would you like to input new numbers?\n1 for Y, 2 for N\n");
			scanf("%d",&cont);
		}while(cont == 1);
	}
	else if (choice == 2)
	{
		cont = 0;
		
		do
		{
			printf("\n\nEnter number of nickels: ");
			scanf("%d",numNick);
			printf("\nEnter number of dimes: ");
			scanf("%d",numDime);
			printf("\nEnter number of quarters: ");
			scanf("%d",numQuart);
		
			totChange = calcChange(numNick, numDime, numQuart);
			
			printf("\nYou have %d cents.\n\n",totChange);
			
			printf("Would you like to input new numbers of coins?\n1 for Y, 2 for N\n");
			scanf("%d",&cont);
		}while(cont == 1);
	}
	else if (choice == 3)
	{
		cont = 0;
		
		do
		{
			
			printf("\n\nEnter how long, in seconds, that the object is falling: ");
			scanf("%d",&time);
			
			distance = calcDistance(time);
		
			printf("\nThe distance of an object falling for %d seconds is %d feet.\n\n",time, distance);
			
			printf("Would you like to input a new time?\n1 for Y, 2 for N\n");
			scanf("%d",&cont);
		}while(cont == 1);
	}
	else if (choice == 4)
	{
		printf("\n\nThank You and Have A Great Day!!!\n");
		system("pause\n");
		system("cls");
	}

	return 0;	
}

int displayMenu()
{
	int choice;
	
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
	printf("@@@@ 1) Find sum, product, remainder, and average of two numbers.    @@@@\n");
	printf("@@@@ 2) Calculate total change.                                      @@@@\n");
	printf("@@@@ 3) Calculate free fall distance of a dropped object.            @@@@\n");
	printf("@@@@ 4) Exit                                                         @@@@\n");
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
	printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n");
	
	printf("Enter your choice: ");
	scanf("%d",&choice);
	
	return choice;
}

int findSum(int a, int B)/>/>
{	
	return a + b;
}

int findProduct(int a, int B)/>/>
{
	return a * b;
}

int findRemainder(int a, int B)/>/>
{
	return a % b;
}

double findAverage(double a, double B)/>/>
{
	return (a + B)/>/> / 2;
}

int calcChange(int numNick, int numDime, int numQuart)
{	
	return (numNick * NICKEL) + (numDime * DIME) + (numQuart * QUARTER);
}

int calcDistance(int time)
{
	return (ACCELERATION / 2) * (time * time);
}




can you give me insight into what the warnings mean?

This post has been edited by MattC231: 02 March 2018 - 03:20 PM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6220
  • View blog
  • Posts: 21,469
  • Joined: 05-May 12

Re: expected 'int function()' but found 'double function(doubl

Posted 03 March 2018 - 07:18 AM

Most of the warnings are self explanatory.

The ones regarding old style maybe due to those functions not taking any parameters. You will want to put void between the parenthesis to tell the compiler that you really want no parameters, as opposed to the old C style where parameters are specified in the lines between the closing parenthesis, and the opening curly brace.

I am at a loss to explain why you are getting the first warning about displayMenu.
Was This Post Helpful? 0
  • +
  • -

#9 MattC231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 06-April 17

Re: expected 'int function()' but found 'double function(doubl

Posted 03 March 2018 - 12:29 PM

ok thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1