Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

void function for scanf()

Status
Not open for further replies.

amilor

Programmer
Oct 17, 2011
1
SK
Hello, I would like to as for a help. The exercise is following:
Create a program for computing the area or perimeter of the rectangle. Write function
named read, that reads from standard input two real numbers and returns them
through the arguments of the function. Then write functions named area
and perimeter, which take dimensions of the rectangle as arguments and returns
area and perimeter of the rectangle, respectively. In the main fuction call read
function to read dimensions of the rectangle. When the user of the program enters
a character, the progra outputs area of the rectangle. When the user enters p character,
the program outputs its perimeter. For area and perimeter computation call respective
function through pointer to function.
Sample input:
3.5 4.75?
a?
Sample output:
16.625?

Now Ive been trying to run it for quite few minues but It seems to not work, can you help me out pls? here is my code:
 
this works
Code:
#include <stdio.h>
#include <stdlib.h>

void nacitaj(double *x, double *y)
{
	scanf("%lf %lf", x, y);
}

double obsah(double x, double y)
{
	return (x*y);
}

double obvod(double x, double y)
{
	return (2*x+2*y);
}

double (*p_f)(double x, double y);

int main()
{
	char c;
	double *x,*y;

	x=(double *) malloc(1*sizeof(double));
	y=(double *) malloc(1*sizeof(double));
	nacitaj(x, y);
    printf("*x = %lf, *y = %lf\n", *x, *y);

    printf("Which operation (o, s)?: ");
    // in gcc (MinGW) we have to use _flushall() or fflush(stdin)
    // before getchar()
    _flushall();
	c=getchar();
	printf("operation = '%c'\n", c);
	if (c=='o' || c=='O')
		p_f=obsah;
	else
		if (c=='S' || c=='s')
			p_f=obvod;
		else
		{
			printf("fail\n");
			return 1;
		}
	printf("%.3lf\n", p_f(*x,*y));

	free(x);
	free(y);

	return 0;
}
Output:
Code:
3.5 4.75
*x = 3.500000, *y = 4.750000
Which operation (o, s)?: o
operation = 'o'
16.625
You can do it a little bit simpler:
Code:
#include <stdio.h>
#include <stdlib.h>

void nacitaj(double *x, double *y)
{
	scanf("%lf %lf", x, y);
}

double obsah(double x, double y)
{
	return (x*y);
}

double obvod(double x, double y)
{
	return (2*x+2*y);
}

double (*p_f)(double x, double y);

int main()
{
	char c;
	double x, y;

	nacitaj(&x, &y);
    printf("x = %lf, y = %lf\n", x, y);

    printf("Which operation (o, s)?: ");
    // in gcc (MinGW) we have to use _flushall() or fflush(stdin)
    // before getchar()
    fflush(stdin);
	c=getchar();
	printf("operation = '%c'\n", c);
	if (c=='o' || c=='O')
		p_f=obsah;
	else
		if (c=='S' || c=='s')
			p_f=obvod;
		else
		{
			printf("fail\n");
			return 1;
		}
	printf("%.3lf\n", p_f(x, y));

	return 0;
}
Output:
Code:
3.5 4.75
x = 3.500000, y = 4.750000
Which operation (o, s)?: o
operation = 'o'
16.625
Tested with gcc (MinGW)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top