# Homework Help: Basic I/O in C

1. Sep 15, 2016

### Marcin H

1. The problem statement, all variables and given/known data

Write a few lines of code that will accomplish the following:

1. Ask the user to first enter his/her weight (in kilograms) and second enter the height (in meters). For every field to be filled in, print on screen the messages "Type your weight: " and "Type your height: " respectively.
2. Print on screen on a different line the message "Your BMI is: " and the BMI value of the user according to the following formula:
BMI= weight in kg/(height in m)^2

Note: for this problem, there is no need to write any #include preprocessor directives in the solution

Hint: the formula is easy to implement if you remember the definition of power of a number

2. Relevant equations
C language

3. The attempt at a solution
I tried running this program and everything works fine except the last step where it should do the calculation and output the BMI. I am not sure what I did wrong. This is my first time programming with C, so I am sorry if this is a dumb/easy mistake, but I am not sure what I am missing. Why is the program not outputting an answer? Using an online compiler I get this as a result:

sh-4.3$main Type your weight in kilograms:87 Type your height in meters:1.4 Your BMI is 44sh-4.3$

Here is my code:

Code (C):
#include <stdio.h>

int main()
{
float weight, height, BMI;

scanf("%f", &weight);

scanf("%f", &height);

BMI = weight / (height*height);

return 0;
}

2. Sep 15, 2016

### Staff: Mentor

What is that first line? Is it you invoking your program?

3. Sep 15, 2016

### QuantumQuest

Your problem is truncation as I see it. So, take a careful look to your code for some typo.

4. Sep 15, 2016

### Marcin H

I am not sure what that is. That is what the online compiler spits out when I try to execute

5. Sep 15, 2016

### Marcin H

Which line exactly? I am not sure where I made the mistake. I the last printf line to this:

Code (Text):

return 0;
}
Someone told me that should fix my problem, but it did not.

6. Sep 15, 2016

### Krylov

I think it's just the Unix shell "sh" running the generated executable, named "main". It should not matter for your program.

As far as I can see, you are almost there. Try

Code (Text):

This prints the floating point variable BMI accurate to two decimal places and end the output with a newline. (Have a look at formatting specifiers for the "printf" function.)

What puzzles me more is the comment in the assignment about not needing #include directives. I do not see how you can do IO without them.

7. Sep 15, 2016

### QuantumQuest

First, it is absolutely necessary as berkeman asks you to understand what is this sh-4.3\$. On what operating system are you trying to run your program? You have to fully understand such things, in order to run your programs smoothly.

For about the exact line that the typo lies, it will do no good to you and a violation of PF rules, to tell you exactly. My hint, with little thought, will lead to the appropriate line. Think about what your output misses and where the cause would be.

Last edited: Sep 15, 2016
8. Sep 15, 2016

### Krylov

In view of what is mentioned in the OP about not needing #includes, could it be that the real crux of the problem is to write this without using IO library functions? I wouldn't know how to do that out of the top of my head, but most likely the solution will be quite low level and dependent on the system that runs the compiler.

9. Sep 15, 2016

### Marcin H

Yeah I am not sure about that hint either. Doesn't <stdio.h> contain all the functions like print and scan and others?

Also, can you explain why this does not work:

Code (C):
I was told that this should work and I have not learned what you wrote in that one bit. Do you always need to add %.2f\n after trying to print out a result? Is it the newline that makes the program work or the %.2f? Also if you want more accuracy how much more can you get using %.f? Will %.f just give you an integer? Will %.9 give you 9 digits of accuracy? Also why can't you just use %f to say that BMI is just a floating point and make it print as many decimals as a floating point can?

10. Sep 15, 2016

### QuantumQuest

Absolutely right. Because of this complexity and accepting the code that OP shows and if being accepted in this way, I just try to hint OP to make it run. Then, he/she can search the more difficult thing.

11. Sep 15, 2016

### Marcin H

This code won't work if you don't include #include <stdio.h> because then you won't be able to use printf or scanf, which is something really important that we learned. All my examples use those 2 functions so I have to use them here. Also I am using THIS just to test if the program works or not.

12. Sep 15, 2016

### QuantumQuest

Yes, I know that this is the usual way of doing things. So, if there is no problem with your
Code (Text):
#include
according to the Note under the problem statement, the problem is what I pointed out in #3. A little careful inspection line-by-line in conjunction with my hint, will give immediately the solution.

13. Sep 15, 2016

### Krylov

Your questions are partially answered by the link that I gave to the page that explains formatting strings. The basic idea is that "printf" takes an arbitrary number of arguments, the first of which is a formatting string containing zero or more conversion specifiers. The arguments after that, if present, are matched with each conversion specifier and printed accordingly. It is really worthwhile reading the text in that link (or a similar text in your reference book) because you will see this pattern very often, even in other programming languages.

If you leave out the newline, your BMI will print correctly but the shell prompt will appear immediately after it, on the same line. This is not very pretty.

The amount of accuracy you can get depends on the data type of your variable. I suppose you can print a float with 9 or even 15 digits of accuracy, but because of the memory space usually reserved for a float everything beyond the 7th digit or so will not be meaningful.

Some of your other questions can be answered by simply adapting the code accordingly, recompiling and rerunning. Experimenting like this is a good thing. For example, as you can see in your own OP, "%.f" will indeed just give you an integer as output, without a newline. Some of this behavior is system dependent.