Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Strings 2

Status
Not open for further replies.

RavUnRex

Programmer
Oct 24, 2005
8
US
Code:
#include <stdafx.h>
#include <stdio.h>
#define STRING 8
#define error -1

int
main(void)
{
	char COLOR_CODES[10][8] = {"black", "brown", "red",
		"orange", "yellow", "green", "blue", "violet", "gray",
		"white"};
	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);

	[b]if (color1 = "black")
		a = 1;
	if (color1 = "brown")
		a = 2;[/b]

	printf("\n%f", &a);

	
	return (0);
}

I don't really know what I'm doing, but I'm trying to test to see if color1 equals certain colors. But I get an error when I try. Why can't I type if (color1 = "brown") a = 2;, etc.??? And how can I do that?
 
In C, strings aren't so much of a datatype as a convention. When you try (color1 = "brown"), you're trying to numerically compare two arrays of type char.

To compare strings, try using the standard function strcmp() instead.

: Daniel :

-
 
You should IMO, use brackets aswell - like so :

Code:
    if (strcmp(color1, "black")) {
        a = 1;
    } else if (strcmp(color1, "brown")) {
        a = 2;
    } else {
        a = -1;
    }

aswell as handling the case where no option is select, so you should set 'a' to a default value - else you will run into problems because 'a' is not actuall initialized.

Also, in your printf statement, you are trying to print out a float ("%f") - but the actual value is an int - this will not result in your expected behaviour. And finally, why are you printing out the pointer value of the 'a' var ? I don't think that is what you want to be doing ... use instead :

Code:
printf("\n%d\n", a);

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
GREAT! I've got that portion of the program working now.

Time to move on to the next segment :banghead: Thanks for the help!!
 
I might suggest making some sort of function like this.
Code:
#include <stdio.h>
#include <string.h>

[blue]int color_value(const char *color)
{
   static const char color_code[][8] =
   {
      /* 0 */ "black",
      /* 1 */ "brown",
      /* 2 */ "red",
      /* 3 */ "orange",
      /* 4 */ "yellow",
      /* 5 */ "green",
      /* 6 */ "blue",
      /* 7 */ "violet",
      /* 8 */ "gray",
      /* 9 */ "white"
   };
   int i;
   for ( i = 0; i < sizeof color_code / sizeof *color_code; ++i )
   {
      if ( strcmp(color, color_code[i]) == 0 )
      {
         return i;
      }
   }
   return -1;
}[/blue]

int main(void)
{
   static const char *mycolor[] =
   {
      "black", "blue", "muave", "orange",
   };
   size_t i;
   for ( i = 0; i < sizeof mycolor / sizeof *mycolor; ++i )
   {
      printf("color_value(\"%s\") = %d\n", 
             mycolor[i], color_value(mycolor[i]));
   }
   return 0;
}

/* my output
color_value("black") = 0
color_value("blue") = 6
color_value("muave") = -1
color_value("orange") = 3
*/
 
here's what I got... But I gotta incorporate a while loop so the user can decide whether to run this more than once or not. I wrote a do-while loop, but it's not working... the do-while is in bold:





Code:
#include <stdafx.h>
#include <stdio.h>
#include <string.h>
#define STRING 8
#define error -1

int
main(void)
{
	char COLOR_CODES[10][8] = {"black", "brown", "red",
		"orange", "yellow", "green", "blue", "violet", "gray",
		"white"};
	char color1[STRING],
		color2[STRING],
		color3[STRING];
	       
	[b]int holder;
           holder = 0;
	
	do {[/b]

	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);

	/*	for (i=0; i<=9;i++)
	 *		{if (color1 == COLOR_CODES[i])
	 *			a = i;
	 *		}
	 */
	
	int a;
	
	if (strcmp(color1, "black") == 0) {
        a = 0;
    } else if (strcmp(color1, "brown") == 0) {
        a = 1;
    } else if (strcmp(color1, "red") == 0)	{
        a = 2;
	} else if (strcmp(color1, "orange") == 0) {
		a = 3;
	} else if (strcmp(color1, "yellow") == 0) {
		a = 4;
	} else if (strcmp(color1, "green") == 0) {
		a = 5;
	} else if (strcmp(color1, "blue") == 0) {
		a = 6;
	} else if (strcmp(color1, "violet") == 0) {
		a = 7;
	} else if (strcmp(color1, "gray") == 0) {
		a = 8;
	} else if (strcmp(color1, "white") == 0) {
		a = 9;
	}
	
	int b;

	if (strcmp(color2, "black") == 0) {
        b = 0;
    } else if (strcmp(color2, "brown") == 0) {
        b = 1;
    } else if (strcmp(color2, "red") == 0)	{
        b = 2;
	} else if (strcmp(color2, "orange") == 0) {
		b = 3;
	} else if (strcmp(color2, "yellow") == 0) {
		b = 4;
	} else if (strcmp(color2, "green") == 0) {
		b = 5;
	} else if (strcmp(color2, "blue") == 0) {
		b = 6;
	} else if (strcmp(color2, "violet") == 0) {
		b = 7;
	} else if (strcmp(color2, "gray") == 0) {
		b = 8;
	} else if (strcmp(color2, "white") == 0) {
		b = 9;
	}

	int c;

	if (strcmp(color3, "black") == 0) {
        c = 0;
    } else if (strcmp(color3, "brown") == 0) {
        c = 1;
    } else if (strcmp(color3, "red") == 0)	{
        c = 2;
	} else if (strcmp(color3, "orange") == 0) {
		c = 3;
	} else if (strcmp(color3, "yellow") == 0) {
		c = 4;
	} else if (strcmp(color3, "green") == 0) {
		c = 5;
	} else if (strcmp(color3, "blue") == 0) {
		c = 6;
	} else if (strcmp(color3, "violet") == 0) {
		c = 7;
	} else if (strcmp(color3, "gray") == 0) {
		c = 8;
	} else if (strcmp(color3, "white") == 0) {
		c = 9;
	}
	
	printf("\nResistance value: %d%dx10^%d ohms\n", a, b, c);
		printf("\n\nWould you like to decode another resistor? 0 for yes, 1 for no=> ");
		[b]do
			scanf("%d", holder);
		while (holder != 1);
	} while (0);[/b]
	return (0);
}
 
