Need help accepting ordered pair of strings

Hi, I am trying to implement a way to accept an ordered pair of strings besides accepting the integer ordered pair of strings. One of the suggestions I received was implementing and modifying the random generation values, but I am stuck on whether to change things to a <string> vector.

Suggestion:

1
2
3
4
    string empty = "";
    myList2[i].setFirst(empty + char('a' + rand() % 26));
    myList2[i].setSecond(empty + char('A' + rand() % 26));


Here is my header / implementation file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <exception>
using namespace std;

namespace cs_pairs
{
    template <class T>
    class OrderedPair
    {
    private:
        T first;
        T second;
    public:
        typedef std::size_t size_type;
        typedef T value_type;        // changed int to T
        static const int DEFAULT_VALUE = 0;

        class DuplicateMemberError
        {

        };

        OrderedPair(T newFirst = DEFAULT_VALUE, T newSecond = DEFAULT_VALUE) {
            setFirst(newFirst);
            setSecond(newSecond);
        }

        void setFirst(T newFirst) {
            if ((newFirst == second) && (newFirst != 0))
            {
                throw DuplicateMemberError();
            }
            first = newFirst;
        }

        void setSecond(T newSecond) {
            /*  try {
                  if (first == second)
                  {
                      throw DuplicateMemberError();
                  }
              }

              catch (DuplicateMemberError e)
              {
                  cout << "You entered the same two Digits!\n" << endl;
              } */

            if ((newSecond == first) && (newSecond != 0))
            {
                throw DuplicateMemberError();
            }
            second = newSecond;
        }

        T getFirst() const {
            return first;
        }

        T getSecond() const {
            return second;
        }

        OrderedPair<T> operator+(const OrderedPair<T>& right) const {
            return OrderedPair(first + right.first, second + right.second);
        }

        bool operator<(const OrderedPair<T>& right) const {
            return first + second < right.first + right.second;
        }

        void print() const {
            cout << "(" << first << ", " << second << ")";
        }
    };

}



Here is my Client File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <exception>
#include "orderedpair.h"
using namespace std;
using namespace cs_pairs;

int main() {

    int num1, num2;
    OrderedPair<int> myList[10];

    srand(static_cast<unsigned>(time(0)));
    cout << "default value: ";
    myList[0].print();
    cout << endl;

    for (int i = 0; i < 10; i++) {
        myList[i].setFirst(rand() % 50);
        myList[i].setSecond(rand() % 50 + 50);
    }

    myList[2] = myList[0] + myList[1];

    if (myList[0] < myList[1]) {
        myList[0].print();
        cout << " is less than ";
        myList[1].print();
        cout << endl;
    }

    for (int i = 0; i < 10; i++) {
        myList[i].print();
        cout << endl;
    }

    cout << "Enter two numbers to use in an OrderedPair.  Make sure they are different numbers: ";
    cin >> num1 >> num2;
    OrderedPair<int> x;

    try {
        x.setFirst(num1);
        x.setSecond(num2);

        /*if (x.setFirst(num1) == x.setSecond(num2))
        {
            throw OrderedPair<int>::DuplicateMemberError;
        } */

    }
    catch (OrderedPair<int>::DuplicateMemberError e) {
        cout << "YOU ENTERED TWO OF THE SAME VALUES!" << endl;
        x.setFirst(OrderedPair<int>::DEFAULT_VALUE);
        x.setSecond(OrderedPair<int>::DEFAULT_VALUE);
    }

    cout << "The resulting OrderedPair: ";
    x.print();
    cout << endl;
}
Last edited on
> static const int DEFAULT_VALUE = 0;
Make this
static const T DEFAULT_VALUE = T();

> if ((newFirst == second) && (newFirst != 0))
> if ((newSecond == first) && (newSecond != 0))
Change the 0 to be DEFAULT_VALUE

1
2
3
        bool operator<(const OrderedPair<T>& right) const {
            return first + second < right.first + right.second;
        }

I suppose it depends on what you mean by order
To me, 0 x will always come before 1 y regardless of the values of x and y
Whereas you're suggesting that 0 99 comes after 1 2

If you want to use this with strings, you need to construct your < operator to work only in terms of < on the sub-types.

Otherwise, it's a messy "what does it mean to add two strings and then compare them".
Topic archived. No new replies allowed.