Help with homework

Still stuck a little on pointers.

I currently have three classes.

The Main
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
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "finance.h"
#include <iomanip>

using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
	Finance myFinance;
	myFinance.x=7;
	
	for (char response=' '; response != 'x'; ) 
	{
		cout << "This program is designed to calculate a Maturity value of an investment or \n";
		cout << "the monthly payments on a loan.  Please enter the 1 or 2. \n";
		cout << "1) Maturity Calculator \n";
		cout << "2) Monthly Loan Payments \n";
		cin >> response;
        switch (response)
        {

    case '1':
        myFinance.maturitydriver();
        break;
    case '2':
        myFinance.loanDriver();
        break;
    default: ;
    }
	}
	 
	return 0;
}


Finance.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
class Finance
{
public:
	Finance(void);
	~Finance(void);

public:
	double MaturityCalc (double invest, float interestR, float years, int compound);
		
		int maturitydriver ();
		int x;
	
public:
		double loanPay (int loan, float rate, int payment);
		int loanDriver ();
		

};


Finance.cpp
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
#include "StdAfx.h"
#include "Finance.h"
#include <iostream>
#include <math.h.>
#include <iomanip>
using namespace std;


Finance::Finance(void)
{
}


Finance::~Finance(void)
{
}
double Finance::MaturityCalc (double invest, float interestR, float years, int compound)
{	
	double interestT;
	double epon;

	interestT = (1+((interestR/100) / compound)); // =(1+P/M)
	epon = (years * compound); //^(Y*M)

	double result = invest * pow (interestT, epon); //Finally, multiply the investment into the total interest

	return result;
}
int Finance::maturitydriver()
{
	double invest;	
	float interestR; 
	float years;	
	int compound;	
	double maturity; 

	cout << "This program will calcuate a maturity.  Please enter the following: \n";
		
	//ask user for input
	cout << "\nInital investment: ";
	cin >> invest;
	cout << "\n";
	
	cout << "Interest rate, in percentage: ";
	cin >> interestR;
	cout << "\n";
	
	cout << "Total years of maturity: ";
	cin >> years;
	cout << "\n";
	
	cout << "Compound, number of times interest is applied: ";
	cin >> compound;
	cout << "\n";

	maturity = MaturityCalc(invest, interestR, years, compound);
	
	cout << "Your maturity is: " << maturity << "\n";

	system ("pause");
	return 0;
}

double Finance::loanPay (int loan, float rate, int payment)
{
		float baseLoan = 0;		
		double numer = 0;		
		double denom = 0;		
		double resultB = 0;		
		double resultF = 0;		
		double resultT = 0;		

		baseLoan = (1 + (rate/1200));						
		resultB = pow ((double) baseLoan, (double) payment); 
		numer = ((double) rate/1200) * resultB;			
		denom = resultB - 1;							 
		resultF = numer / denom;						 
		resultT = resultF * loan;						 
	
	return resultT;
}
int Finance::loanDriver()
{
	cout << "This program will calculate your monthly loan payments,\n";
	cout << "please enter the following: \n";

		int loan, payment = 0;  //L and N, Loan amount and number of payments
		float rate = 0;			//R, interest rate
		double getLoan = 0; //calls getloan
	
		cout << "\nLoan amount: ";
		cin >> loan;
		cout << "\n";
	
		cout << "Interest Rate, in percentage: ";
		cin >> rate;
		cout << "\n";

		cout << "Number of payments: ";
		cin >> payment;
		cout << "\n";

		getLoan = loanPay(loan,rate,payment);

		cout << "Your monthly loan payment is: " << getLoan << "\n"; //display results

		system ("pause");
		return 0;
}


What I'm trying to do is add a swap function and a swapdriver I believe.
Add a new function to your class called swap.
• The swap function takes two variables as parameters, swaps their value and returns both values to the
calling function. Notice that a return keyword can only return one value. We now have to return 2
values, so we can not use the return keyword for this purpose. The variables need to passed to this
function as pointers. The swap function will need a temporary variable as a temporary holder as it
swaps the values in the two variables.
• Add a swapDriver function to your class as well.
• Add a new item to your menu that calls swapDriver
• Test the swapDriver and swap functions by running the program and selecting the “Swap values” menu
item.


