Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2011
    Location
    London
    Posts
    31
    Thanks
    5
    Thanked 1 Time in 1 Post

    Why wont this input-output code work in C

    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.

    Code:
    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

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,297
    Thanks
    10
    Thanked 282 Times in 281 Posts
    Code:
    			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:
    Code:
            char *me = "Arthur Taggart";
    	char name[80]; /* fixed size of 80 here is not perfect, but for now will do */
    Dave


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •