#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;
}
Resistor Code Calculator Help! basic problem done just need help wCode works fine w/o telling user invalid color choice try again. how d
Page 1 of 1
3 Replies - 2487 Views - Last Post: 09 April 2008 - 05:29 PM
#1
Resistor Code Calculator Help! basic problem done just need help w
Posted 09 April 2008 - 12:40 PM
Replies To: Resistor Code Calculator Help! basic problem done just need help w
#2
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
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
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.
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;
}
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|