I'm trying to convert a number to a string then back to a number to perform math, then back to a string to return it to the user. I don't understand the problem because this is how my friend did it (that I can remember and it worked for him). Basically if someone can help me with this topic it would be apreciated!

std::string Resistor::CalculateResistance(int Digit1, int Digit2, int Multiplier, int Tolerance) {
    std::istringstream buffer;
    buffer >> Digit1;

    std::string x = buffer.str; // Problem
    buffer >> Digit2;
    x += buffer.str; // Problem

    double y = atof(x.c_str) * Multiplier; // Problem

    std::istringstream buffer2;
    std::string z = "";

    if (y > 999 && y < 999999) {
        y /= 1000;
        buffer2 >> y;
        z = buffer2.str + "KΩ" + Tolerances[Tolerance]; // Problem
        return z;
    }

    else if (y > 999999 && y < 999999999) {
        y /= 1000000;
        buffer2 >> y;
        z = buffer2.str + "MΩ" + Tolerances[Tolerance]; // Problem
        return z;
    }

    return "An error occured, please try again...\n";
}

The errors being thrown are:

error C3867: 'std::basic_istringstream<_Elem,_Traits,_Alloc>::str': function call missing argument list; use '&std::basic_istringstream<_Elem,_Traits,_Alloc>::str' to create a pointer to member

error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)

Thanks,
Jamie

Recommended Answers

All 7 Replies

x += buffer.str; // Problem

str() is a member function: x += buffer.str();

double y = atof(x.c_str) * Multiplier; // Problem

c_str() is also a member function: 'double y = atof(x.c_str()) * Multiplier;'

Repeat that fix for every usage.

Okay so after looking at that link I threw together a class that follows the same principles. However it's giving me an error saying unresolved external symbol in Main.obj. I'll post the entire source and hopefully we can get this fixed. I appreciate all the help!

Error: error LNK2019: unresolved external symbol "public: int __thiscall Convert::ToInt32(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?ToInt32@Convert@@QAEHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Resistor::CalculateResistance(int,int,int,int)" (?CalculateResistance@Resistor@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HHHH@Z)

Location: Main.obj

Error: error LNK1120: 2 unresolved externals
Location: ResistorCalc.exe

// Main.cpp
#include <iostream>
#include "Resistor.h"

using namespace std;

int main() {
    Resistor r1;
    int b1;
    int b2;
    int b3;
    int b4;

    cout << "Please input the color of the first three bands using the following numbers:\n";
    cout << "Black - 0\n";
    cout << "Brown - 1\n";
    cout << "Red - 2\n";
    cout << "Orange - 3\n";
    cout << "Yellow - 4\n";
    cout << "Green - 5\n";
    cout << "Blue - 6\n";
    cout << "Violet - 7\n";
    cout << "Gray - 8\n";
    cout << "White - 9\n";

    cin >> b1 >> b2 >> b3;

    cout << "Please input the color of the tolerance band color using the following numbers:\n";
    cout << "Gold - 0\n";
    cout << "Silver - 1\n";
    cout << "None - 2\n";

    cin >> b4;

    cout << "Your resistor is: " << r1.CalculateResistance(b1, b2, b3, b4);

    system("pause");
}

// Resistor.h
#include <string>
#include "Convert.h"

class Resistor {
    public:
        Resistor();
        int Bands[10];
        int Multipliers[10];
        int Tolerances[3];

        std::string CalculateResistance(int, int, int, int);
};

Resistor::Resistor() {
        // Black, Brown, Red, Orange, Yellow, Green, Blue, Violet, Gray, White
        for (int i = 0; i < 10; i++)
            Bands[i] = i;

        for (int x = 0; x < 10; x++) {
            for (int y = 0.01; y < 10000000; y *= 10)
                Multipliers[x] = y;
        }

        // Gold, Silver, None
        for (int x = 0; x < 3; x++) {
            for (int y = 5; y < 20; y *= 2)
                Tolerances[x] = y;
        }
}

std::string Resistor::CalculateResistance(int Digit1, int Digit2, int Multiplier, int Tolerance) {
    Convert convert;
    std::string Additive = convert.ToString(Digit1) + convert.ToString(Digit2);

    double y = convert.ToInt32(Additive) * Multiplier;

    std::string z = "";

    if (y >= 0 && y < 999) {
        z = convert.ToString(y) + "Ohm +- " + convert.ToString(Tolerances[Tolerance]);
        return z;
    }

    else if (y > 999 && y < 999999) {
        y /= 1000;

        z = convert.ToString(y) + "K Ohm +- " + convert.ToString(Tolerances[Tolerance]);
        return z;
    }

    else if (y > 999999 && y < 999999999) {
        y /= 1000000;

        z = convert.ToString(y) + "M Ohm +- " + convert.ToString(Tolerances[Tolerance]);
        return z;
    }

    return "error 0: result was out of range.\n";
}

// Convert.h
#include <sstream>
#include <string>

using namespace std;

