B
Bimo Remus
Hi, I'm having problems with an assignment. We're supposed to take an
old assignment and rewrite it so that each task is carried out by a
different function. I just started this afternoon but have already
run into a wall.
When I try to compile the program, it seems to work, but after the
first cin, an error message pops up and it crashes. It might have
something to do with the pointers, seeing how I am still very shaky on
them (I'm also a little shaky on when to use some things with
functions), but I don't know.
And I should add that I am a student so I would appreciate it if
someone could point me in the right direction without telling exactly
what to do... but any help is much appreciated.
Thanks, Bimo
#include <iostream>
#include <cstdio>
using namespace std;
// prototype for a function that gets name and stores it
char *getName(void);
int main() {
char *person;
int month;
double years; // number of years invested
double rate; // yearly interest rate
double inv; // total investment
// collects customer information
person = getName();
cout << "Enter the initial investment ammount: ";
cin >> inv;
cout << "Enter interest rate: ";
cin >> rate;
cout << "How many years will the money be invested?: ";
cin >> years;
// calculate compounded investment
cout << "Investment table if " << inv << " at " << rate << "% for ";
cout << years << " years for " << person << " is:\n\n";
cout << "Year Monthly compounded\n";
for(int count = 0; count != years; count++)
for( month=1; month <= 12; month++) {
inv += inv * (rate/100/12);
if(month == 12) cout << " " << count + 1 << " " << inv << "\n";
}
return 0;
}
/* the Function, getName, asks the user for name and uses gets()
to store it in a character array.
Parameters: none
Return Values: *char
*/
char *getName(void) {
char *person; // this holds the string returned by *getname()
static char name[80];
cout << "Enter your name: ";
gets(person); // gets the name
return person; //returns result
}
old assignment and rewrite it so that each task is carried out by a
different function. I just started this afternoon but have already
run into a wall.
When I try to compile the program, it seems to work, but after the
first cin, an error message pops up and it crashes. It might have
something to do with the pointers, seeing how I am still very shaky on
them (I'm also a little shaky on when to use some things with
functions), but I don't know.
And I should add that I am a student so I would appreciate it if
someone could point me in the right direction without telling exactly
what to do... but any help is much appreciated.
Thanks, Bimo
#include <iostream>
#include <cstdio>
using namespace std;
// prototype for a function that gets name and stores it
char *getName(void);
int main() {
char *person;
int month;
double years; // number of years invested
double rate; // yearly interest rate
double inv; // total investment
// collects customer information
person = getName();
cout << "Enter the initial investment ammount: ";
cin >> inv;
cout << "Enter interest rate: ";
cin >> rate;
cout << "How many years will the money be invested?: ";
cin >> years;
// calculate compounded investment
cout << "Investment table if " << inv << " at " << rate << "% for ";
cout << years << " years for " << person << " is:\n\n";
cout << "Year Monthly compounded\n";
for(int count = 0; count != years; count++)
for( month=1; month <= 12; month++) {
inv += inv * (rate/100/12);
if(month == 12) cout << " " << count + 1 << " " << inv << "\n";
}
return 0;
}
/* the Function, getName, asks the user for name and uses gets()
to store it in a character array.
Parameters: none
Return Values: *char
*/
char *getName(void) {
char *person; // this holds the string returned by *getname()
static char name[80];
cout << "Enter your name: ";
gets(person); // gets the name
return person; //returns result
}