Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,161 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,354 people online right now. Registration is fast and FREE... Join Now!




Need help with resistors value, and output. THIS IS C++ PROGRAMMING

 
Reply to this topicStart new topic

Need help with resistors value, and output. THIS IS C++ PROGRAMMING

TheHater
1 May, 2007 - 06:13 PM
Post #1

New D.I.C Head
*

Joined: 1 May, 2007
Posts: 3


My Contributions
CODE
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
#define size 4

struct ResistorsValue
{
    char ColorBand[10];
    int black, brown, red, orange, yellow, green, blue, violet, gray, white; // 0 1 2 3 4 5 6 7 8 9
    double gold, silver, none; // +-5% +-10% +-15%
};

//void Function(Resistor*, int, double);

void main(void)
{
    int num, num2, i, colors, numcolor1, numcolor2, numcolor3;
    double Value;

    cout << setprecision(2) << setiosflags(ios :: fixed | ios :: showpoint);

    cout << "Enter the number of Resistors value you want to create: ";
    cin >> num;
    cout << endl;

    ResistorsValue *cal = new ResistorsValue[num];

    cal.black = 1;

    for(i = 0; i < num; ++i)
    {
        cout << "Enter information for resistor number" << i + 1 << endl;

        for(colors = 0; colors < size; ++colors)
        {
            cout << "Enter color band number " << colors + 1 << ": ";
            cin.ignore();
            cin.getline(cal[colors].ColorBand,10);
        }


        /*num2 = 1 * pow(10, numcolor3);
        Value = ((numcolor1 * 10) + numcolor2) * num; // This is the equation to calculate the resistor value.
        */
        
    }
}



can someone help me show the out put, and how to calculate the resistor value. the equation i came up with work to calculate the resistor value, but I can't seem to get the numcolor# input

This post has been edited by NickDMax: 1 May, 2007 - 07:38 PM

User is offlineProfile CardPM
+Quote Post


William_Wilson
RE: Need Help With Resistors Value, And Output. THIS IS C++ PROGRAMMING
1 May, 2007 - 07:10 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,446



Thanked: 55 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
Here's a chart to calculate resistors:
Attached Image
it was possibly my best friend in electrical physics
User is offlineProfile CardPM
+Quote Post

TheHater
RE: Need Help With Resistors Value, And Output. THIS IS C++ PROGRAMMING
1 May, 2007 - 07:12 PM
Post #3

New D.I.C Head
*

Joined: 1 May, 2007
Posts: 3


My Contributions
QUOTE(William_Wilson @ 1 May, 2007 - 08:10 PM) *

Here's a chart to calculate resistors:
Attached Image
it was possibly my best friend in electrical physics

I need to write a program to do that
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Need Help With Resistors Value, And Output. THIS IS C++ PROGRAMMING
1 May, 2007 - 09:20 PM
Post #4

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 320



Thanked: 6 times
Dream Kudos: 50
My Contributions
This is just a quick stab at this, and it will need a lot of extra features to accommodate the two different numbers of bands possible. A profound difference is the use of command line arguments rather than a menu, but if you finish the solving everything else it will be easy to throw a menu back in there. Hopefully you will be able to start to see the switching possibilities here that will bring fuzzy data into order.
The enum makes it so that the color bands can be labeled as numbers or colors and still get the same results. Hopefully it will take the confusion out of the case statements that will be needed in the getValue(str) getMulitplier(str) getTolerance(str) functions.


CODE

  enum colorBand { black, brown, red, orange, yellow, green, blue, violet, gray, white, gold, silver };
  double multiplier[] = {1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 0.0, 0.0, 0.0, 0.1, 0.01};
  double tolerance[] = { 0.0, 0.01, 0.02, 0.0, 0.0, 0.005, 0.0025, 0.001, 0.0, 0.0, 0.05, 0.10 };



int main(int argc, char **argv)
{
  if (argc != 6) {
    cout <<  "USAGE: This program requires 5 resistor color codes, it then displays the value in ohms of the resitor.\n"
         << "Example: " << arg[0] << " red blue green violet gray\n"
         << "Output -> 10000 ohms \n";
    return 0;
  }
  double equation[6];
  
  for (int i = 1; i < 6; i++)
    equation[i-1] = getValue( arg[i], i );
  
  displayResults( equation );
}

