Help for pointers

hi everyone just want some help from the experts..
what i am trying to do is to make a function outside the main that will ask the user for the values of 2 variables. i want to assign the same value to the 2 variables r and q in the main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "stdafx.h"
#include <math.h>

//func pro
void pc(double *r, double *q);



int main(void)
{
	double r,q;

	//fun call
 pc(&r,&q);
 
 printf("The value of r is %f\n",&r);
 printf("the value of q is %f\n",&q);
	return 0;
}

// func
void pc(double *x, double *y)
{
	printf("enter r\n");
	scanf("%lf",&x);
	printf("enter q\n");
	scanf("%lf",&y);
}


and i try to print the values in main so that it returns the same values
but its only prining the value of q..even in r..
thanks :)
1
2
3
4
5
6
7
void pc(double *x, double *y)
{
	printf("enter r\n");
	scanf("%lf",x);
	printf("enter q\n");
	scanf("%lf",y);
}
Also when you print the values:
1
2
 printf("The value of r is %f\n",r);
 printf("the value of q is %f\n",q);

@vlad from moscow
Thanks for quick reply :)

I already tried that it returns both values as 0.00000 :(
@peter87
Thanks ! mate ! you are GOD !
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "stdafx.h"
#include <math.h>
#include<stdio.h>
void reader(double,double,double,double);
double length(double,double,double,double);



int main(void)
{	double r1,r2;
	double q1,q2;
	while(r1 || r2 != -1)/*SENTINEL*/
	{
		reader(&r1,&q1,&r2,&q2);
		printf("%lf %lf %lf %lf",r1,r2,q1,q2);
	}
 printf("The value of r is %f\n",r1);
 printf("the value of q is %f\n",q1);
	return(0);
}










/*FUNCTIONS*/

/*Function to Read the Polar Coordinates*/
void reader(double *e, double *f, double *g, double *h)
{
	
	printf("Please Enter the Distance From the origin and the Angle from an arbitrary axis >");
	scanf("%lf %lf",e,f);
	printf("Please Enter the Distance From the origin and the Angle from an arbitrary axis for next point >");
	scanf("%lf %lf",g,h);

}

/*Function to Calculate the Length of a side of Polygon*/
double length(double r1,double q1,double r2,double q2)
{
	double d;
	d = sqrt((r1*r1)+(r2*r2)-(2*r1*r2*cos(q2-q1)));
	return(d);
}


i'm getting 2 errors
Error 7 error LNK2019: unresolved external symbol "void __cdecl reader(double,double,double,double)" (?reader@@YAXNNNN@Z) referenced in function _main C:\Users\Kamal\Desktop\c3173751\c3173751PA1\c3173751PA1\c3173751PA1.obj c3173751PA1
and
Error 8 error LNK1120: 1 unresolved externals C:\Users\Kamal\Desktop\c3173751\c3173751PA1\Debug\c3173751PA1.exe 1 1 c3173751PA1
Can anyone tell me why i get this error?
thanks
Last edited on
Lines 4 and 34 do not match.
the prototype on line 4 doesn't match the implementation on line 34
I lost it guys...can u explain a bit more?
thanks
Look at those two lines. Do you see differences? If yes, what are they?
@keskiverto
yes buddy their is a difference...i got the thing working if i put the 4th line as
void reader(double *r1, double *q1, double *r2, double *q2)

but i dont understand why is that... ?
was it that i have to define the pointer values too in the prototype ?
The types of the parameters in the prototype have to be the same as those in the actual function definition. That's the whole point of the prototype - so that the compiler knows what's supposed to be passed in to the actual function.
got it bro.!
another question...
what if i want to put multiple conditions in the /*while(condition)*/ condition part ?
like im trying to do this
while (r1 || r2 || q1 || q2 >= 0);
i want the while loop to stop when any of the 4 variables are negative?
i tried to run using this but its not working the loop is still going on
but if i put a single variable condition
r1 >= 0
it works !
i know its a stupid question...
but im learning :)
i wont ask the same again :D
Last edited on
while ( 0 < r1 && 0 < r2 && 0 < q1 && 0 < q2 )
http://www.cplusplus.com/doc/tutorial/operators/

The "Logical operators" chapter has an example, but doesn't explicitly explain why it is as it is. "Operator precedence" actually sets some more light to it.

while ( q1 || q2 >= 0 )
>= has higher precedence than ||. Therefore, one could write:
1
2
const bool cq2 = (q2 >= 0);
while ( q1 || cq2 )

Last edited on
got it ! thanks !
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "stdafx.h"
#include <math.h>
#include<stdio.h>


void reader(double *r1, double *q1, double *r2, double *q2); /*Prototype of Function responsible for taking the inputs from user*/
double length(double,double,double,double);                  /*Prototype of Function accountable for the calculation of length*/
double total(double,double);                                 /*Prototype of Function to keep on adding all the perimeters together*/
void format(double,double);                                  /*Protoype of format descryptor*/

int main(void)
{	double r1=0,r2=0; /*Declaration of First pair of Point where r1 is distance from origin and q1 is angle from an arbitary axis*/
	double q1=0,q2=0; /*Declaration of second pair of Point*/
	double d;         /*variable which will hold the ongoing perimeter for a particular points in loop*/
	double t=0;       /*Initialised variable which holds the total perimeter calculated in the program*/
   do {                 
        reader(&r1,&q1,&r2,&q2);  /*calling the function to get the inputs from user*/
		d = length(r1,q1,r2,q2);  /*Calling the function to calculate the length of side using the points given by user*/
		
		t = total(t,d);           /*Calling the function to add up the existing calculated perimeter to existing*/
		
		format(d,t);              /*Calling the function to print the values to 6 decimal places*/
		
	} while (0 < r1 && 0 < r2 && 0 < q1 && 0 < q2);/*SENTINEL - whenever any of the number is negative the loop will finish*/
	printf("INVALID INPUTS THANKS FOR USING THE PROGRAM");
}








/*FUNCTIONS*/

/*Function to Read the Polar Coordinates*/
void reader(double *r1, double *q1, double *r2, double *q2)
{
	
	printf("Please Enter the Distance From the origin and the Angle from an arbitrary axis >");
	scanf("%lf %lf",r1,q1);
	printf("Please Enter the Distance From the origin and the Angle from an arbitrary axis for next point >");
	scanf("%lf %lf",r2,q2);

}

/*Function to Calculate the Length of a side of Polygon*/
double length(double r1,double q1,double r2,double q2)
{
	double d1,d2;
	d1 = (r1*r1)+(r2*r2)-(2*r1*r2)*(cos((q2-q1)*(0.0175)));
	d2 = sqrt(d1);
	
	return(d2);
}

/*Function to calculate the total Perimeter of polygon*/
double total(double t,double d)
{
	double a;
	a = t + d;
	return(a);

}

/*Function to print values format descriptor*/
void format(double d, double t)
{
	printf("The length of side is %0.6f and the partially calculated perimeter is %0.6f\n\n\n",d,t);

}


This code is running perfectly ...
the only problem now is that whenever a negative value goes in...it exits the loop and finishes but it calculates the perimeter with negative values too and adds to the answer i dont want the negative value calculations to be added up.. ! im not getting how to modify the loop to first validate the values inputed and then do the calculations..
thanks for your time all !
Last edited on
Anybody ?
Topic archived. No new replies allowed.