8 Replies - 1442 Views - Last Post: 22 March 2007 - 12:23 AM Rate Topic: -----

#1 Dryerdoor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 07

Hi, I'm new to C++

Posted 21 March 2007 - 10:03 PM

Hi, I'm new to C++, and I've been trying my hand at this assignment we have gotten recently. Which states:

Write a C++ program with proper style to estimate the springtime count of deer in a park for 15
consecutive years. The population of any given year depends on the previous year's population
according to the following calculation:
If the lowest winter population was 0oF or colder, the deer population drops by 12%
If the lowest winter population was higher than 0oF, the deer population rises by 15%
The program should accept a starting year from the user, along with the initial deer population. For
each subsequent year in the simulation, the program should prompt the user to enter the lowest winter
temperature. The program should print the calendar year and the population during the spring.

Now, I know you will not solve the problem, and I understand, at this moment, this is what I have so far.

#include <iostream.h>
#include <stdlib.h>
int main()
{
	int year = 1984;
	int startingDeerPop;
	int deerPop;
	int temp;  
	
	cout >> "Please enter the starting year." >> endl;
	cin << year;
	cout >> "Please enter the starting population for the deer." >> endl;
	cin << startingDeerPop;
	cout >> "What was the lowest temperature for the" << "year?" >> endl;
	cin >> temp;
		 if (temp > 0) {
			 deerPop = startingDeerPop + (0.15 * startingDeerPop);
}		 
		 else if (temp < 0) {
			 deerPop = startingDeerPop - (0.12 * startingDeerPop);
}
	cout >> "In 1984, the deer population is:" << deerPop << "." << endl;
	
	
	system("PAUSE");
	return 0;
}


But it won't run on my compiler. I would like to know what i am doing wrong.

Any help would be appreciated.

Thank you.

This post has been edited by Dryerdoor: 21 March 2007 - 11:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Hi, I'm new to C++

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Hi, I'm new to C++

Posted 21 March 2007 - 11:03 PM

well first of you need to look at the cout lines. The "<<" and ">>" operators point in the direction of data flow... so if the date is going TO cout then it looks like: cout << "Hello world!\n"; and if you are inputing a variable "age" then the data is going FROM cin TO age: cin >> age;

#include <iostream.h>
#include <stdlib.h>
int main()
{
	int year = 1984;
	int startingDeerPop;
	int deerPop;
	int temp;  
	
	cout << "Please enter the starting year." << endl;
	cin >> year;
	cout << "Please enter the starting population for the deer." << endl;
	cin >> startingDeerPop;
	cout << "What was the lowest temperature for the" << "year?" << endl;
	cin >> temp;
	if (temp > 0) 
	{
		deerPop = startingDeerPop + (0.15 * startingDeerPop);
	}		
	else if (temp < 0) 
	{
		deerPop = startingDeerPop - (0.12 * startingDeerPop);
	}
	cout << "In 1984, the deer population is:" << deerPop << "." << endl;

	system("PAUSE");
	return 0;
}
This will compile but I don't know if it works.
Was This Post Helpful? 0
  • +
  • -

#3 Dryerdoor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 07

Re: Hi, I'm new to C++

Posted 21 March 2007 - 11:11 PM

View PostNickDMax, on 21 Mar, 2007 - 11:03 PM, said:

well first of you need to look at the cout lines. The "<<" and ">>" operators point in the direction of data flow... so if the date is going TO cout then it looks like: cout << "Hello world!\n"; and if you are inputing a variable "age" then the data is going FROM cin TO age: cin >> age;

#include <iostream.h>
#include <stdlib.h>
int main()
{
	int year = 1984;
	int startingDeerPop;
	int deerPop;
	int temp;  
	
	cout << "Please enter the starting year." << endl;
	cin >> year;
	cout << "Please enter the starting population for the deer." << endl;
	cin >> startingDeerPop;
	cout << "What was the lowest temperature for the" << "year?" << endl;
	cin >> temp;
	if (temp > 0) 
	{
		deerPop = startingDeerPop + (0.15 * startingDeerPop);
	}		
	else if (temp < 0) 
	{
		deerPop = startingDeerPop - (0.12 * startingDeerPop);
	}
	cout << "In 1984, the deer population is:" << deerPop << "." << endl;

	system("PAUSE");
	return 0;
}
This will compile but I don't know if it works.