double getBandValue(char *str, int location) {
   double temp;
   switch (location) {
     case 1: case 2: temp = getValue(str);
     case 3: case 4: temp = getMulitplier(str);
     case 5: temp = getTolerance(str);
   };
   return temp;
}

P.S.
This probably won't compile very well, but a good solution to that is to make a class and place the functions, arrays and enumerator into it. (I hope that you have learned how to make classes already.)

This post has been edited by ajwsurfer: 1 May, 2007 - 09:36 PM
User is offlineProfile CardPM
+Quote Post

TheHater
RE: Need Help With Resistors Value, And Output. THIS IS C++ PROGRAMMING
8 May, 2007 - 07:17 PM
Post #5

New D.I.C Head
*

Joined: 1 May, 2007
Posts: 3


My Contributions
CODE

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
#define size 4

struct ResistorsValue
{
    char Color1[10], Color2[10], Color3[10], Color4[10];
    
    double gold, silver, none; // +-5% +-10% +-15%
    double Value;
    int numcolor4;
};

void display(ResistorsValue*, int);

void main(void)
{
    
    int num, i, numcolor1, numcolor2, numcolor3;
    double num2;
    
    int black = 0, brown = 1, red = 2, orange = 3, yellow = 4;
    int green = 5, blue = 6, violet = 7, gray = 8, white = 9; // 0 1 2 3 4 5 6 7 8 9

    cout << setprecision(2) << setiosflags(ios :: fixed | ios :: showpoint);

    cout << "Enter the number of Resistors value you want to create: ";
    cin >> num;
    cout << endl;

    ResistorsValue *cal = new ResistorsValue[num];

    for(i = 0; i < num; ++i)
    {
            cin.ignore();
            cout << "Enter the first color on the band: ";
            cin.getline(cal[i].Color1,10);

            cout << "Enter the second color on the band: ";
            cin.getline(cal[i].Color2,10);

            cout << "Enter the third color on the band: ";
            cin.getline(cal[i].Color3,10);

            cout << "Enter the fourth color on the band: ";
            cin.getline(cal[i].Color4,10);
                
                if(strcmp (cal[i].Color1,"black") == 0)
                {
                    numcolor1 = 0;
                }
        
                else if(strcmp (cal[i].Color1,"brown") == 0)
                {
                    numcolor1 = 1;
                }
                else if(strcmp (cal[i].Color1,"red") == 0)
                {
                    numcolor1 = 2;
                }
                else if(strcmp (cal[i].Color1,"yellow") == 0)
                {
                    numcolor1 = 3;
                }
                else if(strcmp (cal[i].Color1,"orange") == 0)
                {
                    numcolor1 = 4;
                }
                else if(strcmp (cal[i].Color1,"green") == 0)
                {
                    numcolor1 = 5;
                }
                else if(strcmp (cal[i].Color1,"blue") == 0)
                {
                    numcolor1 = 6;
                }
                else if(strcmp (cal[i].Color1,"violet") == 0)
                {
                    numcolor1 = 7;
                }
                else if(strcmp (cal[i].Color1,"gray") == 0)
                {
                    numcolor1 = 8;
                }
                else
                {
                    numcolor1 = 9;
                }



            
                if(strcmp (cal[i].Color2,"black") == 0)
                    numcolor2 = 0;
        
                else if(strcmp (cal[i].Color2,"brown") == 0)
                {
                    numcolor2 = 1;
                }
                else if(strcmp (cal[i].Color2,"red") == 0)
                {
                    numcolor2 = 2;
                }
                else if(strcmp (cal[i].Color2,"yellow") == 0)
                {
                    numcolor2 = 3;
                }
                else if(strcmp (cal[i].Color2,"orange") == 0)
                {
                    numcolor2 = 4;
                }
                else if(strcmp (cal[i].Color2,"green") == 0)
                {
                    numcolor2 = 5;
                }
                else if(strcmp (cal[i].Color2,"blue") == 0)
                {
                    numcolor2 = 6;
                }
                else if(strcmp (cal[i].Color2,"violet") == 0)
                {
                    numcolor2 = 7;
                }
                else if(strcmp (cal[i].Color2,"gray") == 0)
                {
                    numcolor2 = 8;
                }
                else
                {
                    numcolor2 = 9;
                }


            
                if(strcmp (cal[i].Color3,"black") == 0)
                {
                    numcolor3 = 0;
                }
        
                else if(strcmp (cal[i].Color3,"brown") == 0)
                {
                    numcolor3 = 1;
                }
                else if(strcmp (cal[i].Color3,"red") == 0)
                {
                    numcolor3 = 2;
                }
                else if(strcmp (cal[i].Color3,"yellow") == 0)
                {
                    numcolor3 = 3;
                }
                else if(strcmp (cal[i].Color3,"orange") == 0)
                {
                    numcolor3 = 4;
                }
                else if(strcmp (cal[i].Color3,"green") == 0)
                {
                    numcolor3 = 5;
                }
                else if(strcmp (cal[i].Color3,"blue") == 0)
                {
                    numcolor3 = 6;
                }
                else if(strcmp (cal[i].Color3,"violet") == 0)
                {
                    numcolor3 = 7;
                }
                else if(strcmp (cal[i].Color3,"gray") == 0)
                {
                    numcolor3 = 8;
                }
                else
                {
                    numcolor3 = 9;
                }


                if (strcmp (cal[i].Color4, "gold") == 0)
                {
                    cal[i].numcolor4 = 5;
                }
                else if (strcmp (cal[i].Color4, "silver") == 0)
                    cal[i].numcolor4 = 10;
                else
                {
                    cal[i].numcolor4 = 15;
                }
            
                    


        num2 = 1 * pow(10, numcolor3);
        cal[i].Value = ((numcolor1 * 10) + numcolor2) * num2; // This is the function to calculate the resistor value.

        
    }
    display(cal, num);
    
}// End of main program

