Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,413 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,216 people online right now. Registration is fast and FREE... Join Now!




Hi, I'm new to C++

 
Reply to this topicStart new topic

Hi, I'm new to C++

Dryerdoor
21 Mar, 2007 - 09:03 PM
Post #1

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 6


My Contributions
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.

CODE
#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 Mar, 2007 - 10:20 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Hi, I'm New To C++
21 Mar, 2007 - 10:03 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,864



Thanked: 53 times
Dream Kudos: 550
My Contributions
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;

CODE
#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.
User is offlineProfile CardPM
+Quote Post

Dryerdoor
RE: Hi, I'm New To C++
21 Mar, 2007 - 10:11 PM
Post #3

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 6


My Contributions
QUOTE(NickDMax @ 21 Mar, 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;

CODE
#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"?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Hi, I'm New To C++
21 Mar, 2007 - 10:26 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,864



Thanked: 53 times
Dream Kudos: 550
My Contributions
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).


User is offlineProfile CardPM
+Quote Post

Dryerdoor
RE: Hi, I'm New To C++
21 Mar, 2007 - 10:35 PM
Post #5

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 6


My Contributions
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 Mar, 2007 - 10:37 PM
User is offlineProfile CardPM
+Quote Post

Dryerdoor
RE: Hi, I'm New To C++
21 Mar, 2007 - 10:46 PM
Post #6

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 6


My Contributions
Another question, but this deals with the arithmetic:

CODE
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?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Hi, I'm New To C++
21 Mar, 2007 - 11:08 PM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,864



Thanked: 53 times
Dream Kudos: 550
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Dryerdoor
RE: Hi, I'm New To C++
21 Mar, 2007 - 11:14 PM
Post #8

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 6


My Contributions
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:

CODE
#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?
User is offlineProfile CardPM
+Quote Post

Dryerdoor
RE: Hi, I'm New To C++
21 Mar, 2007 - 11:23 PM
Post #9

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 6


My Contributions
Never mind, I have got the program working fine.

I appreciate all the help you have given me.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:51AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month