Hello everyone,

This is my first time here. I happen to run into this site as I was looking for solutions for some c program I have to write for my programming class. Anyway I'm running into issues that I just don't see any reason for why they are happening. The code is correct, but they just don't work correctly. There is a load of them, but for now I'll stick to one.

In this case the one I want to talk about is the scanf function. It never works properly for me. So here is a little test i created to explain it...

double input ()
{
    double x,y,z,rad;
    printf("Please input data for one of the spheres... \n");
    printf("Radius:");
    scanf("%lf\n",&rad);
    printf("X value:");
    scanf("%lf\n",&x);
    printf("Y value:");
    scanf("%lf\n",&y);
    printf("Z value:");
    scanf("%lf\n",&z);
    printf("%f, %f, %f, %f",x,y,z,rad);

Input:

Raduis: 9
5         <<<<< (notice how it just asks me to input something in the middle of nowhere)
X value:1
Y value:2
Z value: 3

Output:

5,1,2,9 <<<<<(5 became x and everything else was shifted down and no 3!!!!!)
double input ()
{
    double x  = 0, y  = 0, z = 0, rad = 0 ;
    printf("Please input data for one of the spheres... ");
    printf("\nRadius:");
    scanf("%lf",&rad);
    printf("X value:");
    scanf("%lf",&x);
    printf("Y value:");
    scanf("%lf",&y);
    printf("Z value:");
    scanf("%lf",&z);
    printf("X = %f, Y = %f, Z = %f, Radius = %f  line",x,y,z,rad);
}

The problem arises bcoz of the "\n" which you place in your scanf stmts. It is a very complex function which is better left off unless its really necessary to use it ( and such condition never arises)

You are better off using something like this:

char buffer [BUFSIZ] = '\0' ;
printf ("Enter a double value: ") ;
fgets (buffer, BUFSIZ, stdin) ;
double value = strtof ( buffer ) ;

There is no way to return multiple values from a C function. You can get around that by returning an array or manipulating the values by passing them to the function using reference usign pointers.

Try something like this for reference parameter passing:

void input (double* x, double* y, double* z, double* rad)
{

    printf("Please input data for one of the spheres... ");
    printf("X value:");
    scanf("%lf",x);
    printf("Y value:");
    scanf("%lf",y);
    printf("Z value:");
    scanf("%lf",z);
    printf("\nRadius:");
    scanf("%lf",rad);
}

int main()
{
    double x  = 0, y  = 0, z = 0, rad = 0 ;
    input (&x, &y, &z, &rad) ;
    printf("X = %f, Y = %f, Z = %f, Radius = %f  line",x,y,z,rad);
    return 0 ;
}

nope not doing the job...

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

double input ()
{
    double x,y,z,rad;
    printf("Please input data for one of the spheres... \n");
    printf("\nRadius:");
    scanf("%lf",&rad);
    printf("\nX value:");
    scanf("%lf",&x);
    printf("\nY value:");
    scanf("%lf",&y);
    printf("\nZ value:");
    scanf("%lf",&z);
    printf("ccc %f, %f, %f, %f, line",x,y,z,rad);
    return x,y,z,rad;
}
double diff (double a, double b)
{
    return abs(a-b);
}
double PT2 (double a, double b)
{
    return sqrt((a*a)+(b*b));
}
double PT3 (double x, double y, double z)
{
    return PT2(x,PT2(y, z ));
}
int main ()
{
    double x=0;
    double y=0;
    double z=0;
    double x1=0;
    double y1=0;
    double z1=0;
    double rad=0;
    double rad1=0;
    
    input(&x,&y,&z,&rad);
    input(&x1,&y1,&z1,&rad1);

    printf("dd %f\n",x);
    printf("%f %f %f %f",x,y,z,rad);
    if (.0000000001<PT3(diff(x,x1),diff(y,y1),diff(z,z1))-(abs(rad)+abs(rad1)))
    {
        printf("Objects are not colliding.");
    }
    else
    {
        printf("Objects are colliding.");
    }
    return 0;

}

There is no way to return multiple values from a C function. You can get around that by returning an array or manipulating the values by passing them to the function using reference usign pointers.

If you have already decided not to pay attention to what we write then what is the point in helping you out ???

Read my previous post which mentions teh details of how to make the funciton return multiple values by means of passign by reference using pointers. Incorporate the changes which i have made in your program and it will surely accept the data the way you want.

Thanks for letting me know. I think I'll just use a seperate function to do the job

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.