0

I am confused on how to complete this for loop. The mission is to read input in unix. For the input if the radius is >0 it should prompt the user each time and then if <=0 it should terminate. I am going from centimeters to square inches. My current configuration requires 2 inputs (1 prompted, 1 not) before giving output to the console. Cheers.

#include <stdio.h>
#define PI 3.14159

main()
{
float r, a;
  int y = 9999999;


  for(int i =0; i <y; i++){
     printf("Enter the circle's radius (in centimeters): ");
     scanf ("%f", &r);

        if(r>0){
           r=r;
       a = PI * r * r *2.54;

       printf("Its area is %3.2f square inches.\n", a);
   }  else {}
     }

  }
Fish
  • 103
  • 3
  • 10
  • 2
    You should consider it a fatal mistake to ignore the result of `scanf`. – Kerrek SB Sep 06 '13 at 17:18
  • 3
    Actually you should divide by 2.54, not multiply, and you should do it _twice_, not once. And what's this `r=r;` business? – Mike Dunlavey Sep 06 '13 at 17:19
  • btw it's not allowed to declare the loop counter "i" inside the loop description. that's java-style – PrR3 Sep 06 '13 at 17:21
  • ^^ I am used to java, which is why even simple things in C seem unfamiliar – Fish Sep 06 '13 at 17:22
  • 3
    @PrR3; Yes, you can. Its C99 feature. – haccks Sep 06 '13 at 17:23
  • 1
    Note that `else {}` is just noise for the sake of adding noise. It does nothing useful. – Jonathan Leffler Sep 06 '13 at 17:33
  • 1
    Is there some significance to the use of "C-" in your title? There actually is a language called "C--", and I've heard of a joke language called "C-", named after the grade its author received in a language design course. Your title needn't say what language you're using (the tag does that), and it should describe the problem you're having. – Keith Thompson Sep 06 '13 at 17:38

5 Answers5

2

Your code flow is the following:

for (infinite condition) {
  scan input
  if (input > 0) {
    do things
  }
  else {
    do nothing
  }
}

So there's no way to exit out of the loop, that's why the break statement exists, to force quitting an iterative block of code:

while (true) {
  scanf ("%f", &r);
  if (r > 0) {
    // do whatever;
  }
  else
    break;
}

The break will stop the cycle when executed, just going out of the loop.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • 2
    It's not infinite; it is just shy of 10 million iterations, which is a long way off infinite in any mathematical sense. In fact, only in the sense of 'humans won't type that many entries' is it infinite. If the data comes from a file, 10 million lines isn't a big problem (though the count still shouldn't be hard-coded; it should be conditioned on EOF). – Jonathan Leffler Sep 06 '13 at 17:35
  • 1
    @JonathanLeffler: It's an assumption I made to simplify the problem and focus on the question itself. Actually input is parsed from `stdin` so the only break condition should be EOF as you point out. – Jack Sep 06 '13 at 17:37
  • 1
    @Jack: Not a very good assumption, it really can confuse the inexperienced reader. Stating that this loop will never finish is just plainly wrong. – zoska Sep 06 '13 at 19:17
1

Consider a while loop instead:

#include <stdio.h>
#define PI 3.14159

main(){
    float r, a;

    int continueBool = 1;
    while(continueBool == 1){
         printf("Enter the circle's radius (in centimeters): ");
         scanf ("%f", &r);

         if(r>0){
              a = PI * r * r *2.54;
              //the above formula may be wrong, so consider trying:
              //a = PI * r * r/2.54/2.54;
              printf("Its area is %3.2f square inches.\n", a);
         }
         else{
             continueBool = 0;
         }
     }
}

The break statement can be dangerous if you are new to C programming, so I recommend not using it until you get a better understanding of C and break. If you do want to use break, then this could be your solution:

#include <stdio.h>
#define PI 3.14159

main(){
    float r, a;

    while(1){
         printf("Enter the circle's radius (in centimeters): ");
         scanf ("%f", &r);

         if(r<=0){
             break;
         }

         a = PI * r * r *2.54;
         //the above formula may be wrong, so consider trying:
         //a = PI * r * r/2.54/2.54;
         printf("Its area is %3.2f square inches.\n", a);
     }
}
  • 2
    Please explain why `break` should be dangerous. It's perfectly fine to use it in this situation. It's readable and the duty of knowing which loop you are breaking in case of nested loops is of the programmer. Using a boolean variable when not needed is not just readable. Please take a look at: http://stackoverflow.com/questions/3922599/is-it-a-bad-practice-to-use-break-in-a-for-loop and avoid personal opinions when not completed by explanations. – Jack Sep 06 '13 at 17:31
  • What is the problem with `break` statement? – haccks Sep 06 '13 at 17:52
  • Break "can" be dangerous depending on programming skill level. It seems that the open poster is new to C programming. Getting him/her into the practice of using "break" statements over properly controlled loops is (in my opinion) a bad idea. However, learning break is still very important (I'm not saying it's not). – But I'm Not A Wrapper Class Sep 06 '13 at 18:13
  • Also, how can I avoid personal opinions if you are arguing readability and good/bad practices....? – But I'm Not A Wrapper Class Sep 06 '13 at 18:22
1
r=1.0f;

// break if no. of cases exhausted or r is negative or zero
for(int i =0; i < y && r > 0; i++) 
{
     printf("Enter the circle's radius (in centimeters): ");

     if( scanf ("%f", &r) == 1) // Always check for successful scanf
     {
          a = PI * r * r/2.54/2.54; //This is correct formula

          printf("Its area is %3.2f square inches.\n", a);
     } 

 }
P0W
  • 46,614
  • 9
  • 72
  • 119
1

You may want to try using a while loop instead so that the question is continually prompted until the user inputs a value =>0. see if below helps (also your conversion factor was not quite right);

#include <stdio.h>
#define PI 3.14159

void main()
{
    float r, a;
    printf("Enter the cirle's radius (in centimeters):");
    scanf("%f",&r); 

    while (r>0)
    {
        a=PI*r*r*0.155; // conversion from sqcm to sqin is ~0.155
        printf("Its area is %3.2f square inches \n", a);
        printf("Enter the cirle's radius (in centimeters):");
        scanf("%f",&r);
    }
}
sheepez
  • 986
  • 1
  • 10
  • 26
0

Use this:

for(int i =0; i < y; i++)
{
     printf("Enter the circle's radius (in centimeters): ");
     scanf ("%f", &r);

     if(r > 0)
     {
          a = PI * r * r *2.54;

          printf("Its area is %3.2f square inches.\n", a);
     } 
     else
     {
          break;
     }
 }
haccks
  • 104,019
  • 25
  • 176
  • 264