-2

So I've decided to add some programming skills to my repertoire, typically i've been an application support analyst with beginner to average SQL coding skills, but in today's climate I know I need to get better at automating simple tasks. So I bought a course in c programming (this is not for college credit, so by asking I'm not cheating). So far it's going quite well, but I've come against a problem, actually I decided to improve upon the challenge given to me and I can't seem to figure it out. Originally I was asked to create a small application that printed out the width, height, perimeter and area of a rectangle, with the width and the height hard-coded. That was simple to figure out. I decided to add an extra challenge and have the end user input the width and height of rectangle, but it's not working like I planned. I'm including my code and as well my output after compiling. I don't really want an answer as I would like to discover the solution myself, but if someone could point me to some documentation that would be much appreciated.

CODE:

#include <stdio.h>
#include <stdlib.h>
int main()

{
    double width;
    double height;
    double perimeter;
    double area;
    char newLine = '\n';
    printf("Please enter the width of your rectangle:");
    scanf("%f", &width);
    printf("Please enter the height of your rectangle:"); 
    scanf("%f", &height);
    perimeter = 2.0 * (height + width);
    area = (width * height);
    printf("%c",newLine);
    printf("The widith of the Rectangle is: %.2f centimeters %c",width, newLine);
    printf("The height of the rectangle is: %.2f centimeters %c",height, newLine);
    printf("The permimeter of the rectangle is: %.2f centimeters %c",perimeter, newLine);
    printf("The area of the rectangle is: %.2f centimeters %c",area, newLine);
    printf("%c",newLine);
    return 0;
}

OUTPUT:

[user@localhost.local]$ ./output/rectangle.a 
Please enter the width of your rectangle:15
Please enter the height of your rectangle:12

The widith of the Rectangle is: 0.00 centimeters 
The height of the rectangle is: 0.00 centimeters 
The permimeter of the rectangle is: 0.00 centimeters 
The area of the rectangle is: 0.00 centimeters 
Reuben deVries
  • 127
  • 1
  • 7
  • 4
    Did you compile with `-Wall`? If you did, the compiler would most likely have complained that you are using the wrong format specifier. `%f` are for reading/writing `float`. You have `double`, and should use `%lf`. – HAL9000 Oct 20 '19 at 20:50
  • I was using gcc, it didn't complain but I'll try that for sure. – Reuben deVries Oct 20 '19 at 20:51
  • 3
    `-Wall` is not a compiler, it is an option that most compilers support for warning you when you are doing something suspect. It helps you miss when aiming at your foot. It is highly recommended for all c-programmers, beginners and experts alike. – HAL9000 Oct 20 '19 at 21:04
  • Thanks great advice. I will use this from now on. – Reuben deVries Oct 20 '19 at 21:05
  • 1
    Just take note that no warnings will stop you from doing a semantically and syntactically valid facepalm, like only copying one byte with `memcpy`. – S.S. Anne Oct 20 '19 at 21:24
  • 1
    Side note: There's a poor lesson that virtually all beginning C courses teach you, and that is: "If you want to read input from the user, use `scanf`". There is an important real-world lesson that almost no beginning C course teaches you, and that is: "Never use `scanf` for anything." `scanf` is square training wheels for beginning C programmers. Wean yourself off of it as soon as you can (see [here](https://stackoverflow.com/questions/58403537) for hints). – Steve Summit Oct 20 '19 at 22:12

1 Answers1

0

Your code is fine, the only problem is you using %f insted of %lf. %f is for float variable and %lf is for double variable.

The output of your program after fix:

Please enter the width of your rectangle:25
Please enter the height of your rectangle:12
The height of the rectangle is: 12.00 centimeters
The widith of the Rectangle is: 25.00 centimeters

The permimeter of the rectangle is: 74.00 centimeters
The area of the rectangle is: 300.00 centimeters

you can use this table to understand how to use each sort of data-types in c: https://www.geeksforgeeks.org/data-types-in-c/

ValeriF21
  • 454
  • 1
  • 3
  • 14