Reputation: 117
Im making a weather man code but when i ask for the temperature it ends the program. here's the code.
#include <stdio.h>
int main(void)
{
int a;
int b;
int c;
int d;
printf("What Is Your Name?\n");
scanf("%s",&b);
printf("Hi %s\n", &b);
sleep(1);
printf("Im Your Own Weather Man\n");
sleep(1);
printf("Isn't That Great!\n");
sleep(1);
printf("Please Type A Number In\n");
sleep(1);
scanf("%d",&a);
//checking out the number you wrote
if(a<3)
{
printf("Its A Beatiful Day\n");
printf("%c\n",2);
}
if(a>3)
{
printf(":(\n");
}
if(a==3)
{
printf("Its Snowing!\n");
printf("%c\n",3);
sleep(5);
printf("Would you like to know the temprature?\n");
scanf("%s", &c);
}
if(c=='yes')
printf("20F\n");
return 0;
}
I think its messing up on if(c=='yes')
how would i make it to print out the 20 F?
[EDIT]Thanks for helping me out the new code is right below feel free to use it when you want but give credit to me.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
int a,b;
char c[5];
char d[30];
printf("What Is Your Name?\n");
scanf("%s",d);
printf("Hi %s\n", d);
sleep(1);
printf("Im Your Own Weather Man\n");
sleep(1);
printf("Isn't That Great!\n");
sleep(1);
printf("Please Type A Number In\n");
scanf("%d",&a);
//checking out the number you wrote
if(a<3)
{
printf("Its A Beatiful Day\n");
printf("%c\n",2);
}
if(a>3)
{
printf("Its A Cloudy Day");
printf(":(\n");
}
if(a==3)
{
printf("Its Snowing!\n");
printf("%c\n",3);
sleep(5);
printf("Would you like to know the temprature?\n");
scanf("%s",c);
}
if(strcmp("yes", c) == 0)
printf("20F\n");
return 0;
}
Upvotes: 1
Views: 150
Reputation: 303
if(c=='yes')
printf("20F\n");
You cannot do this because 'yes'
is a falsely declared character constant, and even if it was a string literal like "yes"
you cannot compare strings directly in c. Use inbuilt library function or write your own function for string comparison/
Upvotes: 1
Reputation: 3239
c
is defined as an int
, but you are treating it like a char*
.
You should also use strcmp
instead of ==
. You can't compare strings with the ==
operator.
Change your definition of c
to: char c[128]
or some other arbitrary length. Then remove the &
operator when doing scanf: scanf("%s", c);
Finally, change if (c=='yes')
to if (strcmp("yes", c) == 0)
.
Upvotes: 3
Reputation: 1433
The problem lies in that you're reading a string into an int with this line:
scanf("%s", &c);
Try this instead, where the %d
is showing that the input is of type int.
scanf("%d", &c);
Upvotes: 3