• C

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

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:

int  Menu( );
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
Please enter a selection
1      Calculate Area
2      Calculate Moment Inertia
3      Calculate Section Modulus
4      Quit
Selection: 1
Please enter Base: 2
Please enter Height: 2
Area = 4

Welcome to LumberCalc
Please enter a selection
1            Calculate Area
2            Calculate Moment Inertia
3            Calculate Section Modulus
4            Quit
Selection: 3
Please enter Base: 2
Please enter Height: 4
Section Modulus = 5.333
 
Welcome to LumberCalc
Please enter a selection
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>

int  Menu( );
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);
      choice = Menu( );
      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");
}

int Menu(int choice)
{
      printf("Please enter a selection:\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)
{
      printf("Please enter the base  \t");
      scanf("%d", &*b);
      printf("\n");
      printf("Please enter the height\t");
      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








Redevo7Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
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
brettmjohnsonCommented:
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'.



DineshJolaniaCommented:
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". ?
SolarWinds® VoIP and Network Quality Manager(VNQM)

WAN and VoIP monitoring tools that can help with troubleshooting via an intuitive web interface. Review quality of service data, including jitter, latency, packet loss, and MOS. Troubleshoot call performance and correlate call issues with WAN performance for Cisco and Avaya calls

brettmjohnsonCommented:
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).
Redevo7Author 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>

int  Menu( );
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);

      choice = Menu( );
      
      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);}

            Menu( );
      }


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

int Menu(int choice)
{
      printf("Please enter a selection:\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)
{
      printf("Please enter the base  \t");
      scanf("%d", &*b);
      printf("\n");
      printf("Please enter the height\t");
      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!
Redevo7Author Commented:
Anything????
sunnycoderCommented:
1. You need to change the value of choice in the loop
choice = menu(); and not menu()

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

Cheers!
sunnycoder
Redevo7Author Commented:
#include <stdio.h>

int  Menu( );
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);

      choice = Menu( );
      
      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);}

            choice = Menu( );
      }
      
      printf("Thank you\n\n");




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

int Menu(int choice)
{
      printf("Please enter a selection:\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)
{
      printf("Please enter the base  \t");
      scanf("%f", &b);
      printf("\n");
      printf("Please enter the height\t");
      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....
sunnycoderCommented:
>scanf("%f", &b);
%lf and not %f
sunnycoderCommented:
>scanf("%f", &b);
b is already a pointer

scanf("%lf", b);
Redevo7Author 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.
sunnycoderCommented:
Glad to help :)

Please dont forget to close the question

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.