If-else if- else statements in C

keep getting "unpaired else error

Page 1 of 1

14 Replies - 2003 Views - Last Post: 21 July 2007 - 08:39 AM Rate Topic: -----

#1 jadedjeanne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-June 07

If-else if- else statements in C

Posted 20 July 2007 - 08:30 AM

Hi. I'm trying to do an assignment where in I have to write a sort of banking transaction program.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: If-else if- else statements in C

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: If-else if- else statements in C

Posted 20 July 2007 - 08:36 AM

else if (transaction_code == 'T');


If statements do not end with a semi colon. this is the cause of your error.

and yes, you can structure a while loop in a variety of ways to do the error checking.
Was This Post Helpful? 0
  • +
  • -

#3 jadedjeanne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-June 07

Re: If-else if- else statements in C

Posted 20 July 2007 - 08:48 AM

View PostAmadeus, on 20 Jul, 2007 - 08:36 AM, said:

else if (transaction_code == 'T');


If statements do not end with a semi colon. this is the cause of your error.

and yes, you can structure a while loop in a variety of ways to do the error checking.


Cool! That worked. Thanks. Wow. All that trouble for just one little semi-colon. :P I was this '_' close to pulling out my hair when I was trying to code the darn thing a few hours ago. Thank you so much.

This post has been edited by jadedjeanne: 20 July 2007 - 08:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: If-else if- else statements in C

Posted 20 July 2007 - 10:22 AM

View Postjadedjeanne, on 20 Jul, 2007 - 08:48 AM, said:

View PostAmadeus, on 20 Jul, 2007 - 08:36 AM, said:

else if (transaction_code == 'T');


If statements do not end with a semi colon. this is the cause of your error.

and yes, you can structure a while loop in a variety of ways to do the error checking.


Cool! That worked. Thanks. Wow. All that trouble for just one little semi-colon. :P I was this '_' close to pulling out my hair when I was trying to code the darn thing a few hours ago. Thank you so much.


Out of curiosity, what was the error that Visual Studio gave you?
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: If-else if- else statements in C

Posted 20 July 2007 - 10:41 AM

The error was likely that there was a mismatch between if else statements: "'else' statement without a partnered 'if' " since the semi colon would have caused the compiler to see an execution point as opposed to another decision making process. the if with the semi colon would have stood along - and the next piece of code was an else that had no if.
Was This Post Helpful? 0
  • +
  • -

#6 jadedjeanne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-June 07

Re: If-else if- else statements in C

Posted 20 July 2007 - 11:46 AM

View PostAmadeus, on 20 Jul, 2007 - 08:36 AM, said:

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:

#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 July 2007 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: If-else if- else statements in C

Posted 20 July 2007 - 11:55 AM

   printf("enter a transaction code!\n");
   scanf ("%c", &transaction_code);
while(toupper(transaction_code)!='D' && toupper(transaction_code)!='T' && toupper(transaction_code)!='W')
{
   printf("enter a valid transaction code!\n");
   scanf ("%c", &transaction_code);
}
//rest of your code


Include the ctype.h library for the toupper function.
Was This Post Helpful? 0
  • +
  • -

#8 jadedjeanne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-June 07

Re: If-else if- else statements in C

Posted 20 July 2007 - 01:06 PM

View PostAmadeus, on 20 Jul, 2007 - 11:55 AM, said:

   printf("enter a transaction code!\n");
   scanf ("%c", &transaction_code);
while(toupper(transaction_code)!='D' && toupper(transaction_code)!='T' && toupper(transaction_code)!='W')
{
   printf("enter a valid transaction code!\n");
   scanf ("%c", &transaction_code);
}
//rest of your code


Include the ctype.h library for the toupper function.


I tried that but it still doubles the error message "Enter a valid transaction code!" when I enter an invalid code. I also works okay when I leave it blank. Hmm... maybe I should just wait until I know about pointers maybe?
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: If-else if- else statements in C

Posted 20 July 2007 - 01:09 PM

Can you post the exact code you are now using?
Was This Post Helpful? 0
  • +
  • -

#10 jadedjeanne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-June 07

Re: If-else if- else statements in C

Posted 20 July 2007 - 01:20 PM

View PostAmadeus, on 20 Jul, 2007 - 01:09 PM, said:

Can you post the exact code you are now using?


