# Capacitor code

• 09-15-2005
muralemerica
Capacitor code
Im doing the tutorials over at 3dbuzz and my assigment is to create a program that calculates a capacitors resistance based on its colors. My first version of this code did just that, it worked fine and all was good, when I turned it in, the guy said (I used a bunch of switch statements for it) to get rid of all that unecesary code. After a day of pondering I figured out a new method. This works but he also wants the output arranged as 3000 = 3k; 4,500,000 = 4.5M etc.
Code:

```   if (band1 == 0)   {             cout<<"Your resistor has "<<band2;                         for (int increase = 0; increase < band3; increase++)             {                 cout<<"0";             }                         cout<<" Ohms of resistance"<<endl;   }           else   {             cout<<"Your resistor has "<<band1<<band2;                     for (int increase = 0; increase < band3; increase++)             {                 cout<<"0";             }                     cout<<" Ohms of resistance."<<endl;   }```
My question is, Is there any way to keep this set up and still produce it as 4.5M etc instead of all the zeros? Thanks in advance to anyone that can help me.
• 09-15-2005
ZuK
Shure. It's not hard
sth like this

Code:

```if ( resistance >= 1000000 ) { // gte 1M     cout << setprecision(1) << (double)resistance / 1000000 << "M ohms" << endl; } else if ( resistance >= 1000 ) { //gte 1k     cout << setprecision(1) << (double)resistance / 1000 << "k ohms" << endl; } else {     cout << resistance << " ohms" << endl; }```
Kurt
• 09-15-2005
-JM
When you finish this one you can write a program to calculate the capacitance of a resistor. ;)
• 09-15-2005
orbitz
In C++ we use C++ style casts, that's why they are there.

static_cast<double>(resistance) / 1000

Also, I would sugget dividing yoru program up into various sections. In one you do everything needed to compute the resistence. In another your take this value and prepare it for output. Then output it. That way if you need ot od other work with the resistence you ahv efunctions to calculate it, So on and so forth.
• 09-15-2005
ZuK
Quote:

Originally Posted by orbitz
In C++ we use C++ style casts, that's why they are there.
static_cast<double>(resistance) / 1000.

True but they are ugly and its a lot of typing. ;)
Kurt
• 09-15-2005
orbitz
Since when has any part of C++ not been ugly? This isn't a beauty contest it's a writing safe code that doesn't suck contest.
• 09-15-2005
Dae
Quote:

Originally Posted by orbitz
Since when has any part of C++ not been ugly? This isn't a beauty contest it's a writing safe code that doesn't suck contest.

/signed
• 09-15-2005
muralemerica
Well at this point I have no idea what you guys are talking about (tomorow ill look into it, right now i gota do some actual school homework) but the way that Zuk made the code im not sure if he relizes thats...pretty much all there is to my code. Although earlier I did make a function of getting the zeros, heres the full code.
Code:

```#include <iostream> using namespace std; int process(int); int main() {   int band1, band2, band3;         cout<<"-------------"<<endl;   cout<<"0 is black"<<endl;   cout<<"1 is brown"<<endl;   cout<<"2 is red"<<endl;   cout<<"3 is orange"<<endl;   cout<<"4 is yellow"<<endl;   cout<<"5 is green"<<endl;   cout<<"6 is blue"<<endl;   cout<<"7 is violet"<<endl;   cout<<"8 is gray"<<endl;   cout<<"9 is white"<<endl;   cout<<"-------------"<<endl;   cout<<endl;    cout<<"1st band: ";   cin>>band1;   cout<<"2nd band: ";   cin>>band2;   cout<<"3rd band: ";   cin>>band3;   cout<<endl;     if (band1 == 0)   {   cout<<"Your resistor has "<<band2;   process(band3);   }           else   {   cout<<"Your resistor has "<<band1<<band2;   process(band3);   } cout<<endl; system("pause"); return 0; } int process(int band3) {             for (int increase = 0; increase < band3; increase++)             {                 cout<<"0";             }                     cout<<" Ohms of resistance."<<endl; return 0; }```
But im still gonna look into those casts (Im assuming those are the things Zuk suggested)
• 09-16-2005
ZuK
It should work this way.
Code:

```#include <iostream> #include <iomanip> using namespace std; int main() {   int band1, band2, band3;   unsigned long resistance = 0;   cout<<"-------------"<<endl;   cout<<"0 is black"<<endl;   cout<<"1 is brown"<<endl;   cout<<"2 is red"<<endl;   cout<<"3 is orange"<<endl;   cout<<"4 is yellow"<<endl;   cout<<"5 is green"<<endl;   cout<<"6 is blue"<<endl;   cout<<"7 is violet"<<endl;   cout<<"8 is gray"<<endl;   cout<<"9 is white"<<endl;   cout<<"-------------"<<endl;   cout<<endl;    cout<<"1st band: ";   cin>>band1;   cout<<"2nd band: ";   cin>>band2;   cout<<"3rd band: ";   cin>>band3;   cout<<endl;   if (band1 == 0) {       cout<<"Your resistor has ";       resistance = band2;   }          else {     resistance = 10* band1 + band2;     cout<<"Your resistor has ";   }   // process band3   unsigned long mult = 1;   for (int increase = 0; increase < band3; increase++) {       mult *= 10;   }   resistance *= mult;     if ( resistance >= 1000000000 ) { // gte 1G       cout << setprecision(1) << static_cast<double>(resistance) / 1000000000 << "G";   }   else if ( resistance >= 1000000 ) { // gte 1M       cout << setprecision(1) << static_cast<double>(resistance) / 1000000 << "M";   }   else if ( resistance >= 1000 ) { //gte 1k       cout << setprecision(1) << static_cast<double>(resistance) / 1000 << "k";   }   else       cout << resistance;   cout << " Ohms of resistance." << endl;     cout<<endl;   system("pause");   return 0; }```
Kurt