3 Replies - 1161 Views - Last Post: 08 November 2008 - 06:53 AM Rate Topic: -----

#1 jovankamcev24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-November 08

need help creating a code using the switch function as a variable.

Posted 08 November 2008 - 12:03 AM

i need help writing a program that takes 2 integers and either a for add s for subtract d for divide and m for multiply. the main program stores the input in the variables num1 num2 and operator. the main program then checks operator . each of the functions add, subtract, divide, or multiply invoke a function called print_results, which prints the results of the arithmetic operation. use a switch statement construction in the program.
here is what i have so far but im pretty lost.

 #include <stdio.h>
#include <math.h>
float print_results(float num1, float num2, char x);
float num1, num2;
char x;
float total;
void main()
{
	printf("Input 2 numbers and either a, s, d, or m.\n");
	scanf("%f %f %f", &num1, &num2, &x);
	print_results(num1, num2, x);
}
float print_results(float num1, float num2, char x)
{
	switch(x)
	{
	case 'a': 
		total=num1+num2;
		printf("total is %f.\n", total);
		break;
	case 's':
		total=num1-num2;
		printf("total is %f.\n", total);
		break;
	case 'm':
		total=num1*num2;
		printf("total is %f.\n", total);
		break;
	case 'd':
		total=num1/num2;
		printf("total is %f.\n", total);
		break;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: need help creating a code using the switch function as a variable.

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: need help creating a code using the switch function as a variable.

Posted 08 November 2008 - 12:25 AM

Your code has only 2 show stoppers:
1. print_results() has a type of float but does not return anything. Change to void.
2 the scanf() format has 3 %f specifiers. Change the last one to %c to match the char input.

Otherwise it works. I added system("PAUSE"); after the call to print_results() so I
could see the results. :)
...
void print_results(float num1, float num2, char x);
...
	scanf("%f %f %c", &num1, &num2, &x);
	print_results(num1, num2, x);
}
void print_results(float num1, float num2, char x)
{
	switch(x)
	{
	case 'a': 
...


Was This Post Helpful? 0
  • +
  • -

#3 jovankamcev24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-November 08

Re: need help creating a code using the switch function as a variable.

Posted 08 November 2008 - 01:11 AM

thanks a lot for the help but can you please explain why you changed it to void. i dont really understand that. thanks a lot
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: need help creating a code using the switch function as a variable.

Posted 08 November 2008 - 06:53 AM

If a function does not return a value to the caller, it should be declared as returning void (i.e., nothing at all).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1