# Resistor Code Calculator Help! basic problem done just need help w

Page 1 of 1

## 3 Replies - 3230 Views - Last Post: 09 April 2008 - 05:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=48646&amp;s=903a2332f84a4aa9d8324a8fb43f26f8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cmm5512

Reputation: 0
• Posts: 2
• Joined: 08-April 08

# Resistor Code Calculator Help! basic problem done just need help w

Posted 09 April 2008 - 12:40 PM

```
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
string x;
double band[2];
char ans;
int main ()
{
string colorcode[10]={"black", "brown", "red",
"orange", "yellow", "green", "blue", "violet", "grey", "white"};
do
{
cout<<"Enter the colors of the resistor's three bands,"<<endl<<"beginning with the band nearest the end."<<
endl<<"Type the colors in lowercase letters only, NO CAPS. "<<endl<<endl;
for (int i=0; i<3; i++)
{
cout<<"Enter color for Band "<<i+1<<" => ";
cin>>x;

while(x!="black" &&  x!="brown" &&  x!="red" && x!=
"orange"&& x!= "yellow"&&  x!="green"&&  x!="blue"&&  x!="violet"&& x!= "grey"&& x!= "white");		  //THIS IS THE PART I NEED HELP ON
{					cout<<"Invalid color. Try again."<<endl;   //AND HERE TOO
cout<<"Enter color for Band "<<i+1<<" => ";
cin>>x;
}
for(int j=0; j<10; j++)
{
if(x==colorcode[j])
band[i]=j;
}
}
int R= (10*band[0]+band[1])*pow(10.0,band[2]);
cout<<"Resistance value is "<<R<<" Ohms"<<endl;
cout<<"Do you want to decode another resistor (y/n)? "<<endl;
cin>>ans;
}
while(ans!='n');
[color=#333399]
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Resistor Code Calculator Help! basic problem done just need help w

### #2 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Resistor Code Calculator Help! basic problem done just need help w

Posted 09 April 2008 - 12:54 PM

please describe your problem IN you post not it the title or description since those are limited fields.

### #3 cmm5512

Reputation: 0
• Posts: 2
• Joined: 08-April 08

## Re: Resistor Code Calculator Help! basic problem done just need help w

Posted 09 April 2008 - 04:17 PM

Oh okay. this is my first time posting anything. i didn't know that it didn't show. okay well i have the basic code working but i can't get it to work when the user enters a wrong color. I used a while loop to compare from the users input.

### #4 jeronimo0d0a

Reputation: 1
• Posts: 146
• Joined: 03-March 08

## Re: Resistor Code Calculator Help! basic problem done just need help w

Posted 09 April 2008 - 05:29 PM

I thought it was cleaner not to give the user choices.
Make them pick the color from a list.

```#include <iostream>
#include <string>
#include <cmath>
#include <conio.h>

using namespace std;
string x;
double band[2];
char ans;
int main ()
{
string colorcode[10]={"black", "brown", "red",
"orange", "yellow", "green", "blue", "violet", "grey", "white"};
do
{
//cout<<"Enter the number of the colors of the resistor's three bands,"
//<<endl<<"beginning with the band nearest the end."
//<<endl<<"Type the colors in lowercase letters only, NO CAPS. "
//<<endl<<endl;
for (int i=0; i<3; i++) // get three bands
{
for(int j=0; j<10; j++) // list the colors for the user
{ cout << j << " = " << colorcode[j] << "\n"; }

cout<<"Enter number of the color for Band "<<i+1<<" => ";
//cin>>x;
ans = -1;
while( (ans<'0') || (ans>'9') ) // ignoring all but the listed choices
{ ans=_getch(); }
cout << ans << "\n";
band[i] = ans-'0';
}
// pow returns a double
double R= (10*band[0]+band[1])*pow(10.0,band[2]);
cout<<"Resistance value is "<<R<<" Ohms"<<endl;
cout<<"Do you want to decode another resistor (y/n)? "<<endl;
ans = 0;
while( (ans !='y') && (ans!='n') ) // only two choices here
{ ans=_getch(); }
if(ans == 'y') { continue; } else { break; }

} while( 1 );

return 0;
}

```