class Convert {
public:
    bool IsInt32(string);
    int ToInt32(string);
    string ToString(int);
};

    bool IsInt32(string s) {
        string hyphen = "-";

        if (s[0] == hyphen[0])
            s.erase(0, 1);

        for (int i = 0; i < (int)s.size(); i++)
            if (isdigit(s[i] == 0)) return false;

        return true;
    }

    string ToString(int i) {
        stringstream s;
        s << i;

        return s.str();
    }

    string ToString(double d) {
        stringstream s;
        s << d;

        return s.str();
    }

    int ToInt32(string s) {
        stringstream i;
        int returnVal;

        if (IsInt32(s)) {
            i << s;
            i >> returnVal;
            return returnVal;
        }

        else return -1;
    }

Thanks,
Jamie

Look in Convert.h and ask yourself how to define a member function outside of the class definition. That's your error. You'll also have overloading issues because the return type doesn't make a function signature unique.

Here's a hint:
Line 114:bool IsInt32(string s) {
Should look like: bool Convert::IsInt32(string s) {

This is not the only change you will have to make.

Later on, I suggest you change the methods in your Convert class into static's so you can call them directly from the class instead of creating a Convert object everytime. But save that for later. For now, this will probably only be confusing. Right now, just get it working. Then, get it working nicely.

Okay so I got the application to successfully compile, however there is still a problem; the application is not printing anything I told it to in the main() function. It just sits there and I'm thinking it might be infinite loop or recursion because I can't even type anything into the terminal?

// Main.cpp
#include <iostream>
#include <string>
#include "Resistor.h"

using namespace std;

int main() {
    Resistor r1;
    int b1;
    int b2;
    int b3;
    int b4;

    cout << "Please input the color of the first three bands using the following numbers:\n";
    cout << "Black - 0\n";
    cout << "Brown - 1\n";
    cout << "Red - 2\n";
    cout << "Orange - 3\n";
    cout << "Yellow - 4\n";
    cout << "Green - 5\n";
    cout << "Blue - 6\n";
    cout << "Violet - 7\n";
    cout << "Gray - 8\n";
    cout << "White - 9\n";

    cin >> b1 >> b2 >> b3;

    cout << "Please input the color of the tolerance band color using the following numbers:\n";
    cout << "Gold - 0\n";
    cout << "Silver - 1\n";
    cout << "None - 2\n";

    cin >> b4;

    cout << "Your resistor is: " << r1.CalculateResistance(b1, b2, b3, b4);

    system("pause");
}

// Resistor.h
#include <string>
#include "Convert.h"

class Resistor {
    public:
        Resistor();
        int Bands[10];
        int Multipliers[10];
        int Tolerances[3];

        std::string CalculateResistance(int, int, int, int);
};

Resistor::Resistor() {
        // Black, Brown, Red, Orange, Yellow, Green, Blue, Violet, Gray, White
        for (int i = 0; i < 10; i++)
            Bands[i] = i;

        for (int x = 0; x < 10; x++) {
            for (int y = 0.01; y < 10000000; y *= 10)
                Multipliers[x] = y;
        }

        // Gold, Silver, None
        for (int x = 0; x < 3; x++) {
            for (int y = 5; y < 20; y *= 2)
                Tolerances[x] = y;
        }
}

std::string Resistor::CalculateResistance(int Digit1, int Digit2, int Multiplier, int Tolerance) {
    Convert convert;
    std::string Additive = convert.ToString(Digit1) + convert.ToString(Digit2);

    double y = convert.ToInt32(Additive) * Multiplier;

    std::string z = "";

    if (y >= 0 && y < 999) {
        z = convert.ToString(y) + "Ohm +- " + convert.ToString(Tolerances[Tolerance]);
        return z;
    }

    else if (y > 999 && y < 999999) {
        y /= 1000;

        z = convert.ToString(y) + "K Ohm +- " + convert.ToString(Tolerances[Tolerance]);
        return z;
    }

    else if (y > 999999 && y < 999999999) {
        y /= 1000000;

        z = convert.ToString(y) + "M Ohm +- " + convert.ToString(Tolerances[Tolerance]);
        return z;
    }

    return "error 0: result was out of range.\n";
}

// Convert.h
#include <sstream>
#include <string>

using namespace std;

class Convert {
public:
    bool IsInt32(string);
    int ToInt32(string);
    string ToString(int);
};

    bool Convert::IsInt32(string s) {
        string hyphen = "-";

        if (s[0] == hyphen[0])
            s.erase(0, 1);

        for (int i = 0; i < (int)s.size(); i++)
            if (isdigit(s[i] == 0)) return false;

        return true;
    }

    string Convert::ToString(int i) {
        stringstream s;
        s << i;

        return s.str();
    }

    int Convert::ToInt32(string s) {
        stringstream i;
        int returnVal;

        if (IsInt32(s)) {
            i << s;
            i >> returnVal;
            return returnVal;
        }

        else return -1;
    }

All help is appreciated,
Jamie

Your multiplier array and your code to populate it is the problem. The array is of type int but you are trying to store a double into it. This will just cause a truncation problem for storing the values. The main issue is in your for loop on line 61. You set y to .01 which the compiler will make equal to 0 for an int data type. Since y is zero and you are multiplying it by 10 you will always get zero and have an infinite loop. I would suggest you change the type of the array you are using and you will also need to rewrite the code to fill the array. For filling the array I would do something like this

// in class body
Double Multipliers[10];
//...

// in constructor
Multipliers[0] = .01;
for (int i = 1; i < 10; i++)
    Multipliers[i] = Multipliers[i – 1] * 10;
//…
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.