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