Here it is:

#include <stdio.h>
#include <ctype.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);
	while(toupper(transaction_code)!='D' && toupper(transaction_code)!='T' && toupper(transaction_code)!='W')
	{
   printf("Enter a Valid Code!\n");
   scanf ("%c", &transaction_code);
	}

		
	

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



Was This Post Helpful? 0
  • +
  • -

#11 zyruz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 13-August 05

Re: If-else if- else statements in C

Posted 20 July 2007 - 01:46 PM

Problem is that you read a the newline.

change
	while(toupper(transaction_code)!='D' && toupper(transaction_code)!='T' && toupper(transaction_code)!='W')
	{	printf("Enter Transaction Code:");
	scanf ("%c", &transaction_code);
	}

to
	while(toupper(transaction_code)!='D' && toupper(transaction_code)!='T' && toupper(transaction_code)!='W')
	{   printf("Enter a Valid Code!\n");
   scanf ("%c%", &transaction_code);
	}


zy

This post has been edited by zyruz: 20 July 2007 - 01:49 PM

Was This Post Helpful? 0
  • +
  • -

#12 gogole  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 17-July 07

Re: If-else if- else statements in C

Posted 20 July 2007 - 04:35 PM

	while(toupper(transaction_code)!='D' && toupper(transaction_code)!='T' && toupper(transaction_code)!='W')
	{   printf("Enter a Valid Code!\n");
   scanf ("%c%", &transaction_code);
	}


Hey how come u used "%c%" ,that will definately generate an error. it should be this:
	while(toupper(transaction_code)!='D' && toupper(transaction_code)!='T' && toupper(transaction_code)!='W')
	{   printf("Enter a Valid Code!\n");
   scanf ("%c", &transaction_code);
	}

Was This Post Helpful? 0
  • +
  • -

#13 zyruz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 13-August 05

Re: If-else if- else statements in C

Posted 21 July 2007 - 01:07 AM

This shuld work as intended. the %c% is wrong :(
	scanf ("%c", &transaction_code);
	while(toupper(transaction_code)!='D' && toupper(transaction_code)!='T' && toupper(transaction_code)!='W')
	{
		if(transaction_code != '\n') // dont show the warning for the newlines.
		{
			printf("Enter a Valid Code!\n");
		}
		scanf ("%c", &transaction_code);
	}



zyruz

This post has been edited by zyruz: 21 July 2007 - 02:00 AM

Was This Post Helpful? 0
  • +
  • -

#14 jadedjeanne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-June 07

Re: If-else if- else statements in C

Posted 21 July 2007 - 07:23 AM

View Postzyruz, on 21 Jul, 2007 - 01:07 AM, said:

This shuld work as intended. the %c% is wrong :(
	scanf ("%c", &transaction_code);
	while(toupper(transaction_code)!='D' && toupper(transaction_code)!='T' && toupper(transaction_code)!='W')
	{
		if(transaction_code != '\n') // dont show the warning for the newlines.
		{
			printf("Enter a Valid Code!\n");
		}
		scanf ("%c", &transaction_code);
	}



zyruz


hey thanks. that did it. Although I'm not really sure I understand why it was reading the new line and putting an error for it.

Anyway, thanks guys. You've been a big help.

View Postzyruz, on 21 Jul, 2007 - 01:07 AM, said:

This shuld work as intended. the %c% is wrong :(
	scanf ("%c", &transaction_code);
	while(toupper(transaction_code)!='D' && toupper(transaction_code)!='T' && toupper(transaction_code)!='W')
	{
		if(transaction_code != '\n') // dont show the warning for the newlines.
		{
			printf("Enter a Valid Code!\n");
		}
		scanf ("%c", &transaction_code);
	}



zyruz


hey thanks. that did it. Although I'm not really sure I understand why it was reading the new line and putting an error for it.

Anyway, thanks guys. You've been a big help.
Was This Post Helpful? 0
  • +
  • -

#15 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: If-else if- else statements in C

Posted 21 July 2007 - 08:39 AM

It's due to the fact that when you are using scanf() to read a character, you are reading it from the buffer. When a character is entered by the user, however, the user actually enters a character, and then presses the enter key, also putting a newline character into the buffer. Next time scanf() is used to read a character, the first character in there is the last character entered - the newline.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1