Reputation: 13
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;
}
Upvotes: 1
Views: 106
Reputation: 169
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.
Upvotes: 0
Reputation: 8308
To make sure the loop is executed at least once, you could use
do {
// your code
} while ( r != 0);
Upvotes: 1
Reputation: 169
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
}
}
Upvotes: 1
Reputation: 8346
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.
Upvotes: 6