Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

New to C Programming


ghost's Avatar
0 0

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


ghost's Avatar
0 0

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();
)



n3w7yp3's Avatar
Member
0 0

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


ghost's Avatar
0 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!


ghost's Avatar
0 0

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


ghost's Avatar
0 0

Before you return from main..


ghost's Avatar
0 0

My solution is:

#include <stdio.h>

int main(_)
{	
char out[010];
scanf("%d",&_);
strcpy(out,(((10-_)>0)?"Smaller":"Greater"));
printf("%s than ten.\n",out);
return 00;
}

ghost's Avatar
0 0

@ 0X702CH…i want my code to stay pretty much the same and you changed lots of things, and most of em i havent learned yet, so can you do it with the stuff that i've got so far, or does it have to include what you just showed me? end3r


ghost's Avatar
0 0

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

ghost's Avatar
0 0

No it don't have, this is my very-own-only-for-educational-reasons alternative solution to your, ehm problem. And if you wanted your code to stay as it was…


ghost's Avatar
0 0

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


ghost's Avatar
0 0

forget it, none of yall understand that im brand new, ill do it later.


ghost's Avatar
0 0

I understand that you're brand new, but how much more simplistic can you get than an if-else statement? Perhaps if you don't understand you should re-read your book or tutorial whichever the case may be.


ghost's Avatar
0 0

i understand if-else, what i dont understand is inside your main () where you said:> int argc, char **argv end3r


n3w7yp3's Avatar
Member
0 0

That defines the array argv, which containts command line arguments.


ghost's Avatar
0 0

Protokol:

  • Undefined types are automatically treated as int
  • How you come to say that _ is stupid name for a variable? It is legal.
  • I don't know what is your problem with the array.
  • And in the original program end3r did'nt took care about that 10 isn't greater than 10.

ghost's Avatar
0 0

For the time being this thread is locked to prevent arguments.

You can continue this debate some where else.