Strange error need help

Im getting a strange error from line 108 reading, "non-lvalue in assignment"
all im looking for is a point in the right direction
Thanx

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
using namespace std;


//class for a bank certificate of deposit:
class CDAccount
{   

    double balance;
    double interest_rate;
    int term;//months until maturity
    
public:
       CDAccount(); //default constructor (works)
       
       CDAccount (double balance, double interest_rate, int term); //constructor w/ arguments ????
       
       void display()const; //display the CD Account (works)
       
       double Balance (double b); //enter the initial balance (works) 
      
       double IntrestRate (double ir); //enter the interest rate (works)
       
       int Term (int t); //enter the term in months (works)
       
       double getBalance (); // returns the balance
       
       double getRate (); // returns the Interest Rate
       
       int getTerm (); // returns the term
       
       double MatureBalance (double InitialBalance, double IntrestRate, int term); //return balance at maturity
       
};
CDAccount::CDAccount(): balance(0), interest_rate(0), term(0)
{}//default 

CDAccount::CDAccount (double  balance, double interest_rate, int term) :
    balance(balance), interest_rate(interest_rate), term(term)
{}//constructor 

void CDAccount::display() const 
{
     cout << "the ballance is: " << balance << " the interest rate is: " << interest_rate << " the term in months is: " << term << endl;
}

double CDAccount::Balance (double b)
{
    cout << "please enter the account balance" << endl;
     cin >> b; 
     balance=b;
     if (balance < 0)
     {
     cout << "Bankrupt" << endl;
     }
}

double CDAccount::IntrestRate (double ir)
{
       cout << "please enter the intrest rate as a decimal" << endl;
       cin >> ir;
       interest_rate=ir;
       if (interest_rate>=1.00)
       {
       cout << "improper input for rate" << endl;
       }
}

int CDAccount::Term(int t)
{
    cout << "please enter a term lenght for 1 to 12 months" << endl;
    cin >> t;
    term=t;
    if (term > 12)
    {
    cout << "term lenght too long" << endl;
    }
}

double CDAccount::getBalance ()
{
       return balance;
}

double CDAccount::getRate()
{
       return interest_rate;
}

int CDAccount::getTerm ()
{
    return term;
}

int main( )
{
    CDAccount account;
    account.display(); //display the default constructor 
    account.Balance(0);
    account.IntrestRate(0);
    account.Term(0);



    double rate_fraction, interest ;
    rate_fraction = account.getRate()/100.0;
    interest = account.getBalance()*rate_fraction*(account.getTerm()/12.0);
    account.getBalance() += interest ;

   cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << "When your CD matures in " 
        << account.getTerm() << " months,\n"
       << "it will have a balance of $" 
        << account.getBalance() << endl;
    system("pause");
    return 0;
}
Last edited on
closed account (o3hC5Di1)
Hi there,

The a += b operator does two things:

1. it adds b to a
2. it makes the value of variable a equal to the result of step 1.

Now, your function account.getBalance() returns a double.
Step 1 will be no problem, but the result of getBalance is called a temporary, it just returns a number.
You can't assign the result of step 1 to another number, only to an actual variable.

Lvalue references are, simplistically put, variable names, they are names for spaces in memory where information can be stored.

Hope that helps, let us know if you need any further help.

All the best,
NwN
Thank you thats makes since why it wont work, but then how would i access these private variables to do the calculations.
closed account (o3hC5Di1)
Hi,

It's not accessing the variable that's the problem, it's where you are trying to store the result.

I assume you want to set the balance to the result of that calculation, so you would use your CDAccount::Balance (double b) function to do so:

account.Balance( account.getBalance() + interest );

Hope that helps.

All the best,
NwN
Hi, Thank you for your help i tried that and because of line 49 in the CDAccount::Balance function. it asks for the balance again and skips the calculations. So is there a way around the second input or am i going to need another function? Sorry about the constant questions but I want to understand this.
I think there was a misunderstanding. Your function double CDAccount::Balance (double b) accepts a parameter b, but then ignores its value. I think it was assumed that the supplied parameter would directly update the stored balance value.

You could do something like this:
1
2
double b = account.getBalance();
b += interest ;

