...

View Full Version : Why wont this input-output code work in C



Artyboy2011
01-12-2012, 11:28 AM
Hi, im just starting C, and i tried to make a question thing, which basically has a variable with my name and the user puts in a name. if its not the same as mine it says you are not Arthur Taggart.



int main() {
int me;
me = Arthur Taggart;
char name;
printf("Whats your name?");
scanf("%s".&name);
if(name != me){
printf("You are Arthur Taggart");
}else{
if(name = me){
printf("Hello Arthur!");
}
}


It just inst compiling. :(

Thanks

tracknut
01-12-2012, 03:58 PM
if(name != me){
printf("You are Arthur Taggart");
}else{
if(name = me){
printf("Hello Arthur!");


In C, you cannot compare strings with "!=" and "==" (presumably the double-equal is what you wanted rather than assignment on that second if statement). You are comparing the addresses of "me" and "name" with those comparisons, and the addresses of two strings are normally not the same. You need to use the function strcmp() to compare strings.

Also your declarations of 'me' and 'name' are weird. These are both character strings, and you've declared one as an integer, the other as a single character. They should look like:


char *me = "Arthur Taggart";
char name[80]; /* fixed size of 80 here is not perfect, but for now will do */


Dave



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum