Another Function problem :-(

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 18
Reputation: Nick6425fl is an unknown quantity at this point 
Solved Threads: 0
Nick6425fl Nick6425fl is offline Offline
Newbie Poster

Another Function problem :-(

 
0
  #1
Nov 6th, 2008
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Another Function problem :-(

 
0
  #2
Nov 6th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 18
Reputation: Nick6425fl is an unknown quantity at this point 
Solved Threads: 0
Nick6425fl Nick6425fl is offline Offline
Newbie Poster

Re: Another Function problem :-(

 
0
  #3
Nov 6th, 2008
Thank you....that was my first post.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Another Function problem :-(

 
0
  #4
Nov 6th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 38
Reputation: asifjavaid is an unknown quantity at this point 
Solved Threads: 0
asifjavaid asifjavaid is offline Offline
Light Poster

Re: Another Function problem :-(

 
0
  #5
Nov 7th, 2008
Originally Posted by Nick6425fl View 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.

  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,

  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 18
Reputation: Nick6425fl is an unknown quantity at this point 
Solved Threads: 0
Nick6425fl Nick6425fl is offline Offline
Newbie Poster

Re: Another Function problem :-(

 
0
  #6
Nov 7th, 2008
Thank you for the reply, but Vernon already helped me out on a previous post in this thread.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC