Click here to Skip to main content
15,888,116 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
C
#include<stdio.h>

int main ()
{
	int c,n,fact = 1;
	printf("Enter a number to calculate it's factorial\n");
	scanf("%d", &n);
	for (c=1; c <= n; c++);
	fact = fact *c;
	printf("The factorial of %d	= %d", n, fact);
	
	printf("Do you want to continue?\n");
	scanf("%c", &(char){ch} );
	if (ch == 'y' || ch == 'Y')
	continue;
	else
	break;
	return(0);
	
}


In the above program I have declared ch as char within scanf but it is not working can any one help.
Posted
Updated 3-Feb-16 6:07am
v2

Yeah, you can't do that. This is C, not java.

ch has to be declared outside of a function call.

Besides, why would you want to declare a variable inside the scanf call? It's scope, if this worked at all, would be limited to only the code inside the braces in the scanf call. The variable wouldn't exist outside of that call and you couldn't be used in the if statement immediately after that.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 3-Feb-16 12:23pm    
Sure, a 5.
—SA
gschahal 3-Feb-16 12:26pm    
SO that means there is no other way in C to declare variable? We will have to define it at the top
Dave Kreskowiak 3-Feb-16 15:19pm    
It doesn't have to be at the top. It just has to be before you use the variable. You can put the declaration immediately before the call to scanf for all the compiler cares.
Um...what did you expect that to do?
C#
scanf("%c", &(char){ch} );
if (ch == 'y' || ch == 'Y')
   continue;
That doesn't "declare" anything - and if it did (which it can't, it's not allowed by the C syntax), it wouldn't logically be available outside the call to scanf. So the subsequent line wouldn't be able to access it anyway.
And the semicolon at the end of the for loop doesn't help either...
Try this instead:
C#
int main ()
{
    int c,n,fact = 1;
    char ch[100];
    printf("Enter a number to calculate it's factorial\n");
...
    scanf("%c", ch );
    if (ch[0] == 'y' || ch[0] == 'Y')
    {
        continue;
    }
    else
    {
        break;
    }
    return(0);
}
Except you'll need a loop round most of it for the continue / break logic to work!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Feb-16 12:24pm    
Sure, explained more clearly, a 5.
—SA
gschahal 3-Feb-16 12:39pm    
WHat is a 5?
Sergey Alexandrovich Kryukov 3-Feb-16 12:45pm    
Excellent mark.
—SA
gschahal 3-Feb-16 12:32pm    
#include<stdio.h>

int main ()

{
int c,n,fact = 1;
char ch;

while(1)
{

printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);
for (c=1; c <= n; c++);
fact = fact *c;
printf("The factorial of %d = %d", n, fact);

printf("Do you want to continue?\n");
scanf("%c", &ch);
if (ch == 'y' || ch == 'Y')
continue;
else
break;
}
return(0);
}

I tried changing it to the above one. But when I tried as per your solution it didn't worked
OriginalGriff 3-Feb-16 12:42pm    
Read what I said: you haven't fixed both of the other two problems, and you didn't type what I did anyway!
And please - indent your code! It will make your life a whole lot easier... :laugh:
And as a beginner, always - but always - use curly brackets to "delimit" a block, even if it's only one line. It may seem like more work, but it can save you a lot of hair pulling when you make the inevitable mistake...
The way you made it suffer your exquisite programming language fills me with the urge to...
Try:
C
#include<stdio.h>

int main ()
{
	int c,n,fact = 1;
	char ch;
	do
	{
		printf("Enter a number to calculate it's factorial\n");
		while (scanf("%d", &n) != 1)
		{
			printf("please try again\n");
			fflush(stdin);
		}
		fflush(stdin);
		for (c=1; c <= n; c++)
		{
			fact = fact * c;
		}

		printf("The factorial of %d	is %d\n", n, fact);
	
		printf("Do you want to continue?\n");
		scanf("%c", &ch);
	}  while (ch == 'Y' || ch == 'y');
	return 0;
}
 
Share this answer
 
Comments
Aescleal 3-Feb-16 16:03pm    
Have 5 for the allusion.
CPallini 4-Feb-16 11:48am    
Thank you. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900