help with strings

This is a discussion on help with strings within the C Programming forums, part of the General Programming Boards category; Hey all. I'm writing a program involving strings. I can get the user to input the information, however, I am ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    6

    help with strings

    Hey all. I'm writing a program involving strings. I can get the user to input the information, however, I am getting stuck when trying to test the user's information.


    I want to know why I can't perform the bolded code? I want to test if the color the user input is equal to the color I type in "quotes".
    Code:
    	char color1[STRING],
    		color2[STRING],
    		color3[STRING];
    	char holder;
    	int a, b, c;
           
    	printf("Enter the colors of the resistor's three bands, beginning with\nthe band nearest the end.  Type the colors in lowercase letters\nonly, NO CAPS.\nBand 1 => ");
    	scanf("%s", color1);
    	printf("\nBand 2 => ");
    	scanf("%s", color2);
    	printf("\nBand 3=> ");
    	scanf("%s", color3);
    
    	if (color1 = "black")
    		a = 1;
    	if (color1 = "brown")
    		a = 2;
    
    	printf("\n%f", &a);
    
    	
    	return (0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - You're using = which is the assignment operator, not == which is used for equality testing.
    2 - You can't compare strings in C using ==, you have to use some of the string functions (or write your own), such as strcmp.
    3 - %f is for floating point numbers, not integers.
    4 - &variable is the address of a variable, which is not what you're trying to print.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char s[100], *p;
    
        printf("\nEnter your password: ");
        fgets(s, sizeof(s), stdin);
    
        if((p=&s[strlen(s)-1]) == '\n') *(p-1) = 0;
    
        if(strcmp(p, "thepassword") == 0) {
            printf("\nPass");
        }
        else {
            printf("\nWrong password");
        }
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Code:
    if((p=&s[strlen(s)-1]) == '\n') *(p-1) = 0;
    Don't you mean *p = 0;?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, I just realized I'd already subtracted 1.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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