Whats wrong with my code i cant use scanf in vscode [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
#include <stdio.h>
int main()
{
int ab;
printf("Whats 5+5:");
int scanf("%d", int ab);
printf("\nbla,bla,blabla,bla,blabla,bla,bla");
if (ab == 10) {
printf("Correct anwser!!!");
}
else {
printf("Incorrect anwser!!!");
}
//it says that the scanf isnt compatible with the declaration and excpected type
//specifier
return 0;
}

int scanf("%d", int ab);
the parameters beyond the 'format string' has to be address of where the input data is to be stored. The function returns EOF or the number of successful input conversions. suggest:
if( 1 != scanf("%d", &ab) )
{
fprintf( stderr, "scanf failed\n" );
exit( EXIT_FAILURE );
}

It's because you forgot to put '&' int the scanf, which signals where your input goes. You don't have to state the 'int' part again. You just have to write:
scanf("%d",& ab);
That's it. It's just a simple distraction error.

Related

'else' without a previous if in c [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am new to C can anyone help me how to fix this code.
I am getting a error:
'else' without a previous if
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20];
char str2[20];
int value;
printf("Enter the string: ");
scanf("%s",str1);
printf("Enter the string: ");
scanf("%s",str2);
value=strcmp(str1,str2);
if (value==0);
printf("The strings are same");
else
{
printf("The strings are not same");
}
return 0;
}
The problem here is that the semicolon after the if statement
if (value==0);
evaluates to: if value is equal to 0, do nothing.
The following line
printf("The strings are same");
not only would get executed every time, but also breaks the if-else apart so that the following else statement does not find any related if statement.
You could use braces here instead to prevent such problems in the future.
if (value == 0) {
printf("The strings are same");
} else {
printf("The strings are not same");
}

Please tell me what's wrong with this code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
#include<stdio.h>
void main()
{
int i=1,s=0;
do{
s+=i;
}while(i<=10);
printf("sum is %d",s);
}
this code is not giving any output, please tell me what is wrong.
You have an infinite loop because the variable i that is used in the condition of the do-while loop is not being changed within the loop. It stays equal to 1 as it was initialized.
do{
s+=i;
}while(i<=10);
It seems you mean
do{
s += i++;
} while( i <= 10 );
or
do{
s += i;
} while( ++i <= 10 );
Pay attention to that according to the C Standard the function main without parameters shall be declared like
int main( void )
Also it is desirable to flush the output buffer by including the new line character '\n' in the output stream like
printf( "sum is %d\n", s );
you didn't increment i;
do
{
s+=i;
i++;
}while(i<=10);

Why my simple C code to calculate factorial not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Following code returns correctly if I enter ZERO or NEGATIVE value. But, if I enter any positive value it does nothing.
Can someone explain why? My expectation is, it should return factorial of positive number.
#include<stdio.h>
int functionfact(int);
void main()
{
int x,fact;
printf("Input an integer value:\n");
scanf("%d",&x);
if (x<0)
printf("Please enter positive value!!");
else if (x==0)
printf ("The factorial of 0 is 1");
else
{
fact=functionfact(x);
printf("The factorial of %d is %d",x,fact);
}
}
int functionfact(int n)
{
return(n*functionfact(n-1));
}
I've upvoted Kalbi's proposal, but I would write it as:
int functionfact(int n)
{
if (n > 0) {
return n * functionfact(n-1);
} else {
return 1;
}
}
Let's face it: this question is asked by a beginner who is learning about the basics of recursion. Best is to first give a complete understanding of how to deal with such kind of programming (as we all struggled with it before, recursion is not easy), and only afterwards, let's make some typical C oneliners.
As Eric pointed out, there is no way for the function to now when to stop.
It should work if you just change the function to
int functionfact(int n)
{
return n > 0 ? (n*functionfact(n-1)) : 1;
}
Hello Dear!
There is not any termination condition for function functionfact. You can try this...
int functionfact(int n)
{
if(n>1)
return(n*functionfact(n-1));
}
I hope u got your answer.

My c program is not giving any result.pls help everyone [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
i have written a program which is not giving proper result.
main()
{
int i=1,n,s=1;
printf("enter the value of n");
scanf("%d",&n);
while(i<=n)
{
s=s*i;
i++;
if (i==n+1)
{
break;
}
}
printf("factorial of n=",s);
}
it is giving the result as shown in the picture below.
Your problem is in this line:
printf("factorial of n=",s);
This outputs factorial of n=, but it does not simply concatenate the value of s, and there is no placeholder for s, so you actually have too many parameters.
You need a placeholder for the int output:
printf("factorial of n=%d",s);
Without it, your program exits with an error (status 15, when 0 would be normal).
Also, (as Vlad pointed out in his answer) the if (i==n+1) { ... } block is redundant, as your while loop will already exit when i > n.
Write
printf("factorial of n=%d\n",s);
^^
And this code snippet
if (i==n+1)
{
break;
}
is redundant and may be removed.
You could write the loop simpler. For example
while ( n > 1 ) s *= n--;
without a need to use one more variable i.

I get the error "expected ‘;’ before ‘!’ token" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
int menu () {
char choice [5];
int i;
int c;
printf("Welcome to your own personal tamaguchi!");
printf("\n 1.Name your tamaguchi.\n");
printf("\n 2.Check health and age.\n");
printf("\n 3.Feed tamaguchi.\n");
printf("\n 4. Exercise with tamaguchi.\n");
printf("Let tamaguchi sleep.n");
printf("\n 5. Close program.\n");
printf("Choose action: ");
scanf("%s", choice);
printf("\n");
for (i=0; choice[i]! = '\0'; i++){
if(!isdigit(choice[i]))
return -1;
}
c = atoi(choice);
return c;
}
They say the problem lies where ! is where choice[i]!='\0'.
I have included stdio, string, time and stdlib.
I don't know what I've done wrong, if you can see the mistake please tell me?
Thanks.
You need to change
for (i=0; choice[i]! = '\0'; i++){
to
for (i=0; choice[i] != '\0'; i++){
^
| //notice here
The operator here is not equal to !=. This is a single operator.
If you write like ! = [with a space in between], that becomes two separate operators, Logical NOT and assignment, thereby producing the error.

Resources