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 {}
     }

  }
7
  • 2
    You should consider it a fatal mistake to ignore the result of scanf. Commented Sep 6, 2013 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? Commented Sep 6, 2013 at 17:19
  • 3
    @PrR3; Yes, you can. Its C99 feature. Commented Sep 6, 2013 at 17:23
  • 1
    Note that else {} is just noise for the sake of adding noise. It does nothing useful. Commented Sep 6, 2013 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. Commented Sep 6, 2013 at 17:38

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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).
@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: Not a very good assumption, it really can confuse the inexperienced reader. Stating that this loop will never finish is just plainly wrong.
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);
    }
}

Comments

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);
     } 

 }

Comments

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);
     }
}

4 Comments

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: stackoverflow.com/questions/3922599/… and avoid personal opinions when not completed by explanations.
What is the problem with break statement?
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).
Also, how can I avoid personal opinions if you are arguing readability and good/bad practices....?
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;
     }
 }

1 Comment

@P0W; OK. Now it will terminate if r <= 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.