Random error

Can anyone spot this error?

//PAGE 1 CLASS
#include <string>
#include<iostream>
#include<cmath>

using namespace std;
const int X = 4;
class Resistance
{
friend void ConvertColor(char, string[], int);
private:
double ResistValue;
double Tolerance;
double ConFact;
string Band[X];
string Units;
public:
Resistance(double, double, string [], string, double);
int string_Convert();
int ResistToleranceConvert();
double Rmax();
double Rmin();
double Standard_Resistance();
void Set_Units();
void Display(double, double);
void Display_Color();
};

//PAGE 2, Functions

#include <string>
#include<iostream>
#include<cmath>
#include "Class.h"

using namespace std;
string Lowercase(string);

//***RESISTANCE****************************************************************
Resistance::Resistance(double R, double T, string Color[X], string U, double C)
{
ResistValue = R;
Tolerance = T;
for (int i = 0; i < X; i++)
{
Band = Color[i];
}
Units = U;
ConFact = C;
}
//***STRING*CONVERSION*********************************************************
int Resistance::string_Convert()
{
double ResistArray[X];
int i;

for (i = 0; i < X; i++)
Band[i] = Lowercase(Band[i]);

if (Band[0] == "black" || Band[0] == "gold" || Band[0] == "silver")
return 1;

if (Band[1] == "gold" || Band[0] == "silver")
return 2;

for (i = 0; i < X - 1; i++)
{
if (Band[i] == "black")
ResistArray[i] = 0;
else if (Band[i] == "brown")
ResistArray[i] = 1;
else if (Band[i] == "red")
ResistArray[i] = 2;
else if (Band[i] == "orange")
ResistArray[i] = 3;
else if (Band[i] == "yellow")
ResistArray[i] = 4;
else if (Band[i] == "green")
ResistArray[i] = 5;
else if (Band[i] == "blue")
ResistArray[i] = 6;
else if (Band[i] == "violet")
ResistArray[i] = 7;
else if (Band[i] == "gray")
ResistArray[i] = 8;
else if (Band[i] == "white")
ResistArray[i] = 9;
else if (Band[i] == "silver")
ResistArray[i] = -1;
else if (Band[i] == "gold")
ResistArray[i] = -2;
else
return 5;
}

if (Band[3] == "gold")
Tolerance = 5.0;
else if (Band[3] == "silver")
Tolerance = 10.0;
else if (Band[3] == "none")
Tolerance = 20.0;
else
return 4;

ResistValue = (ResistArray[0]*10+ResistArray[1])*pow(10.0,ResistArray[2]);

return 0;
}
//***RESISTOR*TOLERANCE*CONVERSION*********************************************
int Resistance::ResistToleranceConvert()
{
string ResistString;
int i(0), element(0);

[i] ResistString = ResistValue;

while(i < ResistString.length() && element < 3)
{
if (isdigit(ResistString.at(i)))
{
ConvertColor(ResistString.at(i), Band, element);
element++;
}
else if (ResistString.at(i) == '^')
{
if (ResistString.at(i+1) == '-')
{
C = '-' + ResistString.at(i+2);
ConvertColor(C, Band, element);
element++;
}
else
{
C = ResistString.at(i+1);
ConvertColor(C, Band, element);
element++;
}
}
i++;
}

if (ResistValue < 0.01 || ResistValue > 100000000)
return 1;

if (Tolerance == 5)
Band[3] = "Gold";
else if (Tolerance == 10)
Band[3] = "Silver";
else if (Tolerance == 20)
Band[3] = "None";
else
return 2;

return 0;
}
//***MAX/MIN*******************************************************************
double Resistance::Rmax()
{
return(ResistValue * (1.0 + Tolerance/100.));
}
double Resistance::Rmin()
{
return(ResistValue * (1.0 - Tolerance/100.));
}
//***CONVERSION*TO*COLOR*******************************************************
void ConvertColor(char RValue, string Band[4], int element)
{
int Resist;

Resist = int(RValue);

if (Resist == 0)
Band[element] = "black";
else if (Resist == 1)
Band[element] = "brown";
else if (Resist == 2)
Band[element] = "red";
else if (Resist == 3)
Band[element] = "orange";
else if (Resist == 4)
Band[element] = "yellow";
else if (Resist == 5)
Band[element] = "green";
else if (Resist == 6)
Band[element] = "blue";
else if (Resist == 7)
Band[element] = "violet";
else if (Resist == 8)
Band[element] = "gray";
else if (Resist == 9)
Band[element] = "white";
else if (Resist == -1)
Band[element] = "silver";
else
Band[element] = "gold";

return;
}
//***SET*UNITS*****************************************************************
void Resistance::Set_Units()
{
if (ResistValue < 990)
{
Units = " ohms";
ConFact = 1.0;
}
else if (ResistValue >= 1000 && ResistValue <= 999000)
{
Units = " kilaohms";
ConFact = 1.0/1000.0;
}
else
{
Units = " Megaohms";
ConFact = 1.0/1000000.0;
}
return;
}
//***DISPLAY*******************************************************************
void Resistance::Display(double Min,double Max)
{
ResistValue = ResistValue * ConFact;
Max = Max * ConFact;
Min = Min * ConFact;
cout << endl << ResistValue << Units << endl
<< Tolerance << " %" << endl
<< "Max = " << Max << Units << endl
<< "Min = " << Min << Units << endl;

return;
}
//***DISPLAY*COLOR*************************************************************
void Resistance::Display_Color()
{
cout << "The corresponding color bands are:\n";
for (int i = 0; i < X; i++)
{
cout << Band[i] << endl;
}
return;
}
//***TO*LOWERCASE**************************************************************
string Lowercase(string Band)
{
for (int j = 0; j < int(Band.length()); j++)
{
Band[j] = tolower(Band[j]);
}
return Band;
}


I left the main code out since that isn't giving me any errors
The Code bolded and italicized is giving the error

[Warning] passing `double' for converting 1 of `std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'

Any ideas?
Last edited on
You're trying to assign a double to a string. This won't work by just using the assignment operator=.

The simplest way to do this would be to use a stringstream object, as follows:

1
2
3
4
5
6
7
#include <sstream>    //this header has the definitions for string streams

ostringstream os;   //declare an output string stream

os << ResistValue;     //converts ResistValue to a string and stores it in os

ResistString = os.str();     //gets the string associated with the stringstream and assign it to ResistString 
Topic archived. No new replies allowed.