I am trying to write a function that relates a color; for example, brown with a number, say 1. so when the user types brown 1 will appear, etc.

int colortonumber (int x)
{

	int nka;

	switch (x)
	{

	case 'brown':
		nka=1;
		break;
	case 'red':
		nka=2;
		break;
	case 'orange':
		nka=3;
		break;
	case 'yellow':
		nka=4;
		break;
	case 'green':
		nka=5;
		break;
	case 'blue':
		nka=6;
		break;
	case 'violet':
		nka=7;
		break;
	case 'gray':
		nka=8;
		break;
	case 'white':
		nka=9;
		break;
	default:
		nka=-1;
	}

	return nka;
}

is there any way possible to have strings in switch statements?

You should start my defining x as a string and then compare it to things likes

"brown"

Please note the double quotes not signal quotes.

Or better yet you should create an enumeration.

enum Colors {brown, red, yellow,...etc};

Edited 6 Years Ago by gerard4143: n/a

No, there is no possible way to switch on a string.

But you aren't even trying to switch on a string. You have x defined as an int. Also, don't confuse single and double quotes. Strings use double, not single quotes.

Perhaps you want something like this?

int colortonumber (string x)
{
    int nka;

    if(x.compare("brown") == 0)
    {
        nka = 1;
    }
    else if (x.compare("red") == 0)
    {
        nka = 2;
    }
    //  more else if statements
    else
    {
        nka = -1;
    }

    return nka;
}

Edited 6 Years Ago by VernonDozier: Made language more precise.

i have mods to my program, but to fully understand what i am doing i think i have to post my entire code. What this program is doing is taking 3 colors. the first two colors serve as the first two numbers of the value. For example brown is equal to 1 so brown and brown make 11. (this part i am good) but the third color corresponds to a multiplier (10^1, 10^2, 10^3, etc.). it works great until i tried incorporating this tidbit. It compiles perfectly but i am getting a runtime error 3, saying 'bcolorM' is being used without being initialized?

i have no clue how to fix it, i have been tinkering with it for the past 5 hours. I am lost, and i need HELP please:

#include <iostream>
using namespace std;
int colortonumber (char x); 
int colortomultiplier (int y);


int main ()
{

	double value=0;
	char bcolor, bcolor1, bcolor2;
	int number, n, flag=0,colorOrSystem,bcolorM,bcolor1m, numbers;

	cout<< "Press 1 to compute the resistor value from the color code.\n";
	cout<< endl;
	cout<< "Press 2 to compute the equivalent resistor value \n";
	cout<< "of a system connected in a series or parallel.\n";
	cin >> colorOrSystem;
	
	if (colorOrSystem  == 1){
	cout << " Enter the first letter of the first,second,and third\n";
	cout << " colors of your band.\n";
	cout << " If it is blue enter a capitalized B, if gray then \n";
	cout << " a capitalized G. For all others just enter in the first\n";
	cout << " letter uncapitalized.-------->";
	cout<< endl;
	cin >> bcolor1 >> bcolor2 >> bcolor1m;
	}

	


	for (n = 0; n <= 3; n++)
	{
		if (n == 0){
			bcolor = bcolor1;
		}
		else if (n == 1){
			bcolor = bcolor2;
		}
		else {
			bcolorM=bcolor1m;
		}
	
		number=colortonumber(bcolor);
		numbers=colortomultiplier(bcolorM);

		if (number, numbers == -1)
		{
			cout << " Wrong entry ";
			flag = 1;
			break;
		}

		if (n = 0){
			value= value + number * 10;
		}
		else if (n = 1){
			value = value + number;
		}
		else{
			value = value *numbers;
		}}

	if (flag == 0){
		cout << " value = " << value;
	}
	cout << "\n";

	return 0;

}

int colortonumber (char x)
{

	int xno;

	switch (x)
	{
	case 'r':
		xno = 2;
		break;
	case 'g':
		xno=5;
		break;
	case 'b':
		xno=1;
		break;
	case 'B':
		xno=6;
		break;
	case 'o':
		xno=3;
		break;
	case 'y':
		xno=4;
		break;
	case 'v':
		xno=7;
		break;
	case 'G':
		xno=8;
		break;
	case 'w':
		xno=9;
		break;
	default:
		xno=-1;
	}

	return xno;
}

int colortomultiplier (int y)
{

	int nka;

	switch (y)
	{
	case 'r':
		nka = 10^2;
		break;
	case 'g':
		nka=10^5;
		break;
	case 'b':
		nka=10^1;
		break;
	case 'B':
		nka=10^6;
		break;
	case 'o':
		nka=10^3;
		break;
	case 'y':
		nka=10^4;
		break;
	case 'v':
		nka=10^7;
		break;
	case 'G':
		nka=10^8;
		break;
	case 'w':
		nka=10^9;
		break;
	default:
		nka=-1;
	}

	return nka;
}

please stay within the parameters of using only switch, if else, for loops, while loops, and do while loops. Nothing to fancy because it is an assignment. (due midnight by the way!)

Damn you Daniweb! Here I had a perfectly good post that I had typed into the reply box, but I wasn't logged in, so I lost it. Why have a reply box when you can't reply?

Anyway, don't have time to rewrite the whole thing, but you're on the wrong track. Get rid of the loop from 0 to 2 in main. You need to either use the pow function from cmath or write your own using integers. The ^ operator is NOT the "raise to a power" operator in C++ so don't use it that way. Use pow (or again, write your own).

pow(10,4) returns 10,000 but you need to typecast to a double. It won't take integers unless you write your own.

In main, your approach should be this:

1) Read in your three characters.
2) Convert them to numbers.
3) Combine the first two numbers into a two digit multiplier.
4) Raise ten to the power of the third number.
5) Multiply the results from steps 3 and 4 to get the resistor value.

No if statements or loops in any of the five steps above. Get rid of the switch statement in colortomultiplier. You already have that code in colortonumber. You can call colortonumber from colortomultipler if you like.

This article has been dead for over six months. Start a new discussion instead.