I have a program that has the user input a resistance value, then the program checks to see if it is a valid resistance value, and then prints the min and max, based off a valid tolerance value.

Here is my code:

Resistor.h

#ifndef Resistor_H
#define Resistor_H
#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;

class resistor          
{
public:

    int getTol(int&);
    double getRes(double&);
    void setTol(const int tolerance);
    void setRes(const double resistor);
    bool checkTol(int);
    bool checkRes(double, int);
    void changeValue(double, int, double, double);  
    void displayResMax(double, int);    
    void displayResMin(double, int);
    resistor();     
    void printResMaxMin() const;
    void calcResMax();
    void calcResMin();

private:
    double E12[12], E24[24], E48[48], E96[96];  
    int tolVal[4];      
    int tol, i;         
    bool found;
    double res, resMax, resMin, modResis; 
};

resistor::resistor()
{
    tolVal[0] = 10;
    tolVal[1] = 5;
    tolVal[2] = 2;
    tolVal[3] = 1;

    E12[0] = 10.0;
    E12[1] = 12.0;
    E12[2] = 15.0;
    E12[3] = 18.0;
    E12[4] = 22.0;
    E12[5] = 27.0;
    E12[6] = 33.0;
    E12[7] = 39.0;
    E12[8] = 47.0;
    E12[9] = 56.0;
    E12[10] = 68.0;
    E12[11] = 82.0;

    E24[0] = 10.0;
    E24[1] = 11.0;
    E24[2] = 12.0;
    E24[3] = 13.0;
    E24[4] = 15.0;
    E24[5] = 16.0;
    E24[6] = 18.0;
    E24[7] = 20.0;
    E24[8] = 22.0;
    E24[9] = 24.0;
    E24[10] = 27.0;
    E24[11] = 30.0;
    E24[12] = 33.0;
    E24[13] = 36.0;
    E24[14] = 39.0;
    E24[15] = 43.0;
    E24[16] = 47.0;
    E24[17] = 51.0;
    E24[18] = 56.0;
    E24[19] = 62.0;
    E24[20] = 68.0;
    E24[21] = 75.0;
    E24[22] = 82.0;
    E24[23] = 91.0;

    E48[0] = 10.0; E48[1] = 10.5; E48[2] = 11.0; E48[3] = 11.5; E48[4] = 12.1;
    E48[5] = 12.7; E48[6] = 13.3; E48[7] = 14.0; E48[8] = 14.7; E48[9] = 15.4;
    E48[10] = 16.2; E48[11] = 16.9; E48[12] = 17.8; E48[13] = 18.7; E48[14] = 19.6;
    E48[15] = 20.5; E48[16] = 21.5; E48[17] = 22.6; E48[18] = 23.7; E48[19] = 24.9;
    E48[20] = 26.1; E48[21] = 27.4; E48[22] = 28.7; E48[23] = 30.1; E48[24] = 31.6;
    E48[25] = 33.2; E48[26] = 34.8; E48[27] = 36.5; E48[28] = 38.3; E48[29] = 40.2;
    E48[30] = 42.2; E48[31] = 44.2; E48[32] = 46.4; E48[33] = 48.7; E48[34] = 51.1;
    E48[35] = 53.6; E48[36] = 56.2; E48[37] = 59.0; E48[38] = 61.9; E48[39] = 64.9;
    E48[40] = 68.1; E48[41] = 71.5; E48[42] = 75.0; E48[43] = 78.7; E48[44] = 82.5;
    E48[45] = 86.6; E48[46] = 90.9; E48[47] = 95.3;

    E96[0] = 10.0; E96[1] = 10.2; E96[2] = 10.5; E96[3] = 10.7; E96[4] = 11.0;
    E96[5] = 11.3; E96[6] = 11.5; E96[7] = 11.8; E96[8] = 12.1; E96[9] = 12.4;
    E96[10] = 12.7; E96[11] = 13.0; E96[12] = 13.3; E96[13] = 13.7; E96[14] = 14.0;
    E96[15] = 14.3; E96[16] = 14.7; E96[17] = 15.0; E96[18] = 15.4; E96[19] = 15.8;
    E96[20] = 16.2; E96[21] = 16.5; E96[22] = 16.9; E96[23] = 17.4; E96[24] = 17.8;
    E96[25] = 18.2; E96[26] = 18.7; E96[27] = 19.1; E96[28] = 19.6; E96[29] = 20.0;
    E96[30] = 20.5; E96[31] = 21.0; E96[32] = 21.5; E96[33] = 22.1; E96[34] = 22.6;
    E96[35] = 23.2; E96[36] = 23.7; E96[37] = 24.3; E96[38] = 24.9; E96[39] = 25.5;
    E96[40] = 26.1; E96[41] = 26.7; E96[42] = 27.4; E96[43] = 28.0; E96[44] = 28.7;
    E96[45] = 29.4; E96[46] = 30.1; E96[47] = 30.9; E96[48] = 31.6; E96[49] = 32.4;
    E96[50] = 33.2; E96[51] = 34.0; E96[52] = 34.8; E96[53] = 35.7; E96[54] = 36.5;
    E96[55] = 37.4; E96[56] = 38.3; E96[57] = 39.2; E96[58] = 40.2; E96[59] = 41.2; 
    E96[60] = 42.2; E96[61] = 43.2; E96[62] = 44.2; E96[63] = 45.3; E96[64] = 46.4; 
    E96[65] = 47.5; E96[66] = 48.7; E96[67] = 49.9; E96[68] = 51.1; E96[69] = 52.3; 
    E96[70] = 53.6; E96[71] = 54.9; E96[72] = 56.2; E96[73] = 57.6; E96[74] = 59.0; 
    E96[75] = 60.4; E96[76] = 61.9; E96[77] = 63.4; E96[78] = 64.9; E96[79] = 66.5; 
    E96[80] = 68.1; E96[81] = 69.8; E96[82] = 71.5; E96[83] = 73.2; E96[84] = 75.0; 
    E96[85] = 76.8; E96[86] = 78.7; E96[87] = 80.6; E96[88] = 82.5; E96[89] = 84.5; 
    E96[90] = 86.6; E96[91] = 88.7; E96[92] = 90.9; E96[93] = 93.1; E96[94] = 95.3; 
    E96[95] = 97.6;
}

#endif

Here is ResistorImp.cpp

/***********************************************
* Programmer: Brian Ediger
*
* Class: Comp 220, OOP structured programming
*
* Description: This is to implement the functions
*               in the resistor class.
************************************************/

#include "Resistor.h"

int resistor::getTol(int& tolerance)
{
    tol = tolerance;
    return tol;
}

double resistor::getRes(double& resistance)
{
    res = resistance;
    return res;
}

void resistor::setTol(const int tolerance)
{
    tol = tolerance;
}

void resistor::setRes(const double resistance)
{
    res = resistance;
}

bool resistor::checkTol(int tol)
{
    found;

    while(i < 4 && !found)
        if(tolVal[i] == tol)
            found = true;
        else
            i++;

    return (found);
}

bool resistor::checkRes(double res, int tol)
{
    found;

    if(res > 10.0)
        modResis = res/10.0;
    else
        modResis = res;
    if(modResis > 10.0)
        modResis = modResis/10.0;

    switch(tol)
    {
    case 10:
        while(i < 12 && !found)
            if(E12[i] == modResis)
                found = true;
            else
                i++;
    case 5:
        while(i < 24 && !found)
            if(E24[i] == modResis)
                found = true;
            else 
                i++;
    case 2:
        while(i < 48 && !found)
            if(E48[i] == modResis)
                found = true;
            else 
                i++;
    case 1:
        while(i < 96 && !found)
            if(E96[i] == modResis)
                found = true;
            else 
                i++;
    }
    return found;
}

void resistor::calcResMax()
{
    resMax = res * (1 + (tol/100));
}

void resistor::calcResMin()
{
    resMin = res * (1 - (tol/100));
}

void resistor::printResMaxMin() const
{
    cout << "Maximum resistance is: " << resMax << endl;
    cout << "Minimum resistance is: " << resMin << endl;
}

Here is my main.cpp

/************************************************
*Programmer: Brian Ediger
*
*Class: Comp220, OOP structured programming
*
*Description: Main function to recieve and return
*               proper resistance and tolerance
*               values. And display tolerance 
*               range.
*************************************************/

#include "Resistor.h"

void main()
{
    resistor resista;
    double ohms;
    int toler;

    cout << "Enter tolerance value: " << endl;
    cin >> toler;

    resista.getTol(toler);
    resista.setTol(toler);
    resista.checkTol(toler);
        if(!resista.checkTol(toler))
        {   
            cout << "Invalid tolerance. Please try again." << endl;
            main ();
        }           

    cout << "Enter resistnace value: " << endl;
    cin >> ohms;

    resista.getRes(ohms);
    resista.setRes(ohms);
    resista.checkRes(ohms, toler);
        if(!resista.checkRes(ohms, toler))
        {
            cout << "Invalid resistance. Please try again." << endl;
            main ();
        }
    resista.calcResMax();
    resista.calcResMin();
    resista.printResMaxMin();

    cout << "\nPress any key to exit." << endl;
    cin.ignore(2);

        return;
}

And here is my build output:

1>------ Build started: Project: Week5iLab, Configuration: Debug Win32 ------
1> Main.cpp
1> Generating Code...
1> Compiling...
1> ResistorImp.cpp
1> Generating Code...
1>ResistorImp.obj : error LNK2005: "public: __thiscall resistor::resistor(void)" (??0resistor@@QAE@XZ) already defined in Main.obj
1>C:\Users\ElZaraq\Downloads\Structured Programming\Comp220\Week5iLab\Debug\Week5iLab.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Obviously I haven't been able to get the program to run, so I don't know what else needs fixed. Right now I would just like some help getting the program to run. Thank you very much for any help you can give!

Recommended Answers

All 2 Replies

Each of your two .cpp files are treated as separate source files by the compiler and are compiled separately. In each source file you have included "Resistor.h", and thus in each source file those objects in "Resistor.h" are created. No problem until the linker trys to combine the two compiled source files and finds dupilcate objects created, when there should be only one set of objects in existance which are shared or used by both source files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.