Thread: Strings and if

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    Strings and if

    Instead of trying to explain what I am attempting to do I'll just post the code:
    Code:
    int main() {
    	char answer [50];
    	char ans1;
    
    	
    	printf("Work out?\n");
    	scanf("%s", answer);
    	
    	if (answer=='Pressure') {
    		printf("worked");
    	}
    	else {
    		printf("nope");
    	}
    As you would know that is a big bowl of fail :P been trolling around the internet trying to find out how to go about if statements and strings and cant find anything (apart from with c++ and that just confuses me even more).

    Would be grateful if anyone could spare some in-sight

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use strcmp and compare to 0.
    Also, single quotes means one character, and double quotes means a string.
    So, 'a' = a char, "a" = a string (char*).

    Also, read up on Scanf woes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed