09-4-2013, 08:30 PM | #1 |
Expect delays.
Join Date: Mar 2008
Location: Montreal, QC
Age: 31
Posts: 4,121
|
Coding in C: help with if statements
Hey peeps! Just started getting into C for University. I am experimenting with the language, and so far I came with a little problem. To begin with, here's my code:
Code:
#include <stdio.h> int main (void) { /*Declarations --------------------------------*/ char yourname[30]; /*Program--------------------------------------*/ printf("What's your name, bro? "); scanf("%s", yourname); // I ask the user to write his name if (yourname == "Felix") // If the name entered is Felix, he gets a warm welcome. { printf("Hello master %s, king of the universe.\n", yourname); } else // If the name entered is anything other than Felix, the program doesn't recognize you. { printf("I don't know you\n"); } } Weirdly enough, when I try to make a similar program using numbers this time around (integers, to be exact), it works like a charm. This is what makes me wonder why it doesn't work when I type letters. Here's an example: Code:
#include <stdio.h> int main (void) { /*Declarations --------------------------------*/ int yourage; /*Program--------------------------------------*/ printf("What's your age, bro? "); scanf("%d", &yourage); // I ask the user to write his age, this time. if (yourage == 20) // If the name entered is 20 years old, he gets a warm welcome. { printf("Hello my master, king of the universe.\n"); } else // If the age is anything other than 20, the program doesn't recognize you. { printf("I don't know you\n"); } } |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|