Logical Error with input function

I am working on a project on inheritance, where DollarAmount is a parent class of class SpendingRecord. There seems to be a problem with these two input functions because whenever I type in -1 ( the delimiter ) it does not enter the if statement in the main, so the program never breaks and it accepts -1 as an acceptable dollar amount ( it should only accept -1 if the user no longer wishes to enter dollar amounts ). getDollar, getCent, and getDELIM are functions of the DollarAmount class, so I assumed SpendingRecord inherited them. Can anyone help?

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
78
79
80
81
82
83
84
85
void DollarAmount::inputDollar( istream &ins )
{
        char decimal;
        int dollar, cent;

        cout << "\n$PrintDollar " << endl;
        cout << "Enter the expenditure record (e.g., 1.95, coffee, enter -1 to end): $";

        ins >> dollar;

        if ( dollar != getDELIM())
        {
                ins >> decimal >> cent;
        }
                //
                //check to make sure that the user inputs the correct amount
                //ensures that dollar is in between 0 and 9999
                //ensures that the user does not put 0 dollars and 0 cents
                //ensures that the cents amount is between 0 and 99
                //
        while ( !setCent(cent) || !setDollar(dollar) || dollar == 0 && cent == 0 && dollar != getDELIM())
        {
                cout << "Invalid dollar/cent amount, please reenter: $";
                ins >> dollar >> decimal >> cent;
        }
        while( ins.fail() ){
                ins.clear();
                ins.ignore(1000,'\n');
                cout << "Wrong input types, please reenter: $";
                ins >> dollar >> decimal >> cent;
        }

        setDollar(dollar);
        setCent(cent);
}
void SpendingRecord::inputDollar( istream & in )
{
        string itemName;
        DollarAmount::inputDollar(in);

        if ( dollar != getDELIM() )
        {
                in >> itemName;
                getline( in, itemName );
                SpendingRecord::itemName = itemName;
        }
}

  //in main
      SpendingRecord *ptr = NULL;

        ptr = new SpendingRecord[MAX];

        for ( int i = 0; i < psize; i++ )
        {
                cin >> ptr[i];
                //
                //if user has put in the last element of the array
                //allocate a new array of a larger size and set it to NULL
                //copy the contents of smaller array into larger array
                //delete the old array
                //assign address location of new array to older array
                //increase the physical size of the array
                //
                if ( i == psize - 1 )
                {
                        SpendingRecord *tmp = NULL;
                        tmp = new SpendingRecord[psize*2];
                        for ( int j = 0; j < psize; j++ )
                                tmp[i] = ptr[i];

                        delete [] ptr;
                        ptr = tmp;
                        psize *= 2;
                }
                //if the user enters -1, break and do not include the last element ( i.e. -1 )
                if ( ptr[i].getDollar() == SpendingRecord::getDELIM() && ptr[i].getCent() == 0 )
                {
                        numElements = i;
                        break;
                }


        }
Last edited on
Topic archived. No new replies allowed.