Thread: New to C - Would appreciate help fixing this issue

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    19

    New to C - Would appreciate help fixing this issue

    Hello all and thanks for taking a look at my problem. When I run this code, a few errors happen. Once the program reaches the Gauss Lens formula, it ignores all of the scanf()'s and returns values of 0's for my answer. Also, under 'character encoding' all I'm getting in return is 'a' regardless of what character I use as my &plaintext_character. The weird thing about the gauss lens formula is that if I play around with the formula, for example, letting focal_length = object_distance, it will not skip scanf(), but will skip with any other combination..here's my code:

    Code:
    
    
    Code:
    #include<stdio.h>
    #include<math.h>
    #define _CRT_SECURE_NO_WARNINGS
    #define PI 3.14159
    
    int main(void)
    {
     
     float mass = 0, acceleration = 0, force = 0, radius = 0, height = 0, volume = 0, radius_squared = 0, object_distance = 0, image_distance = 0, focal_length = 0, tangent = 0, theta = 0;
     
    char plaintext_character = 0, encoded_character; 
     
      
     
     printf("\t\t\t\t\tPhysics Calculator!\n\n");
     
    //Newton's Second Law of Motion
    
     printf("Newton's second law of motion: Force = Mass * Acceleration
    \n\n");
    
     printf("Find 'Mass' in kg and 'Acceleration' in m/s^2:\n");
    
     printf("Mass = ");
    
     scanf("%f", &mass);
    
     printf("Acceleration = ");
    
     scanf("%f", &acceleration);
     
     force = mass * acceleration;
     printf("Force = %.3f N\n", force);
    
     //Volume
    
     printf("\n\nFind the Volume of a cylinder with the following equation: 
    
    Volume = pi * r^2 * height. Find values in feet\n");
    
     printf("Radius = ");
    
     scanf("%f", &radius);
    
     printf("Height = ");
    
     scanf("%f", &height);
     
    radius_squared = pow(radius, 2);
    
     volume = radius_squared * height * PI; 
    
     printf("Volume = %.3lf ft^3", volume); 
    
    
    
     //character encoding
    
     printf("\n\n\nEncoded_character = (plaintext_character - 'a') + 'A' \n");
    
     printf("Enter a single character = ");
    
     scanf("%f", &plaintext_character);
    
        encoded_character = ( plaintext_character - 'a' ) + 'A'; 
    
     printf("\nEncoded character = %c\n", encoded_character );
     
     
    
       //Gauss Lens
    
     printf("\n\nFind the focal length of a lens via the Gauss Lens 
    Formula : 1/f = 1/v + 1/u where v is the image distance and u is the object distance:\n\n");
    
     printf("U = ");
    
     scanf(" %f", &object_distance);
    
     printf("\nV = ");
    
     scanf(" %f", &image_distance);
    
     focal_length = 1/(image_distance/1 + object_distance/1);
    
     printf("\nFocal Length = %.3lf\n\n\n", focal_length); 
    
    
     //Tangent
     printf("Find the Tangent of an angle in radians.\n");
    
     printf("Enter an angle in radians: ");
    
     scanf(" %f ", &theta);
    
     tangent = sin(theta) / cos(theta); 
     printf("\nTangent%f = %lf\n", theta, tangent);
    
    
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    Are you positive that there is a whitespace character before your number in those scanf() calls? Remember that if you add some "stray" character to a scanf() format specifier those "stray" characters must exist in the input stream.
    Code:
    scanf(" %f", &object_distance);
           ^

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    19
    Quote Originally Posted by jimblumberg View Post
    Are you positive that there is a whitespace character before your number in those scanf() calls? Remember that if you add some "stray" character to a scanf() format specifier those "stray" characters must exist in the input stream.
    Code:
    scanf(" %f", &object_distance);
           ^

    Jim
    Hey Jim, thanks for the response. Yeah, I actually went back and added the white space after doing some research and it still skipped over the scanf().

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    Since you're using scanf() why not just remove that space from the scanf() format specifier? Remember by default most of the specifiers skip leading whitespace characters in most circumstances.



    Jim

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    scanf("%f", &plaintext_character);
    you can't do it - %f expects pointer to float not pointer to char

    use a compiler that can check these things (gcc or one of latest VS)

    and always check the return value of scanf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2017
    Posts
    19
    I stripped the whitespace from the scanf() format specifier and the program still skips past them from the gauss lens formula down. I'm a bit confused because the syntax looks exactly the same as the code above it, which does what I intended it to do.

  7. #7
    Registered User
    Join Date
    Jan 2017
    Posts
    19
    thanks for taking a look at my code. Appreciate it. I changed %f to %c for scanf("%c", &plaintext_character);

    when I did this, the program ignored scanf("%c", &plaintext_character); but every scanf() below that was read. Before making that change, scanf() below (//gauss lens) was skipped


    -I'm also using VS 15, no error messages when I compiled it.

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You are trying to read a character with the %f format. Try " %c". The space before the %c is important to force scanf to skip the previous newline character that is still in the input buffer from the last scanf. The leading space is not necessary for your "%f" formats since that format (most formats) skips initial whitespace.

    You are arbitrarily using either "%f" or "%lf" in your printf's to print your floats. You may as well just use "%f", which for printf actually works for either floats or doubles since floats are actually passed to printf as doubles (but scanf needs "%lf" to read a double).

    In fact, we would usually use doubles for a program like this. Nothing is gained by limiting your precision with floats. If you switch to doubles, remember to use "%lf" in your scanfs.

    Your character "encoding" is pretty weird, and perhaps not really what you want. E.g., if I enter an exclamation point and assuming an ascii-ish encoding, the calculation is:
    '!' - 'a' + 'A' = 33 - 97 + 65 = 1
    which yields an unprintable character. If I enter a lowercase character, it converts it to uppercase.

    We usually explicitly "return 0" from main (to indicate success).

  9. #9
    Registered User
    Join Date
    Jan 2017
    Posts
    19
    Thanks algorism. I added whitespace to " %c" and the program ran perfectly. Thanks for pointing that out; my textbook didn't mention how data types treat whitespaces, at least not yet.

    Thanks again for all the pointers, every bit of info helped a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help fixing command prompt file input issue
    By blackfox_1109 in forum C Programming
    Replies: 3
    Last Post: 10-21-2014, 10:08 PM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  3. Need help with fixing modules
    By 116soulja in forum C Programming
    Replies: 10
    Last Post: 01-16-2010, 12:26 AM
  4. How would I got about fixing this?
    By SlyMaelstrom in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2005, 05:34 AM
  5. Fixing errors!
    By Zophixan in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2002, 06:18 PM

Tags for this Thread