| | |
Another Function problem :-(
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 18
Reputation:
Solved Threads: 0
I have 1 line in this code that I can't get to compile. I'm new to C++ and begging for some help...any advise would be appreciated.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; double population(double pop, double birthRate, double deathRate); void printPopulations( double startPop, double birthRate, double deathRate, int numYears); double population(double pop, double birthRate, double deathRate) { double n = 0; n = pop + (birthRate*pop) - (deathRate*pop); return n; } void printPopulations(double startPop, double birthRate, double deathRate, int numYears) { for ( int i = 0 ; i < numYears ; i++ ) { startPop = population (double birthRate, double deathRate, numYears, double n); cout << "For year " << i+1 << " the population is " << startPop << endl ; } } int main() { double startPop, birthRate, deathRate; int numYears; cout << "This program calculates population change.\n"; cout << "Enter the starting population size: "; cin >> startPop; while (startPop < 2.0) { cout << "Starting population must be 2 or more. Please re-enter: "; cin >> startPop; } cout << "Enter the annual birth rate (as % of current population): "; cin >> birthRate; while (birthRate < 0) { cout << "Birth rate percent cannot be negative. Please re-enter: "; cin >> birthRate; } birthRate = birthRate / 100; cout << "Enter the annual death rate (as % of current population): "; cin >> deathRate; while (deathRate < 0) { cout << "Death rate percent cannot be negative. Please re-enter: "; cin >> deathRate; } deathRate = deathRate / 100; cout << "For how many years do you wish to view population changes? "; cin >> numYears; while (numYears < 1) { cout << "Years must be one or more. Please re-enter: "; cin >> numYears; } printPopulations(startPop, birthRate, deathRate, numYears); system ("pause"); return 0; }
•
•
Join Date: Nov 2008
Posts: 18
Reputation:
Solved Threads: 0
Thank you....that was my first post.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <cstring> using namespace std; const int NAME_SIZE = 26; double getSales (char []); void findHighest(double, double, double, double); int main() { double input_div(string); double salesNE, salesSE, salesNW, salesSW; salesNE = getSales("Northeast"); salesSE = getSales("Southeast"); salesNW = getSales("Northwest"); salesSW = getSales("Southwest"); findHighest( salesNE, salesSE, salesNW, salesSW); cout << "Enter the quarterly sales for the Northeast division: "; cin >> salesNE; while (salesNE <=0) { cout << "Sales must be a non-negative number."; cout << "Enter the quarterly sales for the Northeast division: "; cin >> salesNE; } cout << "Enter the quarterly sales for the Southeast division: "; cin >> salesSE; while (salesSE <=0) { cout << "Sales must be a non-negative number."; cout << "Enter the quarterly sales for the Southeast division: "; cin >> salesSE; } cout << "Enter the quarterly sales for the Northwest division: "; cin >> salesNW; while (salesNW <=0) { cout << "Sales must be a non-negative number."; cout << "Enter the quarterly sales for the Northwest division: "; cin >> salesNW; } cout << "Enter the quarterly sales for the Southwest division: "; cin >> salesSW; while (salesSW <=0) { cout << "Sales must be a non-negative number."; cout << "Enter the quarterly sales for the Southwest division: "; cin >> salesSW; } } void findHighest(double salesNE, double salesSE, double salesNW, double salesSW) { string highest, name; highest = "salesNE"; name = "Northeast"; if(salesSE > highest) { highest = "salesSE"; name = "Southeast"; } if(salesNW > highest) { highest = "salesNW"; name = "Northwest"; } if(salesSW > highest) { highest = "salesSW"; name = "Southwest"; } cout << "The highest division is " << name << " with $" << highest << endl; } return 0; }
•
•
Join Date: Oct 2006
Posts: 38
Reputation:
Solved Threads: 0
•
•
•
•
I have 1 line in this code that I can't get to compile. I'm new to C++ and begging for some help...any advise would be appreciated.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; double population(double pop, double birthRate, double deathRate); void printPopulations( double startPop, double birthRate, double deathRate, int numYears); double population(double pop, double birthRate, double deathRate) { double n = 0; n = pop + (birthRate*pop) - (deathRate*pop); return n; } void printPopulations(double startPop, double birthRate, double deathRate, int numYears) { for ( int i = 0 ; i < numYears ; i++ ) { startPop = population (double birthRate, double deathRate, numYears, double n); cout << "For year " << i+1 << " the population is " << startPop << endl ; } } int main() { double startPop, birthRate, deathRate; int numYears; cout << "This program calculates population change.\n"; cout << "Enter the starting population size: "; cin >> startPop; while (startPop < 2.0) { cout << "Starting population must be 2 or more. Please re-enter: "; cin >> startPop; } cout << "Enter the annual birth rate (as % of current population): "; cin >> birthRate; while (birthRate < 0) { cout << "Birth rate percent cannot be negative. Please re-enter: "; cin >> birthRate; } birthRate = birthRate / 100; cout << "Enter the annual death rate (as % of current population): "; cin >> deathRate; while (deathRate < 0) { cout << "Death rate percent cannot be negative. Please re-enter: "; cin >> deathRate; } deathRate = deathRate / 100; cout << "For how many years do you wish to view population changes? "; cin >> numYears; while (numYears < 1) { cout << "Years must be one or more. Please re-enter: "; cin >> numYears; } printPopulations(startPop, birthRate, deathRate, numYears); system ("pause"); return 0; }
hi,
your mistake is here,
C++ Syntax (Toggle Plain Text)
void printPopulations(double startPop, double birthRate, double deathRate, int numYears) { for ( int i = 0 ; i < numYears ; i++ ) { startPop = population (double birthRate, double deathRate, numYears, double n); //---> do not include datatypes while calling any function okay. //-----------------------------------------------------------------------// startPop = population (birthRate, deathRate, numYears, n); //-----> this is correct calling. //-----------------------------------------------------------------------// cout << "For year " << i+1 << " the population is " << startPop << endl ; } }
do not include data types while calling any function okay. Datatypes are part function declaration.
--
Regards,
Asif
![]() |
Similar Threads
- Insertion Sort Problem (C++)
- Ask Function problem?thanks (C)
- Abstract Class member function problem (C++)
- Function Problem. Please help (PHP)
- using OCI to call a ORACLE stored function (C++)
- static function problem (C++)
- AnsiString Template Data Return Problem Builder 6 (C++)
Other Threads in the C++ Forum
- Previous Thread: [Borland C++ Builder] ListBox Multiple Selection
- Next Thread: How to make macros for this?
| Thread Tools | Search this Thread |
api application array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