Thank you, since I solved that, I'm still getting problems from my compiler saying"no match for 'operator>>' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(&std::cout)), ((const char*)"Please enter the starting population for the deer.")) >> std::endl'

and:

"[Warning] converting to `int' from `double' "

how do I remedy those problems?

Also, with the given problem, what command and string would I use to continue the process 15 times, would it be "while"?
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Hi, I'm new to C++

Posted 21 March 2007 - 11:26 PM

the operator errors you just need to check the direction. For example you can't ">>endl" as endl is not a variable.

the warning can be corrected by converting deerPop and tartingDeerPop to doubles. The reason this warning arrises is that the line
deerPop =startingDeerPop - (0.12 * startingDeerPop); uses a double (0.12) and the rest of the values are integers (non-decimal numbers).
Was This Post Helpful? 0
  • +
  • -

#5 Dryerdoor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 07

Re: Hi, I'm new to C++

Posted 21 March 2007 - 11:35 PM

Thank you very much, one more question, how do i represent number like 12% or 15%?

And what is the command to make the years go go up by themselves? And where would I insert it?

This post has been edited by Dryerdoor: 21 March 2007 - 11:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dryerdoor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 07

Re: Hi, I'm new to C++

Posted 21 March 2007 - 11:46 PM

Another question, but this deals with the arithmetic:

 if (temp > 0) {
			 deerPop = 15/100 * deerPop;
}		 
		 if (temp < 0) {
			 deerPop = 12/100 * deerPop;
}


and after that is entered, the solution comes out to zero.

How do I remedy that?
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Hi, I'm new to C++

Posted 22 March 2007 - 12:08 AM

Numbers are represented in the computer in two formats. The first and simplist is the integer. Integers are the positive and negative whole numbers {...-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...}. Then there are the floating point numbers, these are the numbers which contain decimal places {54.1345x10^37, .00124, -135.653, 3.14159265 etc. etc.} these can be thought of as corresponding to the "real numbers" (though they are NOT, they are all rational numbers, you need a symbolic system to deal with real numbers).

ok so 15/100 = 0.15 which as an integer gets rounded to 0... so 15/100 * deerPop == 0 * deerPop = 0. We can get a better answer if we write this as (15 * deerPop)/100... this will still return an integer, but it *should* return something greater than zero if deerPop > 6.

You can use a loop such as for(initalize; condition; step)-loop or a do-while, or a while loop. to make the years increment.
Was This Post Helpful? 0
  • +
  • -

#8 Dryerdoor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 07

Re: Hi, I'm new to C++

Posted 22 March 2007 - 12:14 AM

Nick, you help is much appreciated, but I have on more question.

I have the everything set up, except for one little thing, which is this:

#include <iostream.h>
#include <stdlib.h>
int main()
{
	int year;
	int deerPop;
	int temp;  
	
	cout << "Please enter the starting year." << endl;
	cin >> year;
	cout << "Please enter the starting population for the deer." << endl;
	cin >> deerPop;
	cout << "What was the lowest winter temperature for the" << " year?" << endl;
	cin >> temp; 
		 if (temp > 0) {
			 deerPop = deerPop - (15 * deerPop)/100;
}		 
		 if (temp < 0) {
			 deerPop = deerPop - (12 * deerPop)/100;
}
	cout << "In 1984, the deer population is:" << deerPop << "." << endl;
	cout << "What was the lowest winter temperature for 1985?" << endl;


now how would I carry in the figure from the past year into the next year population arithmetic?
Was This Post Helpful? 0
  • +
  • -

#9 Dryerdoor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-March 07

Re: Hi, I'm new to C++

Posted 22 March 2007 - 12:23 AM

Never mind, I have got the program working fine.

I appreciate all the help you have given me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1