943,754 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 491
  • C++ RSS
Nov 6th, 2008
0

Another Function problem :-(

Expand Post »
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)
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. double population(double pop, double birthRate, double deathRate);
  7. void printPopulations(
  8. double startPop, double birthRate, double deathRate, int numYears);
  9.  
  10.  
  11. double population(double pop, double birthRate, double deathRate)
  12. {
  13.  
  14. double n = 0;
  15.  
  16. n = pop + (birthRate*pop) - (deathRate*pop);
  17.  
  18. return n;
  19.  
  20. }
  21.  
  22. void printPopulations(double startPop, double birthRate, double deathRate, int numYears)
  23. {
  24.  
  25. for ( int i = 0 ; i < numYears ; i++ )
  26. {
  27. startPop = population (double birthRate, double deathRate, numYears, double n);
  28. cout << "For year " << i+1 << " the population is " << startPop << endl ;
  29. }
  30.  
  31. }
  32.  
  33.  
  34.  
  35. int main()
  36. {
  37. double startPop,
  38. birthRate,
  39. deathRate;
  40. int numYears;
  41.  
  42.  
  43. cout << "This program calculates population change.\n";
  44. cout << "Enter the starting population size: ";
  45. cin >> startPop;
  46. while (startPop < 2.0)
  47. {
  48. cout << "Starting population must be 2 or more. Please re-enter: ";
  49. cin >> startPop;
  50. }
  51.  
  52.  
  53. cout << "Enter the annual birth rate (as % of current population): ";
  54. cin >> birthRate;
  55. while (birthRate < 0)
  56. {
  57. cout << "Birth rate percent cannot be negative. Please re-enter: ";
  58. cin >> birthRate;
  59. }
  60.  
  61. birthRate = birthRate / 100;
  62.  
  63. cout << "Enter the annual death rate (as % of current population): ";
  64. cin >> deathRate;
  65. while (deathRate < 0)
  66. {
  67. cout << "Death rate percent cannot be negative. Please re-enter: ";
  68. cin >> deathRate;
  69. }
  70.  
  71. deathRate = deathRate / 100;
  72.  
  73. cout << "For how many years do you wish to view population changes? ";
  74. cin >> numYears;
  75. while (numYears < 1)
  76. {
  77. cout << "Years must be one or more. Please re-enter: ";
  78. cin >> numYears;
  79. }
  80.  
  81. printPopulations(startPop, birthRate, deathRate, numYears);
  82. system ("pause");
  83. return 0;
  84. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nick6425fl is offline Offline
18 posts
since Nov 2008
Nov 6th, 2008
0

Re: Another Function problem :-(

Which line? You can either use C++ code tags:


[cose=cplusplus]
// paste code here
[/code]


which add line numbers, and you can reference the line number, or you can highlight the relevant line in red. Give the compiler error too please.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,373 posts
since Jan 2008
Nov 6th, 2008
0

Re: Another Function problem :-(

Thank you....that was my first post.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. const int NAME_SIZE = 26;
  10.  
  11.  
  12. double getSales (char []);
  13. void findHighest(double, double, double, double);
  14.  
  15. int main()
  16. {
  17.  
  18. double input_div(string);
  19. double salesNE,
  20. salesSE,
  21. salesNW,
  22. salesSW;
  23.  
  24.  
  25. salesNE = getSales("Northeast");
  26. salesSE = getSales("Southeast");
  27. salesNW = getSales("Northwest");
  28. salesSW = getSales("Southwest");
  29.  
  30.  
  31. findHighest( salesNE, salesSE, salesNW, salesSW);
  32.  
  33.  
  34. cout << "Enter the quarterly sales for the Northeast division: ";
  35. cin >> salesNE;
  36.  
  37. while (salesNE <=0)
  38. {
  39. cout << "Sales must be a non-negative number.";
  40. cout << "Enter the quarterly sales for the Northeast division: ";
  41. cin >> salesNE;
  42.  
  43. }
  44. cout << "Enter the quarterly sales for the Southeast division: ";
  45. cin >> salesSE;
  46. while (salesSE <=0)
  47. {
  48. cout << "Sales must be a non-negative number.";
  49. cout << "Enter the quarterly sales for the Southeast division: ";
  50. cin >> salesSE;
  51. }
  52.  
  53. cout << "Enter the quarterly sales for the Northwest division: ";
  54. cin >> salesNW;
  55. while (salesNW <=0)
  56. {
  57. cout << "Sales must be a non-negative number.";
  58. cout << "Enter the quarterly sales for the Northwest division: ";
  59. cin >> salesNW;
  60. }
  61. cout << "Enter the quarterly sales for the Southwest division: ";
  62. cin >> salesSW;
  63. while (salesSW <=0)
  64. {
  65. cout << "Sales must be a non-negative number.";
  66. cout << "Enter the quarterly sales for the Southwest division: ";
  67. cin >> salesSW;
  68.  
  69. }
  70. }
  71. void findHighest(double salesNE, double salesSE, double salesNW, double salesSW)
  72. {
  73. string highest, name;
  74. highest = "salesNE";
  75. name = "Northeast";
  76.  
  77. if(salesSE > highest)
  78. {
  79. highest = "salesSE";
  80. name = "Southeast";
  81. }
  82.  
  83. if(salesNW > highest)
  84. {
  85. highest = "salesNW";
  86. name = "Northwest";
  87. }
  88.  
  89. if(salesSW > highest)
  90. {
  91. highest = "salesSW";
  92. name = "Southwest";
  93. }
  94.  
  95. cout << "The highest division is " << name << " with $" << highest << endl;
  96. }
  97.  
  98. return 0;
  99. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nick6425fl is offline Offline
18 posts
since Nov 2008
Nov 6th, 2008
0

Re: Another Function problem :-(

Okay, which line number is the problem on though? Actually, this is the same as your other thread, right? I just commented there. You should mark this one "solved" since you have two threads on the same topic.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,373 posts
since Jan 2008
Nov 7th, 2008
0

Re: Another Function problem :-(

Click to Expand / Collapse  Quote originally posted by Nick6425fl ...
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)
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. double population(double pop, double birthRate, double deathRate);
  7. void printPopulations(
  8. double startPop, double birthRate, double deathRate, int numYears);
  9.  
  10.  
  11. double population(double pop, double birthRate, double deathRate)
  12. {
  13.  
  14. double n = 0;
  15.  
  16. n = pop + (birthRate*pop) - (deathRate*pop);
  17.  
  18. return n;
  19.  
  20. }
  21.  
  22. void printPopulations(double startPop, double birthRate, double deathRate, int numYears)
  23. {
  24.  
  25. for ( int i = 0 ; i < numYears ; i++ )
  26. {
  27. startPop = population (double birthRate, double deathRate, numYears, double n);
  28. cout << "For year " << i+1 << " the population is " << startPop << endl ;
  29. }
  30.  
  31. }
  32.  
  33.  
  34.  
  35. int main()
  36. {
  37. double startPop,
  38. birthRate,
  39. deathRate;
  40. int numYears;
  41.  
  42.  
  43. cout << "This program calculates population change.\n";
  44. cout << "Enter the starting population size: ";
  45. cin >> startPop;
  46. while (startPop < 2.0)
  47. {
  48. cout << "Starting population must be 2 or more. Please re-enter: ";
  49. cin >> startPop;
  50. }
  51.  
  52.  
  53. cout << "Enter the annual birth rate (as % of current population): ";
  54. cin >> birthRate;
  55. while (birthRate < 0)
  56. {
  57. cout << "Birth rate percent cannot be negative. Please re-enter: ";
  58. cin >> birthRate;
  59. }
  60.  
  61. birthRate = birthRate / 100;
  62.  
  63. cout << "Enter the annual death rate (as % of current population): ";
  64. cin >> deathRate;
  65. while (deathRate < 0)
  66. {
  67. cout << "Death rate percent cannot be negative. Please re-enter: ";
  68. cin >> deathRate;
  69. }
  70.  
  71. deathRate = deathRate / 100;
  72.  
  73. cout << "For how many years do you wish to view population changes? ";
  74. cin >> numYears;
  75. while (numYears < 1)
  76. {
  77. cout << "Years must be one or more. Please re-enter: ";
  78. cin >> numYears;
  79. }
  80.  
  81. printPopulations(startPop, birthRate, deathRate, numYears);
  82. system ("pause");
  83. return 0;
  84. }


hi,

your mistake is here,

C++ Syntax (Toggle Plain Text)
  1. void printPopulations(double startPop, double birthRate, double deathRate, int numYears)
  2. {
  3.  
  4. for ( int i = 0 ; i < numYears ; i++ )
  5. {
  6. startPop = population (double birthRate, double deathRate, numYears, double n); //---> do not include datatypes while calling any function okay.
  7. //-----------------------------------------------------------------------//
  8. startPop = population (birthRate, deathRate, numYears, n); //-----> this is correct calling.
  9. //-----------------------------------------------------------------------//
  10.  
  11.  
  12. cout << "For year " << i+1 << " the population is " << startPop << endl ;
  13. }
  14.  
  15. }



do not include data types while calling any function okay. Datatypes are part function declaration.

--
Regards,
Asif
Reputation Points: 10
Solved Threads: 0
Light Poster
asifjavaid is offline Offline
40 posts
since Oct 2006
Nov 7th, 2008
0

Re: Another Function problem :-(

Thank you for the reply, but Vernon already helped me out on a previous post in this thread.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nick6425fl is offline Offline
18 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: [Borland C++ Builder] ListBox Multiple Selection
Next Thread in C++ Forum Timeline: How to make macros for this?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC