Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

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

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

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()

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  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:
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)

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close