# Looping/Repetition help

• 09-16-2010
Looping/Repetition help
I've been trying to figure this out for the past couple days, but it seems to be defeating me. I'm just at the very beginning of learning C Programming and this is one of my assignments. I'm just having trouble with the looping part. In the assignment hand out it said to not use any relational operator (==, !=, >, >=, <, <=) in the condition test for the loop. I can't seem to do it any other way. Even when I do use it, it doesn't seem to stop right away. I put my "while" below where it displays the code numbers so it won't keep repeating every time.

I didn't want to post my actual code, so I just shortened it and changed some stuff.

this is the output I get (more or less). It doesn't want to stop when I put a 0 in at Enter amount or zero to stop. It always ask to enter the code as well.

1 - Code 1
2 - Code 2

Enter amount or zero to stop: x
Enter code: x
xx dollars = x

Enter amount or zero to stop: x
Enter code: x
xx dollars = x

Enter amount or zero to stop: 0
Enter code: 0
00 dollars = 0

Code:

#include <stdio.h>

const float num_1 = 1.234;
const float num_2 = 0.123;

int main()

{

// Declare Variables

int code;
float dollars,total;

// Input

printf("\n");
printf("1 - Code 1 (num_1)");
printf("\n");
printf("2 - code 2 (num_2)");
printf("\n");
printf("\n");
while (amount != 0) {
printf("Enter amount or a zero to stop: ");
scanf("%f", &dollars);
printf("Enter code: ");
scanf("%d", &code);

// Calculations

if (code>0 && code<3)
{
if (code == 1)
{
total = amount * num_1;
}
else if (code == 2)
{
total = amount * num_2;
}

} // End if-else

// Print output

switch (code)
{
case 1:
printf("%.2f dollars = %.2lf", USD, total);
break;
case 2:
printf("%.2f USD = %.2lf ", USD, total);
break;

default:
printf("Code %d is invalid.", code);
} // End Switch

} // End While

return 0;

}

• 09-17-2010
rogster001
.
you could use while(myVariable) this evaluates to while true, so if you get a zero the loop will break, i think minus numbers would be an issue though
• 09-17-2010
Okay, the my variable thing worked, but it is still asking to enter a code after I put in a 0 for the amount. I know it has to do something with putting a printf somewhere else right? I just can't seem to figure out where and still have the program run smoothly.
• 09-17-2010
smokeyangel
You could try to reorder the program, I guess that's what you mean by "putting a printf somewhere else". You'd have to have reading the 0 to stop the last thing in the loop.

A simpler way is to just test for 0 and break out of the loop:

Code:

while (amount != 0) {
printf("Enter amount or a zero to stop: ");
scanf("%f", &dollars);
if (!dollars) //equiv to if dollars != 0.0
break;

printf("Enter code: ");
scanf("%d", &code);

// Calculations

if (code>0 && code<3)
{
if (code == 1)
{
total = amount * num_1;
}
else if (code == 2)
{
total = amount * num_2;
}

} // End if-else

// Print output

switch (code)
{
case 1:
printf("%.2f dollars = %.2lf", USD, total);
break;
case 2:
printf("%.2f USD = %.2lf ", USD, total);
break;

default:
printf("Code %d is invalid.", code);
} // End Switch

} // End While
// execution will resume here after break
return 0;

}

Your code snippet doesn't set the amount variable anywhere -- maybe missed because you didn't post your full code, but thought I'd point it out.
• 09-17-2010