C++ string/array help

daroy99

Weaksauce
Joined
Nov 26, 2003
Messages
80
We are working on a project for C++, and it is to take the users input of either (a) number of a gauge on a resistor or (b) the name as in "green" or "yellow" and it converts both ways. We can't set the value of a string to an array, we get a "cannot convert character[6] to int, but for all of them. Here is a piece of the code:
void numberstocolors(string &value)
{
int i(0);
string a;
char ourstring[4]=value;
while (ourstring != '\0')
{
int a;
if (value=="0")
a="black";
if (value=="1")
a="brown";
if (value=="2")
a="red";
if (value=="3")
a="orange";
if (value=="4")
a="yellow";
if (value=="5")
a="green";
if (value=="6")
a="blue";
if (value=="7")
a="violet";
if (value=="8")
a="grey";
if (value=="9")
a="white";
i++
}
}
 
i cant figure out where the error is coming from. i'm assuming from your string comparisons. use strcmp() to compare strings
 
Idon't think you can set a char[] to a string... debug that and check it.
 
First problem is you can't assign a std::string to an array. In fact, you don't even need to in this case. One much cleaner option is to use
Code:
for(int i = 0; i < value.length(); ++i)
instead of
Code:
int i(0);
...
char ourstring[4]=value;
while (ourstring[i] != '\0')
{
...
i++
}

Second, you're redeclaring 'a' to be an int inside the while loop. That's the source of your "cannot convert character[6] to int" error.

Third, all your comparisons inside the loop don't work. You're comparing the entire string "value" with single digits every time through the loop. I think I know what you're trying to do, but I'll just suggest you look up the at() method for std::string as a hint.
 
From the description, I'm not exactly sure what's really needed, but I figure it's something like this:

1. Ask the user to input a string with 3 digits.
2. Ask the user to input a decimal representing the tolerance. (5 would represent +/- 5&#37;. .25 would represent +/- .25%).

(If you're doing resistors with 4 bands that is.)

So, if I input 000 and then enter 5,

black
black
black
gold

would be displayed.

Or, if I input 143 and then enter .25

brown
orange
yellow
blue

would be displayed.

Is that what you need to do?
 
There are two basic mistakes. You have declared the following:

string a; and later in the while loop ... int a;

the variable that is has been declared within a scope gets the precedence. Hence you variable 'a' is in fact an integer and cannot be assigned a string value. Secondly as bassman pointed out, You don't need to use the whole 'if cases'.

If you're trying to select respective values use this code:

int value=0;
string mycolors[] = {"black","brown","red","orange"..........} //place each color at the //respective sequence you want

string input = NULL;
cout<<"Enter the sequence";
cin>>input;
for (int i=0;i<length;i++)
{
value = int(input) // you can also do this by ASCII conversion.
cout<<mycolors[value];
}

The input will consist of the code you want from the user. Such as a sequence of "0125"
The Respective Color bands will be present in the mycolors string array at each respective index. [Its the same thing you were trying to do with the If structure.]
 
Back
Top