Jump to content
xisto Community
Sign in to follow this  
LouisStDubois

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

Recommended Posts

/* 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.

Share this post


Link to post
Share on other sites

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?
Edited by dimumurray (see edit history)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.