I get the error "expected ‘;’ before ‘!’ token" [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 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.

Related

if else inside for loop in c but else statement doesn't work [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 months ago.
Improve this question
for (int i = 1; i < 22; i++){
if(i<=99 && i>=0){
printf("enter an age");
scanf("%d", &ages[i]);
}
else{
printf("enter a valid number");
}
}
When I enter a number outside the if statement such as 999, my program still accepts it instead of printing enter a valid number message. thanks in advance.
When I enter a number outside the if statement such as 999, my program still accepts it instead of printing enter a valid number message. thanks in advance.
I think as the 'i' is the index of the 'ages' array, you are allowing values with a range of 0-99. So, if the value is out oof this range also, it will still be stored in the 'ages' array. It won't display the warning message (enter a valid number) also. Instead, you can try something like this:
for (int i = 0; i < 22; i++){
printf("enter an age: ");
scanf("%d", &ages[i]);
if(ages[i] > 99 || ages[i] < 0){
printf("enter a valid number\n");
i--;
}
}

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

Error in a recursive function to print out a series [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
So, I am supposed to print out the following series using a C program:
I decide to use a recursive approach and come up with this code:
#include<stdio.h>
float series(int n, float x)
{
float prod;
if(n==1)
return 1;
else
{
prod = (x*x)/((2*n-2)(2*n-3)); //line 10
return prod*series(n-1,x);
}
}
int main()
{
int n;
float x;
printf("\n Enter the values of n and x : ");
scanf("%d %f",&n,&x);
printf("\n The series is :");
for(int i=1;i<=n;i++)
printf(" %f,",series(i,x));
printf("\n\n");
return 0;
}
But this gives an error on line 10:
error: called object type 'int' is not a function or function pointer
I don't see any syntactical error on the line. It would be great if you could point it out.
Thank You!
prod = (x*x)/((2*n-2)(2*n-3)); //line 10
should be
prod = (x*x)/((2*n-2)*(2*n-3)); //line 10
The compiler sees this as a function call where 2*n-2 is the function pointer and 2*n-3 is the argument.

C - Warning while compiling simple program [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
Im receiving warnings like this "warning: array subscript has type 'char' [-Wchar-subscripts]" in lines 11, 12 and 15. Whats wrong with my code?
#include <stdio.h>
#include <ctype.h>
int main()
{
char s[1000], i, d, a;
printf("Enter a string: ");
fgets(s, 1000, stdin );
for(i = 0; s[i] != '\0'; i++){
if (isalpha(s[i])){
a++;
}
if (isdigit(s[i])){
d++;
}
}
printf("Number of digits: %d ... Number of letters: %d ", d, a);
return 0;
}

Resources