that would carry out the arithmetic you want. But it won't be able to update the stored balance because there is currently no mechanism to do that.
oh ok then that explains why it wouldn't work. Thank you for clearing that up. I was supposed to take a struct and turn it into a class. Maybe the purpose of this lesson was to see the difference of how they access their variables. So if you want to let the user control the input of the program then its probably best to use a struct.
Last edited on
There's no real need to switch back to a struct. If you really wanted to, you could make the members of a class public, just like a struct, or make the members of a struct private, like a class.

It's more about doing what is most appropriate. You clearly need some way to modify the account balance, but just changing it directly would be a bad design choice, as it is just too uncontrolled. What might be more appropriate would be to implement member functions to process various transactions such as crediting or debiting the account. Adding the interest might call such a function, or might itself be implemented as a member function.
Last edited on
I made a member function to do the calculations but the output is "N.#J" when it should be a number of type double can someone please help me with correcting the function MatureBalance. I'm pretty sure the problem is in lines 100-108. Thank you

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
using namespace std;


//class for a bank certificate of deposit:
class CDAccount
{   

    double balance;
    double interest_rate;
    int term;//months until maturity
    
public:
       CDAccount(); //default constructor (works)
       
       CDAccount (double balance, double interest_rate, int term); //constructor w/ arguments (works)
       
       void display()const; //display the CD Account (works)
       
       double Balance (double b); //enter the initial balance (works) 
      
       double IntrestRate (double ir); //enter the interest rate (works)
       
       int Term (int t); //enter the term in months (works)
       
       double getBalance (); // returns the balance (works)
       
       double getRate (); // returns the Interest Rate (works)
       
       int getTerm (); // returns the term (works)
       
       double MatureBalance (); //return balance at maturity (in progress)
       
};
CDAccount::CDAccount(): balance(0), interest_rate(0), term(0)
{}//default 

CDAccount::CDAccount (double  balance, double interest_rate, int term) :
   balance(balance), interest_rate(interest_rate), term(term)
{}//constructor 

void CDAccount::display() const // display the contents of the account
{
     cout << "the ballance is: " << balance << " the interest rate is: " << interest_rate << " the term in months is: " << term << endl;
}

double CDAccount::Balance (double b)// set balance
{
    cout << "please enter the account balance" << endl;
     cin >> b; 
     balance=b;
     if (balance < 0)
     {
     cout << "Bankrupt!!!\n"<< "You Need More Money!!!" << endl;
     system("pause");
     
     }
}

double CDAccount::IntrestRate (double ir) //set interest rate
{
       cout << "please enter the intrest rate as a decimal" << endl;
       cin >> ir;
       interest_rate=ir;
       if (interest_rate>=1.00 || interest_rate<=0)
       {
       cout << "improper input for rate" << endl;
       system("pause");
       
       }
}

int CDAccount::Term(int t) // set term
{
    cout << "please enter a term lenght from 1 to 12 months" << endl;
    cin >> t;
    term=t;
    if (term > 12 || term <= 0 )
    {
    cout << "improper input for term" << endl;
    system("pause");
    
    }
}

double CDAccount::getBalance () 
{
       return balance;
}

double CDAccount::getRate()
{
       return interest_rate;
}

int CDAccount::getTerm ()
{
    return term;
}
double CDAccount::MatureBalance() //returns the mature balance
{
       double matbal, interest, aterm;
       const int year=12;
       aterm = term / year;
       interest = balance * interest_rate / aterm;
       matbal = interest + balance;
       return matbal;
}
int main( )
{   
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    CDAccount accountOne(1000.0, .040, 8);
    accountOne.display();
    cout << "the ballance is " << accountOne.getBalance() << endl;
    cout << "the interest rate is " << accountOne.getRate() << endl;
    cout << "the Term is " << accountOne.getTerm() << " months" << endl;
    cout << "the balance at maturity will be: " << accountOne.MatureBalance() << endl;
   
   
    CDAccount accountTwo;
    accountTwo.Balance(0);
    accountTwo.IntrestRate(0);
    accountTwo.Term(0);

    
  
    cout << "When your CD matures in " 
         << accountTwo.getTerm() << " months,\n"
         << "it will have a balance of $" 
         << accountTwo.MatureBalance() << endl;
    accountTwo.display();
    system("pause");
    return 0;
}

division in line 105 should be multiply now the program works as intended
Topic archived. No new replies allowed.