We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# New Program using pointers, functions and a simple condition. Having problem executing....

on
Medium Priority
219 Views
Hello All,
Currently trying to finish up one of my programs but I am stuck as to what is making it not work. Program is basically a program where the user is asked to choose from a list of options. The user then enters two inputs and the values are stored and manipulated using pointers.

Assigment is as followed:

A Lumber Company needs to create a table of engineering properties for its lumber inventory. Each property is calculated with two essential values base and height. The formulas are as follows:
o      Cross-sectional area: base * height
o      Moment of inertia: (base * height^3) / 12
o      Section modulus: (base * height^2)/6

Then using some funcions:

void get_Input(int *baseptr, int *hgtptr);
void Description(void);
double Cross_Sec_Area(int b,int h);
double Moment_Inertia(int b, int h);
double Section_Modulus(int b, int h);

Sample output:

Welcome to LumberCalc
1      Calculate Area
2      Calculate Moment Inertia
3      Calculate Section Modulus
4      Quit
Selection: 1
Area = 4

Welcome to LumberCalc
1            Calculate Area
2            Calculate Moment Inertia
3            Calculate Section Modulus
4            Quit
Selection: 3
Section Modulus = 5.333

Welcome to LumberCalc
1            Calculate Area
2            Calculate Moment Inertia
3            Calculate Section Modulus
4            Quit
Selection: 4
Thank you for visiting LumberCalc
Have a nice day!
----------------------------------------------------------------------------------------

Currently my program looks like this:

#include <stdio.h>

void get_Input(int *baseptr, int *hgtptr);
void Description(void);
double Cross_Sec_Area(int b,int h);
double Moment_Inertia(int b, int h);
double Section_Modulus(int b, int h);

int main(void)
{
int choice;
double b = 0,h = 0,area =0, mo_inert = 0,sect_mod = 0;
void Description(void);
get_Input(&b, &h);
area = Cross_Sec_Area(b, h);
mo_inert = Moment_Inertia(b, h);
sect_mod = Section_Modulus(b, h);

while(choice != 4)
{
if(choice == 1)
{printf("Area = %lf", area);}

else if(choice == 2)
{printf("Moment of Intertia = %lf", mo_inert);}

else if(choice == 3)
{printf("Section Modulus = %lf", sect_mod);}
}

return(0);
}
void Description(void)
{
printf("Welcome to Lumber Engineering Services.\n");
}

{
printf("1\tCalculate Area\n");
printf("2\tCalculate Moment of Intertia\n");
printf("3\tCalculate Section Modulus\n");
printf("4\tQuit\n");
scanf("%d", &choice);
return(choice);
}

void get_Input(int *b, int *h)
{
scanf("%d", &*b);
printf("\n");
scanf("%d", &*h);
printf("\n");
}

double Cross_Sec_Area(int b,int h)
{
double area;
area = b*h;
return(area);

}
double Moment_Inertia(int b, int h)
{
double m_inertia;
m_inertia = (b*h*h*h) / 12;
return(m_inertia);
}

double Section_Modulus(int b, int h)
{
double sect_mod;
sect_mod = (b*h*h)/6;
return(sect_mod);
}
------------------------------------------

When I execute the syntax I just get a screen with alot of numbers going past me. The matrix has corrupted my computer!!! (not really)

Again greatly apprecaite any suggestions and help.

Thanks
Redevo7

Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Top Expert 2006

Commented:
Hi Redevo7,

1. Your function declaration and the actual arguments you are passing are different .. e.g.
void get_Input(int *b, int *h)
while you pass it doubles. This is not good. Type of parameters must match!!!

2. The infinite loop you are getting is due to this
while(choice != 4)
{
if(choice == 1)
{printf("Area = %lf", area);}

else if(choice == 2)
{printf("Moment of Intertia = %lf", mo_inert);}

else if(choice == 3)
{printf("Section Modulus = %lf", sect_mod);}
}

choice does not change in this loop and you get stuck with the same value of choice forever. place a call to menu() near the end of the loop and assign that value to choice

Cheers!
sunnycoder
Top Expert 2005

Commented:
Your primary problem, is you defined b and h as doubles, but pass them around as int.
The int and double types are different sizes, so when you pass the addresses of doubles to get_Input(), which expects addresses of ints, things will get confused.  This shows up as compiler warnings (passing the incorrect type, and implicit casts with loss of precision).  Best to pay attention to such warnings.

Oh, and '&*b' is just 'b'.

Commented:
I see  point no 2 mentioned  by Sunny coder is the potentail problem.
Reagarding loss of precision I disagree(wrt to code here ) . Why ?

void get_Input(int *baseptr, int *hgtptr);
Even you pass double  like
get_Input(&b, &h);

But you are still reading them as ints.
How can there be a loss of precision when you are reading a int  into "double". ?
Top Expert 2005

