CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2014
    Posts
    4

    loop report 10 times

    i wrote over my project and had to start again, i cannot get my report to loop 10 times and add 1 to year, year 1, year 2, etc, been working at this all day. please help...

    /Define a class Hammurabi with a member function that takes a parameter;
    //Create a Hammurabi object and call its displayMessage function.
    #include <iostream> //Header library for the input, output stream
    #include <cstdlib> //Header library defines general purpose functions including random number generation
    #include <time.h> //Header library allows the change of numbers per certain length of time

    using namespace std;

    //Hammurabi class definition
    class Hammurabi
    {
    public:
    //function that displays a message to the Hammurabi user
    //Print out the introductory message
    void displayMessage (int year, int starved, int immigrants, int population, int land, int harvest, int rats, int storage, int trade)
    {
    cout << "Hammurabi: I beg to report to you that in Year " << year << endl << endl;
    cout << starved << " people starved;" << endl;
    cout << immigrants << " immigrants came to the city" << endl;
    cout << "The city population is " << population << endl;
    cout << "The city now owns " << land << " acres" << endl;
    cout << "You harvested " << harvest << " bushels per acre;" << endl;
    cout << "Rats ate " << rats << " bushels;" << endl;
    cout << "You now have " << storage << " bushels in storage;" << endl;
    cout << "Land is trading at " << trade << " bushels per acre" << endl;
    cout << endl;
    }//end function displayMessage
    };//end class Hammurabi

    //function main begins program execution
    int main()
    {
    //variables to store the values
    int year = 0;
    int starved = 0; //people who starved, population loss
    const int immigrants = 5; //people who came to the city, population gain
    int population = 100;
    int land = 1000; //amount of land, acres owned by the city
    const int harvest = 3; //amount of bushels harvested per acre planted
    const int rats = 10; //amount of bushels destroyed by rats
    int storage = 2500; //amount of bushels in storage
    int trade = 15; //price land is trading, how many bushels per acre

    while (year <=11 && population > 0) {
    year = year + 1;

    srand((unsigned)time(NULL));
    //trade = 15 + (rand() % 5) + 1;

    Hammurabi myHammurabi; //create a Hammurabi object named my Hammurabi

    //call my Hammurabi displayMessage function
    //and pass values as an argument
    myHammurabi.displayMessage(year, starved, immigrants, population, land, harvest, rats, storage, trade);

    int buy; //amount of acres to buy
    int sell; //amount of acres to sell
    int food; //amount of bushels to feed the population
    int plant; //amount of acres to plant with bushels

    cout << "How many acres of land do you want to buy? " << endl; //amount of bushels to to trade for land
    cin >> buy;
    land += buy; //assignment by sum and difference, (land = land + buy)
    storage -= buy * trade;

    cout << "How many acres of land do you want to sell? " << endl;
    cin >> sell;
    land -= sell;
    storage += sell * trade;
    cout << "How many bushels do you want to feed to the people? (each needs 20) " << endl;
    cin >> food;
    storage -= food;
    cout << "How many acres do you want to plant with seed? (each acre takes one bushel) " << endl;
    cin >> plant;
    storage -= plant;

    cout << endl;

    population += immigrants;
    storage -= rats;
    storage = storage + (harvest * plant);

    system("pause");
    return 0;
    }//end main
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: loop report 10 times

    You have to properly indent your code. Then you will perhaps see were the problem is.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: loop report 10 times

    ...and also use code tags. Go Advanced, select the code and click '#'.

    Hint. Look at the order of the last 4 lines of code.

    Also, you don't need srand() within the while loop. Executing it once at the start of the program is fine.

    Why have the class Hammurabi? At the moment all this class has is one function that displays the contents of the function parameters.
    Last edited by 2kaud; January 15th, 2014 at 05:47 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured