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");
}
Related
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 4 years ago.
Improve this question
I am trying to solve leetcode 506. (Relative Ranks problem).
This is the c code not complete yet,and there's a puzzle that output is unexpected.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,j,nums[]={5,4,3,2,1},tmp;
char str[21];
char **ret=(char **)malloc(sizeof(*ret)*5);
for(i=0;i<5;i++) //bubble sort
{
for(j=5-1;j>i;j--)
{
if(nums[i]>nums[j])
{
tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
}
}
}
for(i=0;i<5;i++)
{
if(i<3)
{
if(i==0)
*(ret+i)="Gold Medal";
else if(i==1)
*(ret+i)="Silver Medal";
else
*(ret+i)="Bronze Medal";
}
else
{
sprintf(str,"%d",nums[i]);
*(ret+i)=str;
//printf("%s ",*(ret+i));
}
}
for(i=0;i<5;i++)
printf("%s ",*(ret+i));
}
I think the output should be:
Gold Medal
Silver Medal
Bronze Medal
4
5
but the actual output is:
Gold Medal
Silver Medal
Bronze Medal
5
5
ans:
else
{
char *str=malloc(sizeof(char)*10);
sprintf(str,"%d",nums[i]);
*(ret+i)=str;
//printf("%s ",*(ret+i));
}
You are assigning str twice in your first loop. ret[3] and ret[4] both point to the same memory address. When you output your array, in your second loop, you will therefore get 5 twice, because its value is overwritten in the previous loop.
The statement *(ret+i) = str; does not copy the value of the variable str, but simply points ret+1 to the address of str. The address of str does not change when using sprintf to assign values to its bytes.
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
hello=) with this following code i get the exception
error: expected ‘;’ before ‘printf’
#include <stdlib.h>
#include <stdio.h>
int main() {
int i;
scanf("%i", &i);
for(int i=0 ; i<10; i++){
if(i==1) printf("one");
else if(i==2) printf("two");
else if(i==3) printf("three");
else if(i==4)printf("four");
else if(i==5)printf("five");
else if(i==6) printf("six");
else if(i==7) printf("seven");
else if(i==8)printf("eight");
else(i>9) printf("even"+"/n"+"odd");
}
return 0;
}
Can i sum up this code into a sorter form ? And why do i get this exeption?
thank you all
Add some brackets "{}".
Code will looks better and will work.
if(expression) {
} elseif(expression) {
} elseif(expression) {
} else {
}
PS. I know it will better. If this text be comment. But I don't have points reputation yet :(
PS2. #Myst if code will have 9. Code don't printf anything.
else clause can't have a condition. So, you either mean just else or else if (i>9).
Besides, C doesn't have + operator that concatenates strings. You can just leave out the + and the C preprocessor will concatenate adjacent string literals (see C11, 5.1.1.2, 6).
else if(i>9) printf("even" "/n" "odd");
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
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char name[10]; int siblings;
printf("enter the name\n");
scanf("%s",name);
if (strcmp(name,"larry")!=0)
{
printf("you are not larry");
}
else
{
printf("you are larry\n");
printf("how many siblings do you have\n");
scanf("%d",siblings);
fflush(stdin);
printf("you have %d siblings Mr.%s\n",siblings,name);
}
getchar();
return 0;
}
/This is the program running well till it prints "how many siblings do you have" and then stops working/
The code scanf("%s",name); is okay because it is a string but, this one is not because it is not a string scanf("%d",siblings);. You will learn more about it when you reach pointers and why you can just you can just remove the & sign for char[] or with arrays. That is a runtime error by the way. That is why you get the error while the program is already running.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
#include<stdio.h>
#include<string.h>
int main()
{
char string1[100],string2[100];
int count,i=0,j=0;
gets(string1);
gets(string2);
for(i=0;i<strlen(string1)-1;i++)
{
for(j=strlen(string2)-1;j<0;j--)
{
if(string1[i]==string2[j])
{
printf("They are reverse of each other");
}
else
printf("They are not");
}
}
}
/* I am trying to check if ABC and CBA are equal by checking the first index of string1 and last index of string2 and incrementing and decrementing resp. */
Try this code, it is used check both stings are reverse to each other or not.
#include<stdio.h>
#include<string.h>
int main()
{
char s1[100],s2[100];
int count,i=0,j=0, flag=1;
gets(s1);
gets(s2);
int l1=strlen(s1), l2=strlen(s2);
if(l1==l2)
{
l2--;
for(i=0;i<l1;i++)
{
if(s1[i]!=s2[l2-i])
{
flag=0;
break;
}
}
if(flag)
printf("Both are reverse to each other\n");
else
printf("Not revrese to each other\n");
}
else
printf("Not revrese to each other\n");
}