I need to write a program that would test whether the transaction code is valid and then I should be able to enter the amount and then it should display both the type of transaction and then the amount entered.
I was able to write the program using the "switch" statement but I can't make it work when I try to use the "if-else if-else". I keep getting an error saying I have an 'else' statement without a partnered 'if' when I try to compile it in Visual Studio 2005. I think I'm having a problem with where to put the {} symbols.
here's the code I wrote using 'switch' which works, although I was wondering if there was a way to put the validation part within the switch function instead.
#include <stdio.h>
int main ()
{
char transaction_code;
int amount;
printf ("Transaction codes:\n D = Deposit\n W = Withdrawal\n T = Transfer\n\n");
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')
printf ("Transaction code is valid\n");
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;
}
And here's my code when I try to use if-else if - else that I keep getting that unpaired 'else' error on:
#include <stdio.h>
int main (void) {
char transaction_code;
int amount;
printf ("Transaction Codes: \n D = Deposit \n W = Withdrawal \n T = Transfer \n");
printf ("Enter Transaction Code:");
scanf ("%c", &transaction_code);
if (transaction_code == 'D')
{
printf ("Enter Amount:");
scanf ("%d", &amount);
printf ("Deposit \n Amount: %d \n", amount);
}
else if (transaction_code == 'W')
{
printf ("Enter Amount:");
scanf ("%d", &amount);
printf ("Withdrawal \n Amount: %d \n", amount);
}
else if (transaction_code == 'T');
{
printf ("Enter Amount:");
scanf ("%d", &amount);
printf ("Transfer \n Amount: %d \n", amount);
}
else
{ printf ("The Transaction code was invalid!");
}
return (0);
}
I don't know what I'm doing wrong. Any help would be greatly appreciated.
And i'm also wondering how I can make the program go back to the "enter transaction code" statement if the user enters an invalid code. Do I just use a 'while' or some other loop statement?
Our prof. didn't really ask us to do that yet but I was just curious as to how that could be done.
Again, thanks.

New Topic/Question
Reply




MultiQuote





|