0

I'm currently working on some code where I have to declare an array in my main and create a function that allows for me to take user input and store it into the array. I've gotten started, but I'm running into the error binary '>>': no operator found which takes a right-hand operand of type 'CDistance' I have #include <string> included in my code as well. The error occurs below in the function void inputDist(CDistance distList[], int size). Below is the full code. All and any feedback is appreciated. Thanks.

#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

class CDistance
{
private:
    int feet;
    int inches;
    int feet2;
    int inches2;

public:
    CDistance();
    CDistance(int, int, int, int);
    ~CDistance();
    CDistance printAndAdd(const CDistance distList[], int size);
    void setDistt();
    void printDistt() const;
    void add(const CDistance&) const;
    void subtract(const CDistance&) const;
    void menu(const CDistance&) const;
    void inputDist(CDistance distList[], int size);
};

CDistance::CDistance()
{
    feet;
    inches;
    feet2;
    inches2;
}

CDistance::CDistance(int f, int i, int f2, int i2)
{
    feet = f;
    inches = i;
    feet2 = f2;
    inches2 = i2;
}

CDistance::~CDistance()
{
}

void CDistance::setDistt()
{
    cout << "Enter the first set of feet: ";
    cin >> feet;
    cout << "\nEnter the second set of feet: ";
    cin >> feet2;
    cout << "\nEnter the first set of inches: ";
    cin >> inches;
    cout << "\nEnter the second set of inches: ";
    cin >> inches2;
}

void CDistance::printDistt() const
{
    cout << "Feet: " << feet << "," << feet2 << endl << "Inches: " << inches << "," << inches2 << endl;
}

void CDistance::add(const CDistance& total) const
{
    int totFeet = feet + feet2;
    int totInches = inches + inches2;
    if (totInches >= 12)
    {
        totInches = totInches / 12;
        int newFeet = totInches;
        totFeet = totFeet + newFeet;
    }
    cout << totFeet << " feet" << endl;
    cout << totInches << " inches" << endl;
}

void CDistance::subtract(const CDistance& total) const
{
    int totFeet = feet - feet2;
    int totInches = inches - inches2;
    if (totInches >= 12)
    {
        totInches = totInches / 12;
        int newFeet = totInches;
        totFeet = totFeet - newFeet;
    }
    cout << totFeet << " feet" << endl;
    cout << totInches << " inches" << endl;
}

void CDistance::menu(const CDistance& total) const
{
    CDistance m(feet, inches, feet2, inches2);
    int choice;
    bool menuGo = true;


    while (menuGo != false)
    {
        {
            cout <<
                "\nWhat would you like to do?"
                "\n1: Add "
                "\n2: Subtract "
                "\n3: Exit" << endl;
            cin >> choice;
        }

        switch (choice)
        {
        case 1:
            cout << "You chose to add" << endl;
            m.add(total);
            break;
        case 2:
            cout << "You chose to subtract" << endl;
            m.subtract(total);
            break;
        case 3:
            cout << "Please enter 5 digits to enter into the array: ";
            m.inputDist;
        case 4:
            cout << "Goodbye" << endl;
            menuGo = false;
            break;
        default:
            cout << "Not a valid choice." << endl;
            cout << "Choose again." << endl;
            cin >> choice;
            break;
        }
    }
}

void inputDist(CDistance distList[], int size)
{
    int dist = 0;
    for (int i = 0; i < 6; i++)
    {
        cin >> distList[i];
    }
}

//CDistance printAndAdd(const CDistance distList[], int size);
int main()
{
    CDistance d1, d2(0, 0, 0, 0);

    CDistance distList[5];

    d1.setDistt();
    d1.printDistt();
    d1.menu(d2);
    inputDist(distList, 0);
    _getch();
    return 0;
}
Justin Farr
  • 183
  • 3
  • 5
  • 16

2 Answers2

2

If you want to use cin >> variable with a custom type you got to declare your own operator>> otherwise the compiler can't know how you actually want to read the values. A possible way to do it in this case would be:

friend std::istream& operator>>(std::istream& is, CDistance& dist);

declaring it as a friend function inside the body of the class, and then defining it outside like

std::istream& operator>>(std::istream& is, CDistance& dist) {
    is >> dist.feet >> dist.inches >>
          dist.feet2 >> dist.inches2;
    return is;
}

Also on a side note, your default constructor does absolute nothing:

CDistance::CDistance()
{
    feet;       // evaluate feet and discard it as you don't do anything with it.
    inches;     // same
    feet2;      // same
    inches2;    // same
}

you might want to set them to default values instead:

CDistance::CDistance()
{
    feet = inches = feet2 = inches2 = 0;
}

would set them all to 0 as default.

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
0

CDistance is a custom type, and cin does not have interface to set its values.

void inputDist(CDistance distList[], int size)
{
    int dist = 0;
    for (int i = 0; i < 6; i++)
    {
        cin >> distList[i]; //<- invalid
    }
}

Try the following instead:

void inputDist(CDistance distList[], int size)
{
    int dist = 0;
    for (int i = 0; i < 6; i++)
    {
        int feet;    cin >> feet;
        int inches;  cin >> inches;
        int feet2;   cin >> feet2;
        int inches2; cin >> inches2;
        distList[i] = CDistance(feet,inches,feet2,inches2);
    }
}
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271