Strings and if

This is a discussion on Strings and if within the C Programming forums, part of the General Programming Boards category; Instead of trying to explain what I am attempting to do I'll just post the code: Code: int main() { ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21