Thread: Capacitor code

  1. #1
    Motor head pre-programmer
    Join Date
    Aug 2004
    Posts
    8

    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.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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

  3. #3
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    When you finish this one you can write a program to calculate the capacitance of a resistor.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    In C++ we use C++ style casts, that's why they are there.

    static_cast<double>(resistance) / 1000

    http://www.acm.org/crossroads/xrds3-1/ovp3-1.html

    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.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    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.

  7. #7
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    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
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    Motor head pre-programmer
    Join Date
    Aug 2004
    Posts
    8
    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)

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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

Popular pages Recent additions subscribe to a feed