Problems with c++ homework

Discussion in 'Software' started by someguy85, Apr 11, 2009.

  1. someguy85

    someguy85 Private E-2

    I'm writing a program that calculates the population for each year end according to a user defined birth rate and death rate. It's displaying the starting population and the population of the first year end population (with the correct number), but it's not looping to show all the years. I can't seem to find the problem so I finally give in and beg for help once again. The code is below.

    Code:
    /* Jeff Hug
       COSC 1550 G1
       Homework 11 Exercise 2
       Chapter 6 Exercise 15 */
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    long startPop();
    void rates(double &, double &);
    double numYears();
    long newPop(long, double, double);
    
    int main()
    {
    	
    	long startingSize = startPop();
    	long pop = startingSize;
    	double birthRate = 0.0;
    	double deathRate = 0.0;
    
    	rates(birthRate, deathRate);
    
    	double yearsToCalculate = numYears();
    	
    	cout << "\n\n" << setw(20) << "Starting Population:" << setw(15) << startingSize << endl;
    	cout << "\nThe projected population at the end of:\n\n";
    	for(int year = 1; year <= yearsToCalculate; year++)
    	{
    		long newPopulation = newPop(pop, birthRate, deathRate);
    		
    		cout << "Year " << year << ":" << setw(15) << newPopulation << endl;
    		
    		pop = newPopulation;
    		
    		return 0;
    	}
    		
    
    }
    
    
    
    long startPop()
    {
    	long startingPop;
    	cout << "This program will determine the population based on birth and death rates.\n\n";
    	cout << "Please enter the starting population. ";
    	cin >> startingPop;
    
    	while (startingPop < 2)
    	{
    		cout << "\n\nThe starting population must be at least 2.\n";
    		cout << "Please re-enter the starting population. ";
    		cin >> startingPop;
    	}
    	return startingPop;
    }
    
    
    void rates(double &birth, double &death)
    {
    	cout << "\n\nEnter the birth rate (percentage increase of the population due to births). ";
    	cin >> birth;
    	while (birth < 0.0)
    	{
    		cout << "\n\nThe birth rate must be greater than or equal to zero.\n";
    		cout << "Re-enter the birth rate (percentage increase of the population due to births). ";
    		cin >> birth;
    	}
    	birth = birth / 100;
    
    	cout << "\n\nEnter the death rate (percentage decrease of the population due to deaths). ";
    	cin >> death;
    	while (death < 0.0)
    	{
    		cout << "\n\nThe death rate must be greater than or equal to zero.\n";
    		cout << "Re-enter the death rate (percentage decrease of the population due to deaths). ";
    		cin >> death;
    	}
    	death = death / 100;
    }
    
    
    double numYears()
    {
    	int howManyYears;
    	cout << "\n\nHow many years are you calculating the population for? ";
    	cin >> howManyYears;
    	while (howManyYears < 1)
    	{
    		cout << "\n\nYou must calculate for at least 1 year.\n";
    		cout << "Please re-enter the number of years you're calculating for. ";
    		cin >> howManyYears;
    	}
    	return howManyYears;
    }
    
    
    long newPop(long population, double rateBirth, double rateDeath)
    {
    	long newPop = static_cast<long>(population * (1 + rateBirth)*(1 - rateDeath));
    	
    	return newPop;
    }
    
     
  2. zlaser84

    zlaser84 Private E-2

    someguy85, welcome to MG!

    I hope you don't expect to use this forum as a very effective homework completion tool :-D That being said, like anything else C++ isn't very difficult to learn if you really want to. C++ requires hours upon hours of writing/debugging/revising your code in order to get a sound understanding of how this language works. What helped me when I started was handwriting - yes...gruesomely so, handwriting - every program my profs threw at me. I owe it to that simple yet tedious action that really opened my eyes to the functionality of C++, and it also helped me get through tests efficiently since I understood my code better and I didn't have to waste time typing things over and over again in a 55 minute time span.

    Anyway, enough about me, let's get to your problem. I can't give you the quick fix in hard code; you need to understand the code that comes from your fingertips. But I'll tell you this much: you're telling your loop to return a value that compiler considers an exit code ;)

    In the future, here are some tips you may want to follow:
    Remember that you're working w/a very powerful procedural programming language so you must develop an intimate knowledge of which data types are capable of what, how they are used/for what, etc, and it doesn't help to start developing a sense of structure in your coding style to adhere to the ways you see fit.

    Try setting up a stub - a bare bones program that displays what should be where during certain parts of program - then trial and error from there. Start w/main() that compiles/runs and shows what you want it to show.

    Return data type in function is usually same as function data type. Stay away from mixing and matching primitive data types as it has the potential to really skew your learning curve for C++ for the worse, and considering this is your first C++ course you don't wanna run any risk of skewing your learning curve so early on. Try to stick w/data types you are comfortable working with (i.e. I started just using ints, doubles, strings, and chars, then expanded to use of float, long, signed, unsigned, etc.)

    Try not to use basic parameter names as a place holder for a function return value, or instead append "Function" to parameter (e.g. parameterFunction = correlatingFunction(); ) to save yourself some serious :banghead when you have to debug your latter assignments that could include up to 500 or more lines of code (not to mention when you start creating you're own headers and other resource/source files).

    And remember, everything your profs (or others whose opinion you value for that matter) tell you to practice is always to better your coding style. It's all in good practice :major

    Happy coding!
     
  3. someguy85

    someguy85 Private E-2

    Solved

    Thanks for the help. I stupidly made that mistake because I initially forgot to enter a curly brace, then wasn't careful enough to make sure I knew where I was putting it, which placed the return statement in the loop. I guess I was just so tired at that point that I couldn't see that. Anyway, thanks again for your help. I really need to get better at my debugging skills (or lack thereof).
     
  4. someguy85

    someguy85 Private E-2

    I revised my program so the calculation would be more accurate after each year end and so the numbers entering the formula wouldn't get truncated which could lead to a big loss of accuracy with a big enough number or over a long period of time and everything's working great.

    One thing I'm confused about though, is how the output is getting properly rounded when all I did was set the fixed precision to 0. For example, if the new population comes out to be 1256.644, the output will display 1257. I ran the program a few times and found that it's rounding up or down as needed when I just expected it to truncate the output. I don't know if this is just a coincidence or what, but I was hoping someone could help shed some light on this. The revised code is below.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    long startPop();
    void rates(double &, double &);
    double numYears();
    long double newPop(long double, double, double);
    
    int main()
    {
    	
    	long startingSize = startPop();
    	long double pop = startingSize;
    	double birthRate = 0.0;
    	double deathRate = 0.0;
    
    	rates(birthRate, deathRate);
    
    	double yearsToCalculate = numYears();
    	
    	cout << "\n\n" << setw(20) << "Starting Population:" << setw(15) << startingSize << endl;
    	cout << "\nThe projected population at the end of:\n\n";
    	for(int year = 1; year <= yearsToCalculate; year++)
    	{
    		long double newPopulation = newPop(pop, birthRate, deathRate);
    
    		cout << fixed << setprecision(0) << endl;		
    		cout << "Year " << year << ":" << setw(15) << newPopulation << endl;
    		
    		pop = newPopulation;
    		
    		
    	}
    		return 0;
    
    }
    
    
    
    long startPop()
    {
    	long startingPop;
    	cout << "This program will determine the population based on birth and death rates.\n\n";
    	cout << "Please enter the starting population. ";
    	cin >> startingPop;
    
    	while (startingPop < 2)
    	{
    		cout << "\n\nThe starting population must be at least 2.\n";
    		cout << "Please re-enter the starting population. ";
    		cin >> startingPop;
    	}
    	return startingPop;
    }
    
    
    void rates(double &birth, double &death)
    {
    	cout << "\n\nEnter the birth rate (percentage increase of the population due to births). ";
    	cin >> birth;
    	while (birth < 0.0)
    	{
    		cout << "\n\nThe birth rate must be greater than or equal to zero.\n";
    		cout << "Re-enter the birth rate (percentage increase of the population due to births). ";
    		cin >> birth;
    	}
    	birth = birth / 100;
    
    	cout << "\n\nEnter the death rate (percentage decrease of the population due to deaths). ";
    	cin >> death;
    	while (death < 0.0)
    	{
    		cout << "\n\nThe death rate must be greater than or equal to zero.\n";
    		cout << "Re-enter the death rate (percentage decrease of the population due to deaths). ";
    		cin >> death;
    	}
    	death = death / 100;
    }
    
    
    double numYears()
    {
    	int howManyYears;
    	cout << "\n\nHow many years are you calculating the population for? ";
    	cin >> howManyYears;
    	while (howManyYears < 1)
    	{
    		cout << "\n\nYou must calculate for at least 1 year.\n";
    		cout << "Please re-enter the number of years you're calculating for. ";
    		cin >> howManyYears;
    	}
    	return howManyYears;
    }
    
    
    long double newPop(long double population, double rateBirth, double rateDeath)
    {
    	long double newPop = static_cast<long double>(population * (1.0 + rateBirth)*(1.0 - rateDeath));
    	
    	return newPop;
    }
    
     
  5. MutD

    MutD Specialist

    Nope, no coincidence. The setprecision line is doing it.

    Code:
    int main()
    {
    	
    	long startingSize = startPop();
    	long double pop = startingSize;
    	double birthRate = 0.0;
    	double deathRate = 0.0;
    
    	rates(birthRate, deathRate);
    
    	double yearsToCalculate = numYears();
    	
    	cout << "\n\n" << setw(20) << "Starting Population:" << setw(15) << startingSize << endl;
    	cout << "\nThe projected population at the end of:\n\n";
    	for(int year = 1; year <= yearsToCalculate; year++)
    	{
    		long double newPopulation = newPop(pop, birthRate, deathRate);
    
    		cout << fixed << [B]setprecision(0) [/B]<< endl;		
    		cout << "Year " << year << ":" << setw(15) << newPopulation << endl;
    		
    		pop = newPopulation;
    		
    		
    	}
    		return 0;
    
    }
    
     
  6. someguy85

    someguy85 Private E-2

    Oh, ok. I thought setprecision simply truncated. Thanks.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds