Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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








0
Redevo7
Asked:
Redevo7
  • 5
  • 4
  • 2
  • +1
1 Solution
 
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
0
 
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'.



0
 
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". ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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).
0
 
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!
0
 
Redevo7Author Commented:
Anything????
0
 
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
0
 
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....
0
 
sunnycoderCommented:
>scanf("%f", &b);
%lf and not %f
0
 
sunnycoderCommented:
>scanf("%f", &b);
b is already a pointer

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

Please dont forget to close the question

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now