HOME       >>       Programming

If/else Demo Program Bug if/else demo program bug


LouisStDubois

/* Demonstrates the use of if statement with else clause */#include <stdio.h>int x, y;int main(){ /* Input the two values to be tested */ printf("\nInput an integer value for x: "); scanf("%d", &x); printf("\nInput an integer value for y: "); scanf("%d", &y); /* Test values and print result */ if (x == y) printf("x is equal to y\n"); else if (x > y) printf("x is greater than y\n"); return 0;}

The above source code comes from the Sams Teach Yourself C in 21 Days. I know how ambitious the title sounds, but it seems to be a good book, and I've tried several tutorials. Anyway what the program does is prompt for a value for x and then runs right through the prompt for the value for y and displays "x is greater than y" because it doesn't wait for you to enter the value for y. Can anyone tell me what's wrong with the code that it does that. I have it here letter for letter and can't figure out how to make it wait for the value for y. I've just recently passed the "Hello World!" phase, so bear with me.
Thanks for any help.

dimumurray

OK grasshopper , I'll see what I can do to help.
Lets start with the signature of the main function, depending on your compiler, then instead of:

int main()

that line shoud read as either:
int main(int argc, char* argv[])
OR
void main(int argc, char* argv[])

This issue is probably not the source of your programming woes but some compilers have been known to act iffy when the main function does not have parameters defined between the parentheses or if the return type is not 'void'.

So onto the real heart of the problem, your malformed if-else clause. You need to use curly braces to enclose blocks in your if statement. So your code should read:
if ( x == y) { //<-- opening brace demarks the start of a code block printf("x is equal to y\n");} else if ( x > y ) { //<-- closing brace closes first if block followed by an else if conditional and yet another brace printf("x is greater than y\n");} //<-- final closing brace

Some compilers are pretty forgiving when it comes to using if-else clause, allowing you to omit the braces when an if clause has just one statement in its body. But its good practice to always use the braces. Not only will your compiler be less likely to have hiccups when it parses your code but your code will also be easier for you to read.

*Note: Some C compilers treat the else-if clause as one word so instead of 'else if' you might have 'elseif'

BTW Exactly which compiler are your using?

LouisStDubois

OK grasshopper , I'll see what I can do to help. Lets start with the signature of the main function, depending on your compiler, then instead of:

int main()

that line shoud read as either:
int main(int argc, char* argv[])
OR
void main(int argc, char* argv[])

This issue is probably not the source of your programming woes but some compilers have been known to act iffy when the main function does not have parameters defined between the parentheses or if the return type is not 'void'.

So onto the real heart of the problem, your malformed if-else clause. You need to use curly braces to enclose blocks in your if statement. So your code should read:
if ( x == y) { //<-- opening brace demarks the start of a code block printf("x is equal to y\n");} else if ( x > y ) { //<-- closing brace closes first if block followed by an else if conditional and yet another brace printf("x is greater than y\n");} //<-- final closing brace

Some compilers are pretty forgiving when it comes to using if-else clause, allowing you to omit the braces when an if clause has just one statement in its body. But its good practice to always use the braces. Not only will your compiler be less likely to have hiccups when it parses your code but your code will also be easier for you to read.

*Note: Some C compilers treat the else-if clause as one word so instead of 'else if' you might have 'elseif'

BTW Exactly which compiler are your using?

Hey Thanks. I'm using Borland's Turbo C compiler. I think it's version 2.something. The source code came directly out of the book. I've been using Sams Teach Yourself C in Twenty One Days.

dimumurray

Hey Thanks. I'm using Borland's Turbo C compiler. I think it's version 2.something. The source code came directly out of the book. I've been using Sams Teach Yourself C in Twenty One Days.

That's a pretty old compiler. There are lots of better options out there. And many of them are free. You should try DEV C++ or better yet, get the Eclipse IDE For C/C++ Developers. DEV C++ is a decent IDE(Integrated Development Environment) that is built on top of a GCC-based C/C++ compiler(open-source). It is fairly easy to use, once you get the hang of it. However I highly recommend the ECLIPSE platform. I say 'platform' because it is capable of many functions. It's plug-in framework supports a number of language editors such as JAVA, C/C++, HTML, XML, PHP with compiler support for JAVA and C/C++. The learning curve is somewhat steep for the platform. To use Eclipse effectively you will have to take time to learn how to use their extremely flexible and robust GUI. The link I gave you above gives you a list of several Eclipse IDE implementations. Download the one that specifically has the C/C++ compiler. That way you're less likely to be overwhelmed by feature overload. The cool thing is that when you're ready to add other perspectives you can use the update manager that comes with the IDE. But I'm getting ahead of myself. Try these out and see if they work for you.

LouisStDubois

That's a pretty old compiler. There are lots of better options out there. And many of them are free. You should try DEV C++ or better yet, get the Eclipse IDE For C/C++ Developers. DEV C++ is a decent IDE(Integrated Development Environment) that is built on top of a GCC-based C/C++ compiler(open-source). It is fairly easy to use, once you get the hang of it. However I highly recommend the ECLIPSE platform. I say 'platform' because it is capable of many functions. It's plug-in framework supports a number of language editors such as JAVA, C/C++, HTML, XML, PHP with compiler support for JAVA and C/C++. The learning curve is somewhat steep for the platform. To use Eclipse effectively you will have to take time to learn how to use their extremely flexible and robust GUI. The link I gave you above gives you a list of several Eclipse IDE implementations. Download the one that specifically has the C/C++ compiler. That way you're less likely to be overwhelmed by feature overload. The cool thing is that when you're ready to add other perspectives you can use the update manager that comes with the IDE. But I'm getting ahead of myself. Try these out and see if they work for you.

Thanks again. I just found a tutorial on FriedSpace that was written for Pelles C and I'm downloading the Eclipse IDE right now.


iGuest

If/else Demo Program BugIf/else Demo Program Bug

This problem arise due to the compiler problems.Just use two scanf("%d",&y) statements for reading y.

-reply by srijidh


VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.