LouisStDubois
-
Content Count
3 -
Joined
-
Last visited
Posts posted by 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[])
ORvoid 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.
-
/* 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.
If/else Demo Program Bug if/else demo program bug
in Programming
Posted · Report reply
Thanks again. I just found a tutorial on FriedSpace that was written for Pelles C and I'm downloading the Eclipse IDE right now.