Thread: How to connect C with MySql

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Make it like this.
    Code:
     
      // Enter the value of Radius for calculation of above.
      printf("Please enter the value of circle radius \n");
      scanf("%f", &radius);
    
      // These are the lines that need to move.
      diameter = radius * 2;
      circumference = diameter * pie;
      radius = diameter / 2;
      pie = circumference / diameter;  // before area
      area = radius * pie * radius;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by mba_110 View Post
    Not clear you are telling it in a very limited words, move what from what ? input data from what those calculations ?

    Not clear to understand anything, please write in details what actually required to do.

    here i want all values of pie,diameter,circumference and area based on radius i have provided, so once i input radius it should show four separate lines showing for category result.
    C doesn't allow code like this

    Code:
    area = width * height;
    height = 4;
    width = 5;
    // expect area to be 20
    There is no way of setting up an equation then expecting the result to be adjusted when the right hand side is set.

    However if you do this

    Code:
    height = 4;
    width = 5;
    area = width * height;
    // area is now 20
    it will work as you would expect.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #18
    Registered User
    Join Date
    Aug 2020
    Posts
    10
    Quote Originally Posted by Salem View Post
    Make it like this.
    Code:
     
      // Enter the value of Radius for calculation of above.
      printf("Please enter the value of circle radius \n");
      scanf("%f", &radius);
    
      // These are the lines that need to move.
      diameter = radius * 2;
      circumference = diameter * pie;
      radius = diameter / 2;
      pie = circumference / diameter;  // before area
      area = radius * pie * radius;
    I am not getting the result all are zero nothing is showing.

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    int main()
    {                               //Program to find area, circumference and diameter of circle based on Radius.
    
    
      //Data types
      float diameter;
      float circumference;
      float radius;
      float area;
      float pie;
    
      //Enter the value of Radius for calculation of above.
      printf("Please enter the value of circle radius \n");
      scanf("%f", &radius);
    
      // Variable declaration for calculation
      diameter = radius * 2;
      circumference = diameter * pie;
      radius = diameter / 2;
      pie = circumference / diameter;
      area = radius * pie * radius;
    
    
      // Calculation of Pie
      printf("The Pie of Circle is %f\n", pie);
      //scanf("%f",&pie);
    
      // Calculation of Circle's Area.
      printf("The circle area is %f\n", area);
      //scanf("%f",&area);
    
      // Calculation of Circumference of Circle.
      printf("The Circumference of Circle is %f\n", circumference);
      //scanf("%f",&circumference);
    
      // Calculation of Diameter of Circle.
      printf("The Diameter of Circle is %f\n", diameter);
      //scanf("%f",&diameter);
    
      return 0;
    }
    Last edited by Salem; 09-03-2020 at 06:35 AM. Reason: Removed crayola

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    For future reference, please use "copy as text" in your code editor, and/or "paste as text" in your browser.
    The colour/font ridden mess isn't sustainable.

    circumference = diameter * pie;
    pie = circumference / diameter;

    You're going round in circles here!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by mba_110 View Post
    I am not getting the result all are zero nothing is showing.
    Of course, because your variable declaration doesn't make sense.

    circumference = diameter * pie; - is what?

    Code:
    
    
    Code:
    //Konfuse Deklaration - 5. Sept. 2020
    //Program to find area, circumference and diameter of circle based on Radius.
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     
    int main(void)
    {                       
        double diameter, circumference;
      double radius, circel_area;
        double pi = 3.141592;
       
      //Enter the value of Radius for calculation of above.
      printf("Please enter the value of circle radius in cm: ");
      scanf("%lf", &radius);    
      printf("\nThe Diameter of Circle is %.2lf cm\n", diameter = radius * 2);
        
        //Calculation of Circumference of Circle.
      printf("The Circumference of Circle is %.2lf cm\n", circumference = ((2 * pi) * radius));
     
      //Calculation of Circle's Area.
        double y = 2.0;
        circel_area = pi * pow(radius, y);
      printf("The circle area is %.2lf qcm\n", circel_area);
      
      return 0;
    
    }

  6. #21
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Kernelpanic View Post
    Of course, because your variable declaration doesn't make sense.

    circumference = diameter * pie; - is what?
    If diameter is 2 times radius, 2 times PI times r is the same as PI times diameter!

    And your code uses unecessary headers. Take a look at this simplification:
    Code:
    // circ.c
    //
    // Compile with:
    //    gcc -O2 -o circ circ.c
    //
    // NOTE: Don't need to link with libm.
    //
    
    #include <stdio.h>
    #include <stdlib.h>   // nedded only for EXIT_FAILURE, EXIT_SUCCESS constants.
                          // if you use 1 and 0 as return values, stdlib.h isn't necessary.
    #include <math.h>     // needed only for M_PI constant, otherwise, not needed.
    
    // FIX: Notice using "%lf" conversion is WRONG, except in scanf().
    int main( void )
    {
      double d, p, r, a;
    
      // Since stdout is LINE BUFFERED, you should call fflush().
      printf( "Please enter the value of circle radius in cm: " );
      fflush( stdout );
    
      // NOTE: You should check if scanf() was abble to do the
      //       conversion.
      if ( scanf( "%lf", &r ) != 1 )
      {
      error:
        fputs( "ERROR: Invalid value.\n", stderr );
        return EXIT_FAILURE;
      }
    
      if ( r < 0.0 )
        goto error;
    
      // NOTE: math.h defines M_PI with double precision.
      //       Don't need to call pow() from libm!
      d = r + r;          // 2r = r+r
      p = M_PI * d;       // 2πr == πd
      a = M_PI * r * r;   // πr² = πrr
    
      // NOTE: Here %lf is WRONG. To use 'long double' it should be %Lf (uppercase L),
      //       But, we need only double.
      printf( "Diameter : %f cm\n"
              "Perimeter: %f cm\n" 
              "Area     : %f cm²\n",
              d, p, a );
    
      return EXIT_SUCCESS;
    }
    Last edited by flp1969; 09-05-2020 at 02:07 PM.

  7. #22
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by mba_110 View Post
    I am not getting the result all are zero nothing is showing.

    Code:
      //Data types
      float diameter;
      float circumference;
      float radius;
      float area;
      float pie;
    
      //Enter the value of Radius for calculation of above.
      printf("Please enter the value of circle radius \n");
      scanf("%f", &radius);
    
      // Variable declaration for calculation
      diameter = radius * 2;
      circumference = diameter * pie;
      radius = diameter / 2;
      pie = circumference / diameter;
      area = radius * pie * radius;
      ...
    "pie" is undefined when calculating "diameter". Why calculate "radius" since you got it in scanf()?
    There is a M_PI constant in math.h. And you don't need stdlib.h.

  8. #23
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by flp1969 View Post
    There is a M_PI constant in math.h. And you don't need stdlib.h.
    Best to remember that M_PI is not standard, though

    Question 14.8
    Last edited by Hodor; 09-05-2020 at 09:04 PM.

  9. #24
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by flp1969 View Post
    // NOTE: Here %lf is WRONG. To use 'long double' it should be %Lf (uppercase L),
    No, "Lf" is for long double and I don't use long. For double is "lf" correct.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
       long double c = 42.0;
       double d = 42.0;   
    
    
       __mingw_printf("c = %Lf\n", c/3);  //Lf nur bei long double
       __mingw_printf("c = %lf\n", c/3);  //lf ist bei long double falsch (wrong)
       printf("d = %.4lf\n", d/3);             //Normal lf
    
    
       return 0;
    }
    Last edited by Kernelpanic; 09-06-2020 at 12:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C connect with mysql
    By Kinshara in forum C Programming
    Replies: 2
    Last Post: 01-16-2013, 02:44 AM
  2. how to connect borland c++ with mysql database
    By hdhd in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2010, 03:34 PM
  3. how to connect mysql in mfc.
    By sgh in forum C++ Programming
    Replies: 1
    Last Post: 12-28-2008, 12:41 AM
  4. C++ connect to MySQL
    By thl in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2006, 06:27 AM
  5. MySQL Connect from Outside Computer
    By juschillin in forum Windows Programming
    Replies: 0
    Last Post: 09-27-2002, 08:02 AM

Tags for this Thread