Problem with my function

I have a weird issue, when I try and pull the return from my function it gives me a bunch of jumbled letters and numbers. I am obviously doing something wrong and would appreciate it if someone would show me.

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
#include <iostream>
using namespace std;

int counter;
int year;
int stop;

double bank(double balance, double interest)
{
	double account = 0;
	double amount;
while(counter > 0)
{
year++;
counter--;
}
account = (balance * interest) + account;
return account;
}


int main()
{
	double a;
	double b;

cout << "Input balance: ";
cin >> a;

cout << "Input Interest rate: ";
cin >> b;

cout << "Input Years until it takes affect: ";
cin >> counter;

cout << "Input what year you want it to stop at: ";
cin >> stop;

while(stop > 0)
{
year++;
stop--;
bank(a, b);

cout << "Year: " << year << "           Balance: " << bank << endl;
}
return 0;
}
1
2
3
4
5
while(stop > 0) { 
year++; 
stop--; 
cout << "Year: " << year << " Balance: " << bank(a, b)<< endl; 
}
wow ty, I have been running through this book for 19 ish hours if I missed something like that i think i need to take a break xD
If you want to do year++ you first need to initialize it int year = 0 or give it some value
Last edited on
Try to avoid using global variables: Lines 4-6.
Between curly brace
{ }
put an extra tab/indent.
If you ever do variable a = a + .. You can replace it with a += ...: Line 17
Though I really don't know why you are adding 0 to your account..

Line 43 is doing nothing. Yeah it calculates the stuff but you don't do anything with it... Maybe you meant something = bank(a , b )?
Or you could replace bank on line 45 with bank( a , b ) that is more than likely your error. Considering bank is undefined on line 45.

You also need to initialize a value to years.

*edit beat by the other 2
Last edited on
Topic archived. No new replies allowed.