Resistor Color Code Calculator - breaking integers to singles

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
 #include <iostream>
#include <cmath>
using namespace 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;
}
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    int n = 15396042 ;
    std::cout << "number: " << n << '\n' ;

    for( int pow = 0 ; n > 0 ; ++pow )
    {
        int digit = n%10 ; // least significant (rightmost) digit
        n /= 10 ; // divide by 10 to remove the rightmost digit
        std::cout << "digit at 10^" << pow << ": " << digit << '\n' ;
    }
}

http://ideone.com/D57IWA
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....
yup, that is possible

the input for example is

RED BRO GRN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <map>
#include <string>
#include <vector>

using namespace std;

int main(){
   int codex[ 4 ];
   map< string, int > colorToNum;
   colorToNum["RED"] = 1;
   colorToNum["GRN"] =  5;
   colorToNum["BRO"] = 3;
   
   string ColorCode;
   for( int i = 0; i < 4; ++ i ){
       cin >> ColorCode;
       codex[i] = colorToNum[ ColorCode ];
   }
}


I haven't try to compile it but it should be something like that
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.
ok, so it's the other way around, I am sorry I didn't read your post right,

1
2
3
4
5
6
7
8
9
10
11
12
13
14

map<char,string> colordb;
colordb['1'] = "GRN";
colordb['2'] = "BRN";
colordb['3'] = "SLV";


string input;
cin >> input;

for( int i = 0; i < input; ++ i ){
    cout << colordb[ input[i] ] << ",";
}


it should be something like that


but wait ...
Do you have to enter the number without space ??
if you don't then make the map key value to int rather than char

and input int instead of string
Topic archived. No new replies allowed.