void display(ResistorsValue *cal, int num)
{
    int i;
    cout << setw(10) << "Color 1" << setw(10) << "Color2" << setw(10) << "Color 3" << setw(10) << "Color 4" << setw(10) << " Resistor Value" << setw(10);
    cout << endl;

    for (i = 0; i < num; ++i)
    {
            cout << setw(10) << cal[i].Color1 << setw(10) << cal[i].Color2 << setw(10) << cal[i].Color3 << setw(10) << cal[i].Color4 << setw(10) << cal[i].Value << "+-" << cal[i].numcolor4 << "%" << " Oms";
            cout << endl;
    }

    int j;

    ResistorsValue sort;

    for (j = 0; j < num - 1; ++j)
    {
        for (i = 0; i < num - 1; ++i)
        {
            if (cal[i].Value, cal[i + 1].Value > 0)
            {
                sort = cal[i];
                cal[i] = cal[i + 1];
                cal[i + 1] = sort;
            }
        }
    }
    cout << "Here is the record sorted under resistor value" << endl;

    cout << setw(10) << "Color 1" << setw(10) << "Color2" << setw(10) << "Color 3" << setw(10) << "Color 4" << setw(10) << "Resistor Value" << setw(10);
    cout << endl;

    for (i = 0; i < num; ++i)
    {
        cout << setw(10) << cal[i].Color1 << setw(10) << cal[i].Color2 << setw(10) << cal[i].Color3 << setw(10) << cal[i].Color4 << setw(10) << cal[i].Value << "+-" << cal[i].numcolor4 << "%" << " Oms";
            cout << endl;
    }

    cout << endl;

    char respond;
    int search;

    cout << "Do you want to search for a resistor value: [Y,N]";
    cin.ignore();
    respond = cin.get();
    
    while ((respond == 'Y') || (respond == 'y'))
    {
        cin.ignore();
        cout << "Enter the resistor value you want to search";
        cin >> search;

        for (i = 0; i < num; ++i)
        {
            if( cal[i].Value == search )
            {
                break;
            }
        }

        if (i == num)
        {
            cout << "Invalid user input!";
        }

        else
        {
            cout << setw(10) << cal[i].Color1 << setw(10) << cal[i].Color2 << setw(10) << cal[i].Color3 << setw(10) << cal[i].Color4 << setw(10) << cal[i].Value << setw(10) << "+-" << cal[i].numcolor4 << "%" << " Oms";
            cout << endl;
        }

        cout << "Do you want to search again? ";
        respond = cin.get();
    }
    cout << endl;    
}



that seem to work just fine, and thanks to everyone who respond to this topic

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 04:57AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month