QUOTE(Amadeus @ 20 Jul, 2007 - 08:36 AM)

and yes, you can structure a while loop in a variety of ways to do the error checking.
Hi again. I tried doing a while loop thing to make it return back to the "Enter transaction code:" statement.
Here's what I came up with:
CODE
#include <stdio.h>
#define MAXCOUNT 1
int main ()
{
char transaction_code;
int count, amount;
printf ("Transaction codes:\n D = Deposit\n W = Withdrawal\n T = Transfer\n\n");
count = 0;
while (count<MAXCOUNT)
{
printf ("Enter Transaction Code: ");
scanf ("%c", &transaction_code);
if (transaction_code == 'D'||transaction_code == 'W'||transaction_code == 'T'||transaction_code == 'd'||transaction_code == 'w'||transaction_code == 't'&& transaction_code != '\0')
{
printf ("Transaction code is valid\n");
count=1;
}
else
printf ("Transaction code is invalid!\n");
}
switch (transaction_code)
{
case 'D': case 'd':
printf ("Enter Amount:");
scanf ("%d", &amount);
printf ("Deposit \n Amount: %d\n", amount);
break;
case 'W': case 'w':
printf ("\n Enter Amount:");
scanf ("%d", &amount);
printf ("Withdrawal\n Amount: %d\n", amount);
break;
case 'T': case 't':
printf ("\n Enter Amount:");
scanf ("%d", &amount);
printf ("Transfer\n Amount: %d\n", amount);
break;
}
return 0;
}
It works a little bit but it has a kink that I can't figure out. When I don't input anything, it does what I want it to do which is just display
"Transaction code was invalid!"
and then
"Enter transaction code:"
then it lets me enter the code and proceed normally if I enter a valid code. But when I type invalid letter for the code,
it displays
"Enter Transaction code: Transaction code was invalid!"
and then
"Enter Transaction Code:"
which again proceeds normally if I enter a valid code.
I'm trying to move around the } symbol to stop it from displaying that unwanted line but it doesn't seem to be working. I also tried using a break; statement but it doesn't work either.
Like I said, this really isn't part of what we were asked to do anymore but I've been reading ahead on this "A First Book of ANSI C" that I have and it just seems like I'm so close so I'm really curious as to how to make it work.
This post has been edited by jadedjeanne: 20 Jul, 2007 - 10:46 AM