Can anyone help me with this?

All I have so far is
1
2
3
4
5
6
7
8
9
10
11
12
void swapDriver ()
{
int x;
int y;
      cout << "\nPlease enter x: ";
      cin >> x;
      cout << "\nPlease enter y: ";
      cin >> y;
      cin << "\n Here is the x value: "<< x << " and the y value: " << y;
      swap (// blank // );
      cout << "\n After swap: Here is x: "<< x << " and y: " << y;
}


and
1
2
3
4
void swap (// you need to figure out how to declare the parameter list//)
{
// need to come up with the code that allows you to do the swapping of the values
}


Any help would be appreciated.
this is not OK:
cin << "\n Here is the x value: "<< x << " and the y value: " << y

this is some "template" to help you figure out how to swap
1
2
3
4
5
6
void swap(TYPE& arg1, TYPE& arg2) {
    TYPE temp = arg1;
     arg1 = arg2;
     arg2 = temp;
    return;
}
Last edited on
attempted it and got this
void swapDriver () 
{ 
int x; 
int y; 
 cout << "\nPlease enter x: "; 
 cin >> x; 
 cout << "\nPlease enter y: "; 
 cin >> y; 
 swap (x, y);
 cout << "\n After swap: Here is x: "<< x << " and y: " << y; 

}


void swap( int x, int y)
{
int p1=x, p2=y;
int temp = p1;
p1 = p2;
p2 = temp;
return;
}


1>c:\users\andy luong\documents\visual studio 2010\projects\assign8\assign8\finance.h(20): error C2628: 'Finance' followed by 'void' is illegal (did you forget a ';'?)
1>c:\users\andy luong\documents\visual studio 2010\projects\assign8\assign8\finance.h(26): error C2561: 'swap' : function must return a value
1> c:\users\andy luong\documents\visual studio 2010\projects\assign8\assign8\finance.h(20) : see declaration of 'swap'
1> assign8.cpp
1>c:\users\andy luong\documents\visual studio 2010\projects\assign8\assign8\finance.h(20): error C2628: 'Finance' followed by 'void' is illegal (did you forget a ';'?)
1>c:\users\andy luong\documents\visual studio 2010\projects\assign8\assign8\finance.h(26): error C2561: 'swap' : function must return a value
1> c:\users\andy luong\documents\visual studio 2010\projects\assignt8\assign8\finance.h(20) : see declaration of 'swap'
Last edited on
1
2
3
4
5
6
7
8
void swap( int x, int y)
{
int p1=x, p2=y;
int temp = p1;
p1 = p2;
p2 = temp;
return;
}


swap arguments must be pased by refenrence or by pointer to swap them for real.

and this is bad:
1
2
3
4
5
6
7
8
Finance(void);
~Finance(void);
Finance::Finance(void)
{
}
Finance::~Finance(void)
{
}

that's some C stile, it could mean that constructor takes some void parameter but acctualy takes no parameter.
so remove void.

EDIT:
litle tip for you,
double click on each output error to move cursor to that line and see what's error.
Last edited on
all i can seem to get is that something is illegal and that something is missing..

the main
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
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "finance.h"
#include <iomanip>

using namespace std;



int _tmain(int argc, _TCHAR* argv[])
	{Finance myFinance;
	myFinance.x=7;
	
	for (char response=' '; response != 'x'; ) 
	{
		cout << "This program is designed to calculate a Maturity value of an investment or \n";
		cout << "the monthly payments on a loan.  Please enter the 1 or 2. \n";
		cout << "1) Maturity Calculator \n";
		cout << "2) Monthly Loan Payments \n";
		cout << "3) Swap Values \n";
		cin >> response;
        switch (response)
        {

    case '1':
        myFinance.maturitydriver();
        break;
    case '2':
        myFinance.loanDriver();
        break;
	case '3':
		myFinance.swapDriver();
		break;
    }
	}
	 
	return 0;
}


finance.cpp
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
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "Finance.h"
#include <iomanip>
using namespace std;

double Finance::MaturityCalc (double invest, float interestR, float years, int compound)
{	
	double interestT;
	double epon;

	interestT = (1+((interestR/100) / compound)); // =(1+P/M)
	epon = (years * compound); //^(Y*M)

	double result = invest * pow (interestT, epon); //Finally, multiply the investment into the total interest

	return result;
}
int Finance::maturitydriver()
{
	double invest;	
	float interestR; 
	float years;	
	int compound;	
	double maturity; 

	cout << "This program will calcuate a maturity.  Please enter the following: \n";
		
	//ask user for input
	cout << "\nInital investment: ";
	cin >> invest;
	cout << "\n";
	
	cout << "Interest rate, in percentage: ";
	cin >> interestR;
	cout << "\n";
	
	cout << "Total years of maturity: ";
	cin >> years;
	cout << "\n";
	
	cout << "Compound, number of times interest is applied: ";
	cin >> compound;
	cout << "\n";

	maturity = MaturityCalc(invest, interestR, years, compound);
	
	cout << "Your maturity is: " << maturity << "\n";

	system ("pause");
	return 0;
}

double Finance::loanPay (int loan, float rate, int payment)
{
		float baseLoan = 0;		
		double numer = 0;		
		double denom = 0;		
		double resultB = 0;		
		double resultF = 0;		
		double resultT = 0;		

		baseLoan = (1 + (rate/1200));						
		resultB = pow ((double) baseLoan, (double) payment); 
		numer = ((double) rate/1200) * resultB;			
		denom = resultB - 1;							 
		resultF = numer / denom;						 
		resultT = resultF * loan;						 
	
	return resultT;
}
int Finance::loanDriver()
{
	cout << "This program will calculate your monthly loan payments,\n";
	cout << "please enter the following: \n";

		int loan, payment = 0;  //L and N, Loan amount and number of payments
		float rate = 0;			//R, interest rate
		double getLoan = 0; //calls getloan
	
		cout << "\nLoan amount: ";
		cin >> loan;
		cout << "\n";
	
		cout << "Interest Rate, in percentage: ";
		cin >> rate;
		cout << "\n";

		cout << "Number of payments: ";
		cin >> payment;
		cout << "\n";

		getLoan = loanPay(loan,rate,payment);

		cout << "Your monthly loan payment is: " << getLoan << "\n"; //display results

		system ("pause");
		return 0;
};

int swapDriver () 
{ 
int x; 
int y; 
 cout << "\nPlease enter x: "; 
 cin >> x; 
 cout << "\nPlease enter y: "; 
 cin >> y; 
 swap (x, y);
 cout << "\n After swap: Here is x: "<< x << " and y: " << y; 

}


finance.h
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
#pragma once
class Finance
{

public:
	double MaturityCalc (double invest, float interestR, float years, int compound);
		
		int maturitydriver ();
		int x;
	
public:
		double loanPay (int loan, float rate, int payment);
		int loanDriver ();
}

void swap(int x, int y);
{

int swapDriver;
int x=3, y=4;
int temp = x;
x = y;
y = temp;
return;
}

    
	


no matter what i do i keep ending up getting this...
1>c:\users\andy luong\documents\visual studio 2010\projects\assignment8\assignment8\finance.h(16): error C2628: 'Finance' followed by 'void' is illegal (did you forget a ';'?)
1>c:\users\andy luong\documents\visual studio 2010\projects\assignment8\assignment8\finance.h(17): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\andy luong\documents\visual studio 2010\projects\assignment8\assignment8\assignment8.cpp(39): error C2039: 'swapDriver' : is not a member of 'Finance'
1>          c:\users\andy luong\documents\visual studio 2010\projects\assignment8\assignment8\finance.h(3) : see declaration of 'Finance'
1>  Finance.cpp
1>c:\users\andy luong\documents\visual studio 2010\projects\assignment8\assignment8\finance.h(16): error C2628: 'Finance' followed by 'void' is illegal (did you forget a ';'?)
1>c:\users\andy luong\documents\visual studio 2010\projects\assignment8\assignment8\finance.h(17): error C2447: '{' : missing function header (old-style formal list?)
1>  Finanace.cpp
1>c:\users\andy luong\documents\visual studio 2010\projects\assignment8\assignment8\finanace.cpp(1): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?


im about ready to give up, this is driving me nuts!!!
Have you read the error messages?

Your very first error message is telling you exactly where to look - finance.h, line 16.
It then says that it's illegal to have 'void' immediately after the definition of Finance.
It then suggests you might have missed a semi-colon.

By deduction, you're missing a semi-colon after your Finance class declaration.

Fix that one and see how many more errors remain.

Cheers,
Jim
1>c:\users\andy luong\documents\visual studio 2010\projects\assignment8\assignment8\finanace.cpp(1): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?


i get this but i already have #include stdafx.h

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
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "Finance.h"
#include <iomanip>
using namespace std;

double Finance::MaturityCalc (double invest, float interestR, float years, int compound)
{	
	double interestT;
	double epon;

	interestT = (1+((interestR/100) / compound)); // =(1+P/M)
	epon = (years * compound); //^(Y*M)

	double result = invest * pow (interestT, epon); //Finally, multiply the investment into the total interest

	return result;
}
int Finance::maturitydriver()
{
	double invest;	
	float interestR; 
	float years;	
	int compound;	
	double maturity; 

	cout << "This program will calcuate a maturity.  Please enter the following: \n";
		
	//ask user for input
	cout << "\nInital investment: ";
	cin >> invest;
	cout << "\n";
	
	cout << "Interest rate, in percentage: ";
	cin >> interestR;
	cout << "\n";
	
	cout << "Total years of maturity: ";
	cin >> years;
	cout << "\n";
	
	cout << "Compound, number of times interest is applied: ";
	cin >> compound;
	cout << "\n";

	maturity = MaturityCalc(invest, interestR, years, compound);
	
	cout << "Your maturity is: " << maturity << "\n";

	system ("pause");
	return 0;
}

double Finance::loanPay (int loan, float rate, int payment)
{
		float baseLoan = 0;		
		double numer = 0;		
		double denom = 0;		
		double resultB = 0;		
		double resultF = 0;		
		double resultT = 0;		

		baseLoan = (1 + (rate/1200));						
		resultB = pow ((double) baseLoan, (double) payment); 
		numer = ((double) rate/1200) * resultB;			
		denom = resultB - 1;							 
		resultF = numer / denom;						 
		resultT = resultF * loan;						 
	
	return resultT;
}
int Finance::loanDriver()
{
	cout << "This program will calculate your monthly loan payments,\n";
	cout << "please enter the following: \n";

		int loan, payment = 0;  //L and N, Loan amount and number of payments
		float rate = 0;			//R, interest rate
		double getLoan = 0; //calls getloan
	
		cout << "\nLoan amount: ";
		cin >> loan;
		cout << "\n";
	
		cout << "Interest Rate, in percentage: ";
		cin >> rate;
		cout << "\n";

		cout << "Number of payments: ";
		cin >> payment;
		cout << "\n";

		getLoan = loanPay(loan,rate,payment);

		cout << "Your monthly loan payment is: " << getLoan << "\n"; //display results

		system ("pause");
		return 0;
};

int swapDriver () 
{ 
int x; 
int y; 
 cout << "\nPlease enter x: "; 
 cin >> x; 
 cout << "\nPlease enter y: "; 
 cin >> y; 
 swap (x, y);
 cout << "\n After swap: Here is x: "<< x << " and y: " << y; 
 system ("pause");
 return 0;
}
Long shot, file name may be case sensitive, but I doubt it.
lol sorry about that. and sorry if im pestering anyone.. but now another error comes up
1>Finance.obj : error LNK2005: "void __cdecl swap(int,int)" (?swap@@YAXHH@Z) already defined in assign8.obj
1>c:\users\andy luong\documents\visual studio 2010\Projects\assign8\Debug\assign8.exe : fatal error LNK1169: one or more multiply defined symbols found


finance.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once
class Finance
{

public:
	double MaturityCalc (double invest, float interestR, float years, int compound);
		
		int maturitydriver ();
		int x;
	
public:
		double loanPay (int loan, float rate, int payment);
		int loanDriver ();
};

void swap (int x, int y)
{
int temp = x;
x = y;
y = temp;
return;
}


finance.cpp
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
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "Finance.h"
#include <iomanip>
using namespace std;

double Finance::MaturityCalc (double invest, float interestR, float years, int compound)
{	
	double interestT;
	double epon;

	interestT = (1+((interestR/100) / compound)); // =(1+P/M)
	epon = (years * compound); //^(Y*M)

	double result = invest * pow (interestT, epon); //Finally, multiply the investment into the total interest

	return result;
}
int Finance::maturitydriver()
{
	double invest;	
	float interestR; 
	float years;	
	int compound;	
	double maturity; 

	cout << "This program will calcuate a maturity.  Please enter the following: \n";
		
	//ask user for input
	cout << "\nInital investment: ";
	cin >> invest;
	cout << "\n";
	
	cout << "Interest rate, in percentage: ";
	cin >> interestR;
	cout << "\n";
	
	cout << "Total years of maturity: ";
	cin >> years;
	cout << "\n";
	
	cout << "Compound, number of times interest is applied: ";
	cin >> compound;
	cout << "\n";

	maturity = MaturityCalc(invest, interestR, years, compound);
	
	cout << "Your maturity is: " << maturity << "\n";

	system ("pause");
	return 0;
}

double Finance::loanPay (int loan, float rate, int payment)
{
		float baseLoan = 0;		
		double numer = 0;		
		double denom = 0;		
		double resultB = 0;		
		double resultF = 0;		
		double resultT = 0;		

		baseLoan = (1 + (rate/1200));						
		resultB = pow ((double) baseLoan, (double) payment); 
		numer = ((double) rate/1200) * resultB;			
		denom = resultB - 1;							 
		resultF = numer / denom;						 
		resultT = resultF * loan;						 
	
	return resultT;
}
int Finance::loanDriver()
{
	cout << "This program will calculate your monthly loan payments,\n";
	cout << "please enter the following: \n";

		int loan, payment = 0;  //L and N, Loan amount and number of payments
		float rate = 0;			//R, interest rate
		double getLoan = 0; //calls getloan
	
		cout << "\nLoan amount: ";
		cin >> loan;
		cout << "\n";
	
		cout << "Interest Rate, in percentage: ";
		cin >> rate;
		cout << "\n";

		cout << "Number of payments: ";
		cin >> payment;
		cout << "\n";

		getLoan = loanPay(loan,rate,payment);

		cout << "Your monthly loan payment is: " << getLoan << "\n"; //display results

		system ("pause");
		return 0;
};

int swapDriver () 
{ 
	int x;
	int y;
 cout << "\nPlease enter x: "; 
 cin >> x; 
 cout << "\nPlease enter y: "; 
 cin >> y; 
 swap (x, y);
 cout << "\n After swap: Here is x: "<< x << " and y: " << y; 
 system ("pause");
 return 0;
}


main.cpp
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
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "Finance.h"
#include <iomanip>

using namespace std;



int _tmain(int argc, _TCHAR* argv[])
	{Finance myFinance;
	myFinance.x=7;
	
	for (char response=' '; response != 'x'; ) 
	{
		cout << "This program is designed to calculate a Maturity value of an investment or \n";
		cout << "the monthly payments on a loan.  Please enter the 1 or 2. \n";
		cout << "1) Maturity Calculator \n";
		cout << "2) Monthly Loan Payments \n";
		cin >> response;
        switch (response)
        {

    case '1':
        myFinance.maturitydriver();
        break;
    case '2':
        myFinance.loanDriver();
        break;
    }
	}
	 
	return 0;
}


It's because your definition of swap() (the actual body) is in the Finance.h header file, and that's being included twice - once from main.cpp and once from Finance.cpp, resulting in two copies of the function.

Move the definition into Finance.cpp to match swapDriver().

Jim
exactly,
header files may contain declarations only, in your case header which is included into main.cpp must contain swap declaration.
and definition may be located only once in "Finance.cpp" (only once in whole project).

I told you to pass arguments as reference:
1
2
3
4
5
6
7
void swap (int x, int y)
{
int temp = x;
x = y;
y = temp;
return;
}


like this:
1
2
3
void swap (int& x, int& y) {
   //....
}
Topic archived. No new replies allowed.