1

I'm just trying to get this program to take a number between 1 and 9 and calculate the radius and display it. This program is supposed to loop but all I'm getting is the "Thank you for using the software!" print function, it's not going through the loop at all and I can't seem to understand why.

#include <stdio.h> int main(void){ float i,r, V; for(i=0; i <= 4; i++){ while ( r != 0) { printf("Enter a radius between 1 and 9: \n"); scanf ("%f", &r); V= (3.141592)*r*r*10; if (r>=1 && r<=9){ printf("The cylinder volume is %f\n", V); } else if (r > 9 || r < 0){ printf ("The input is out of the acceptable range. Select an integer less than $/n"); } printf("Thank you for using the software!\n"); } return 0; } 

4 Answers 4

6

You never initialize r before entering your while loop, so this is undefined behavior.

Additionally, you want to use int s for equality operators, i.e. == or !=. In your case, you might want to include an "error" that r can be within.

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

4 Comments

What do you mean I don't initialize r, I'm still a noob so I'm a little slow on the lingo.
It means you never assign it a value, so it will give you junk (whatever was previously in that memory location). Initializing means you assign it a legitimate value, i.e. r = 1.0;
Okay i understand what initializing means now but how would i assign a value to a variable if I don't know what value the user is going to enter. The point of the program is to ask for an input from the user between 1 and 9 and assign that number to the variable r, do the calculation, reassign the answer to r again and then display that number. How would i assign r to a number before i know which numbers can be assigned?
scanf ("%f", &r); Will overwrite whatever you place in r. Note ugoren's post to avoid having to explicitly initialize r, but you still should not compare floats with equality operators, I would suggest looking for different criteria for terminating your loop.
1

you have not initialize r before using it. you must initialize it or use i to iterate though the loop.

INITIALIZE r here before using for (i = 0; i <= 4; i++) { while (r != 0) { //code } } 

4 Comments

He should probably initialize r inside the for loop. Otherwise it will never be entered again (not that I know what the for is for).
ofcourse he can initialize r in the loop but it will be over written as many times as the loop is executed. so its not a good idea to do that i guess.
It's a futile argument without really knowing what he wants to do.
I think I've explained this twice, but maybe not clearly. I'm trying to make a program that calculates the volume given the radius entered by the user(as long as the radius is between 1 and 9) if the radius is not in that range then it prints out "The input is out of acceptable range, enter an integer between 1 and 9 or press 0 to exit." If the radius IS in the correct range then it prints out "The Cylinder Volume is ....".
1

To make sure the loop is executed at least once, you could use

do { // your code } while ( r != 0);

Comments

0

you better use a do while loop to do that they way you are trying to do.

for(i=0; i <= 4; i++){ do { printf("Enter a radius between 1 and 9: \n"); scanf ("%f", &r); V= (3.141592)*r*r*10; if (r>=1 && r<=9){ printf("The cylinder volume is %f\n", V); } else if (r > 9 || r < 0){ printf ("The input is out of the acceptable range. Select an integer less than $/n"); }while ( r != 0) printf("Thank you for using the software!\n"); } 

i haven't tried this by my self.this might work.try it if it works.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.