Hello, I have a program to do about making a Resistor Color Code Calculator by entering the resistance value as an integer then picking the tolerance from a list from 1 to 8, I think till now I have done half of it.
QUESTION: Can someone tell me how can I break the resistance value to single? For example 9800 breaks to 9 8 0 0 and then depending on each digit a color is printed also how can I make it exit when a negative value is entered. thanks.
#include <iostream>
#include <cmath>
usingnamespace std;
int main() {
double i;
char t;
cout << "Welcome to my Resistor Color Calculator. \n You will enter a resistance as an integer and then choose a tolerance.\n "
<< endl;
cout<< "Do not enter any characters such as k or M and do not type the word 'ohm'. \n Enter a negative resistance to exit the program"<< endl;
cout<<"Please enter Resistance here"<<endl;
cin>>i;
cout<<"Pick a Tolorence"<<endl;
cout<<"1: 1%"<<endl;
cout<<"2: 2%"<<endl;
cout<<"3: 0.5%"<<endl;
cout<<"4: 0.25%"<<endl;
cout<<"5: 0.10%"<<endl;
cout<<"6: 0.05%"<<endl;
cout<<"7: 5%"<<endl;
cout<<"8: 10%" <<endl<<endl;
cout<<"Pick Tolorence : ";
for (i > 0 ;;)
{
cin>>t;
switch (t)
{
case'1':
cout << "BRO \n"; break;
case'2':
cout << "RED \n"; break;
case'3':
cout << "GRN \n"; break;
case'4':
cout << "BLU \n"; break;
case'5':
cout << "VIO \n"; break;
case'6':
cout << "GRY \n"; break;
case'7':
cout << "GLD \n"; break;
case'8':
cout << "SLV \n"; break;
}}
return 0;
}
There are multiple ways, but I think the easiest is to enter the resistance as a string. If you really want to enter it as integer, transform it to a string. If you have it as a string you can get each character at a time.
The other option is to keep dividing by ten, and show the reminder.
9800%10=0 9800/10=980
980%10=0 980/10=98
98%10=8 98/10=9
9%10=9 9/10=0 (stop here)
I am supposed to enter it as an integer, and I dont know what is a string and how to transform to it. But I searched about the modulo % , but I didnt understand how to use it in this case, all examples I found were on odd and even numbers.
oh great thanks, but how can I make it "cout<<" for every digit a color? like saying
after the number is entered : RED,BRO,GRN,SLV....etc
instead of: 1 5 3 9....
Sorry, I think I didn't make my self clear, what I want to do is when I enter a number e.g. 1234, the program will break this number to 1 , 2, 3, 4 and for that it will show four colors depending on what is each digit number is ==> for 1 it says RED, for 2 it says GRN, for 3 it says BLU, for 4 it says SLV so in the end i will have a sentence saying : RED,GRN,BLU,SLV (which is 1234) and if i enter 3412 it will say BLU,SLV,RED,GRN, so do anyone understand what I am saying? and finally if I enter any negative number, the whole program will exit.