Smart questions
Smart people
 Find A ForumFind An Expert
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Remember Me

Are you a
Computer / IT professional?
Join Tek-Tips now!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Just copy and paste the

#### Feedback

"...(I) have been able to get my problems solved from past messages and also new posts that other users have responded to promptly..."

#### Geography

Where in the world do Tek-Tips members come from?

# void function for scanf()

 Forum Search FAQs Links Jobs Whitepapers MVPs
 amilor (Programmer) 17 Oct 11 17:31
 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  areaand perimeter, which take dimensions of the rectangle as arguments and returns area and perimeter of the rectangle, respectively.  In the main fuction call  readfunction 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:http://codeupload.com/?id=4389
 mikrom (Programmer) 18 Oct 11 15:36
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)

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!