Commented:
The loss of precision happens when you convert the double to int passing b and h to all the other functions (all prototyped to receive ints).

Commented:
Interesting... I basically just changed everything to a double to make things less complicated as I truely hate any error/warnings messages. As I debug the program everything goes correct but when I attempt to do a simple example, it seems as if the pointers aren't working. here is my updated code.

--------------------------

#include <stdio.h>

void get_Input(double *baseptr, double *hgtptr);
void Description(void);
double Cross_Sec_Area(double b,double h);
double Moment_Inertia(double b, double h);
double Section_Modulus(double b, double h);

int main(void)
{
int choice;
double b = 0,h = 0,area =0, mo_inert = 0,sect_mod = 0;
void Description(void);
get_Input(&b, &h);
area = Cross_Sec_Area(b, h);
mo_inert = Moment_Inertia(b, h);
sect_mod = Section_Modulus(b, h);

while(choice != 4)
{
if(choice == 1)
{printf("Area = %lf", area);}

else if(choice == 2)
{printf("Moment of Intertia = %lf", mo_inert);}

else if(choice == 3)
{printf("Section Modulus = %lf", sect_mod);}

}

return(0);
}
void Description(void)
{
printf("Welcome to Lumber Engineering Services.\n");
}

{
printf("1\tCalculate Area\n");
printf("2\tCalculate Moment of Intertia\n");
printf("3\tCalculate Section Modulus\n");
printf("4\tQuit\n");
scanf("%d", &choice);
return(choice);
}

void get_Input(double *b, double *h)
{
scanf("%d", &*b);
printf("\n");
scanf("%d", &*h);
printf("\n");
}

double Cross_Sec_Area(double b,double h)
{
double area;
area = b*h;
return(area);

}
double Moment_Inertia(double b, double h)
{
double m_inertia;
m_inertia = (b*h*h*h) / 12;
return(m_inertia);
}

double Section_Modulus(double b, double h)
{
double sect_mod;
sect_mod = (b*h*h)/6;
return(sect_mod);
}

-------------------------

Thanks Again!

Commented:
Anything????
CERTIFIED EXPERT
Top Expert 2006

Commented:
1. You need to change the value of choice in the loop

2. Doubles are read with format string %lf and not %d ... Your format string should match your data types!!!

Cheers!
sunnycoder

Commented:
#include <stdio.h>

void get_Input(double *baseptr, double *hgtptr);
void Description(void);
double Cross_Sec_Area(double b,double h);
double Moment_Inertia(double b, double h);
double Section_Modulus(double b, double h);

int main(void)
{
int choice;
double b = 0,h = 0,area =0, mo_inert = 0,sect_mod = 0;
Description( );
get_Input(&b, &h);
area = Cross_Sec_Area(b, h);
mo_inert = Moment_Inertia(b, h);
sect_mod = Section_Modulus(b, h);

while(choice != 4)
{
if(choice == 1)
{printf("Area = %2.1lf\n\n", area);}

else if(choice == 2)
{printf("Moment of Intertia = %2.1lf\n\n", mo_inert);}

else if(choice == 3)
{printf("Section Modulus = %2.1lf\n\n", sect_mod);}

}

printf("Thank you\n\n");

return(0);
}
void Description(void)
{
printf("Welcome to Lumber Engineering Services.\n");
}

{
printf("1\tCalculate Area\n");
printf("2\tCalculate Moment of Intertia\n");
printf("3\tCalculate Section Modulus\n");
printf("4\tQuit\n");
scanf("%d", &choice);
return(choice);
}

void get_Input(double *b, double *h)
{
scanf("%f", &b);
printf("\n");
scanf("%f", &h);
printf("\n");
}

double Cross_Sec_Area(double b,double h)
{
double area;
area = b*h;
return(area);

}
double Moment_Inertia(double b, double h)
{
double m_inertia;
m_inertia = (b*h*h*h) / 12;
return(m_inertia);
}

double Section_Modulus(double b, double h)
{
double sect_mod;
sect_mod = (b*h*h)/6;
return(sect_mod);
}

-----------------------------------------------

For some reason I cant get my pointers to work now....
CERTIFIED EXPERT
Top Expert 2006

Commented:
>scanf("%f", &b);
%lf and not %f
CERTIFIED EXPERT
Top Expert 2006

Commented:
>scanf("%f", &b);

scanf("%lf", b);

Commented:
Ohhh I was never told that. Program works now!!! Greatly appreciate it and I will definately refer this site to friends as once again you guys really do help people and in an efficient manner i must say.
CERTIFIED EXPERT
Top Expert 2006
Commented:

Please dont forget to close the question

http://www.experts-exchange.com/help.jsp#hs5

Not the solution you were looking for? Getting a personalized solution is easy.

##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile