Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,679 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,191 people online right now. Registration is fast and FREE... Join Now!




If-else if- else statements in C

2 Pages V  1 2 >  
Reply to this topicStart new topic

If-else if- else statements in C, keep getting "unpaired else error

jadedjeanne
post 20 Jul, 2007 - 07:30 AM
Post #1


New D.I.C Head

*
Joined: 13 Jun, 2007
Posts: 22


My Contributions


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.

CODE

#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:

CODE

#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. smile.gif

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.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 20 Jul, 2007 - 07:36 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


CODE

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.
User is offlineProfile CardPM

Go to the top of the page

jadedjeanne
post 20 Jul, 2007 - 07:48 AM
Post #3


New D.I.C Head

*
Joined: 13 Jun, 2007
Posts: 22


My Contributions


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

CODE

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. tongue.gif 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 Jul, 2007 - 07:49 AM
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 20 Jul, 2007 - 09:22 AM
Post #4


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


QUOTE(jadedjeanne @ 20 Jul, 2007 - 08:48 AM) *

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

CODE

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. tongue.gif 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?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 20 Jul, 2007 - 09:41 AM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

jadedjeanne
post 20 Jul, 2007 - 10:46 AM
Post #6


New D.I.C Head

*
Joined: 13 Jun, 2007
Posts: 22


My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 20 Jul, 2007 - 10:55 AM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


CODE

   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.
User is offlineProfile CardPM

Go to the top of the page

jadedjeanne
post 20 Jul, 2007 - 12:06 PM
Post #8


New D.I.C Head

*
Joined: 13 Jun, 2007
Posts: 22


My Contributions


QUOTE(Amadeus @ 20 Jul, 2007 - 11:55 AM) *

CODE

   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?

User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 20 Jul, 2007 - 12:09 PM
Post #9


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


Can you post the exact code you are now using?
User is offlineProfile CardPM

Go to the top of the page

jadedjeanne
post 20 Jul, 2007 - 12:20 PM
Post #10


New D.I.C Head

*
Joined: 13 Jun, 2007
Posts: 22


My Contributions


QUOTE(Amadeus @ 20 Jul, 2007 - 01:09 PM) *

Can you post the exact code you are now using?


Here it is:

CODE

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

User is offlineProfile CardPM

Go to the top of the page

zyruz
post 20 Jul, 2007 - 12:46 PM
Post #11


New D.I.C Head

*
Joined: 13 Aug, 2005
Posts: 31


My Contributions


Problem is that you read a the newline.

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

to
CODE
    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 Jul, 2007 - 12:49 PM
User is offlineProfile CardPM

Go to the top of the page

gogole
post 20 Jul, 2007 - 03:35 PM
Post #12


D.I.C Head

Group Icon
Joined: 17 Jul, 2007
Posts: 131



Dream Kudos: 25
My Contributions


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



User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/23/08 06:34AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month