Code:
do {

xxxx

} while ( 0 );

will run exactly once: 0 is false so the loop terminates.

A more conventional do forever is:

Code:
while ( 1 )
{
xxxxxx
}
 
I've got the string functions working I just can't get the loop to work...
I've even tried to make it so it prompts the user how many times they want to decode the resistors, then do it that many times, but that still won't work... I get an error after I enter the number of times... here's the code:
Code:
#include <stdafx.h>
#include <stdio.h>
#include <string.h>
#define STRING 8
#define error -1

int
main(void)
{
	char COLOR_CODES[10][8] = {"black", "brown", "red",
		"orange", "yellow", "green", "blue", "violet", "gray",
		"white"};
	char color1[STRING],
		color2[STRING],
		color3[STRING];	       
	int i, counter, a, b, c;
	counter = 0;

	printf("How many resistors do you want to decode? => ");
		scanf("%d", counter);
    for (counter = 0;
		counter < i;
		counter += 1){

	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);

	int a;
	
	if (strcmp(color1, "black") == 0) {
        a = 0;
    } else if (strcmp(color1, "brown") == 0) {
        a = 1;
    } else if (strcmp(color1, "red") == 0)	{
        a = 2;
	} else if (strcmp(color1, "orange") == 0) {
		a = 3;
	} else if (strcmp(color1, "yellow") == 0) {
		a = 4;
	} else if (strcmp(color1, "green") == 0) {
		a = 5;
	} else if (strcmp(color1, "blue") == 0) {
		a = 6;
	} else if (strcmp(color1, "violet") == 0) {
		a = 7;
	} else if (strcmp(color1, "gray") == 0) {
		a = 8;
	} else if (strcmp(color1, "white") == 0) {
		a = 9;
	}
	
	int b;

	if (strcmp(color2, "black") == 0) {
        b = 0;
    } else if (strcmp(color2, "brown") == 0) {
        b = 1;
    } else if (strcmp(color2, "red") == 0)	{
        b = 2;
	} else if (strcmp(color2, "orange") == 0) {
		b = 3;
	} else if (strcmp(color2, "yellow") == 0) {
		b = 4;
	} else if (strcmp(color2, "green") == 0) {
		b = 5;
	} else if (strcmp(color2, "blue") == 0) {
		b = 6;
	} else if (strcmp(color2, "violet") == 0) {
		b = 7;
	} else if (strcmp(color2, "gray") == 0) {
		b = 8;
	} else if (strcmp(color2, "white") == 0) {
		b = 9;
	}

	int c;

	if (strcmp(color3, "black") == 0) {
        c = 0;
    } else if (strcmp(color3, "brown") == 0) {
        c = 1;
    } else if (strcmp(color3, "red") == 0)	{
        c = 2;
	} else if (strcmp(color3, "orange") == 0) {
		c = 3;
	} else if (strcmp(color3, "yellow") == 0) {
		c = 4;
	} else if (strcmp(color3, "green") == 0) {
		c = 5;
	} else if (strcmp(color3, "blue") == 0) {
		c = 6;
	} else if (strcmp(color3, "violet") == 0) {
		c = 7;
	} else if (strcmp(color3, "gray") == 0) {
		c = 8;
	} else if (strcmp(color3, "white") == 0) {
		c = 9;
	}
		}
	printf("\nResistance value: %d%dx10^%d ohms\n", a, b, c);
		return (0);
}
 
ok.. I made scanf("%d", counter) changed to scanf("%d", i) but still no go.
 
In order for scanf() to change the counter variable, you must pass a pointer to the counter with the & operator. Just giving 'counter' to the function passes the value of counter.


-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top