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

Code works fine w/o telling user invalid color choice try again. how d

Page 1 of 1

3 Replies - 2736 Views - Last Post: 09 April 2008 - 05:29 PM Rate Topic: -----

#1 cmm5512  Icon User is offline

  • New D.I.C Head

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

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

Post icon  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  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 cmm5512  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 jeronimo0d0a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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. :ph34r:


#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;
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1