New to C Programming
can anyone explain the errors to me please because it tells me theres an error before 'int' but i don't know whats wrong. Is it set up correctly to execute more than one statement in the if part? i'm new, so sorry if its sloppy.
int main();
(
int number;
printf("Please enter a number: ");
getchar();
if (number<10) {printf("Your number was less than ten.") scanf("%d", &number); getchar();
}
else {printf("Your number was greater than ten.")
}
getchar();
)
thanks. end3r
I am quite good at C if you PM me or contact me with the contact info on my profile I'll help ya :)
Edit: You put a ( instead of {
Patched code:
#include <stdio.h>
int main();
{
int number;
printf("Please enter a number: ";
getchar();
if (number<10) {printf("Your number was less than ten." scanf("%d", &number); getchar();
}
else {printf("Your number was greater than ten."
}
getchar();
)
You have an unneeded semi-colon after the decleration of main().
#include <stdio.h>
int main(void)
{
int i;
printf("Please enter a number: ");
scanf("%d", &i);
if(i <= 10)
{
printf("Number was less then or equal to 10.\n");
}
else if(i >= 10)
{
printf("Number was greater than or equal to 10.\n");
}
return 0;
}
here is the code OVER commented for your learning pleasure.
#include <stdio.h>
int main()
{ /* you used () here, not {}. use chain brackets */
int number; /* integer variable to compare in if statment */
printf("Please enter a number: ");
scanf("%d",&number); /* gets and converts the character to an actual number */
if (number<=10) /* the if statement */
{
printf("Your number was less than ten. \n"); /* prints if number < 10 */
return(0); /* required to end program */
}
else if (number > 10)
{
printf("Your number was greater than ten. \n"); /* prints if number > 10 */
return(0); /* required to end program */
}
}
/* remember, tidy code helps you to fix bugs and problems!
indent your code and add lines between sections*/
So, use no ; after main() use {} not () for the main function code area and close () in the printf() function
keep tidy code, its easier to fix the screw ups you will definatly have, eveyone does
:ninja: hes disabled! oh noes!
EDIT: im on a new install , so i had to DownLoad GCC to make sure it worked, in the mean time I was beaten to the answer!
thanks for the help, but in each of your guys' codes, it doesnt let me see what the number they entered was. it doesn't wait for me to press enter to end the program, after they enter the number the command prompt just exits without replying "Your number was less than ten" or whatever. so where do i put 'getchar();'. end3r
Protokol, i put 'getchar();' before 'return(0);' in 'main();' but it still didn't work? here's what it looks like…
int main()
{
int number;
printf("Please enter a number: \n");
scanf("%d",&number);
if (number<10)
{
printf("Your number was less than ten\n");
getchar();
return(0);
}
}```
end3r
0X702CH: Your logic is slightly flawed in the fact that 10 isn't greater than 10. You didn't include the string header file, and you never defined the type of _
which is a stupid name for any variable:right: and your array overall is useless.
end3r:
#include <stdio.h>
int main(int argc, char **argv)
{
int nVal;
printf("Enter in a number: ");
scanf("%d", &nVal);
if(nVal - 10 < 0)
printf("Smaller than 10\n");
else if(nVal - 10 > 0)
printf("Greater than 10\n");
else
printf("Equal to 10\n");
getchar();
return 0;
}