Function trouble

I am trying to make a user choice based game that uses functions. this is biting off more than i can chew so to speak. I'd like to be able to show this to my teacher and impress him. Anyway, i'm i want to use the plains, forest, river, and mountain variables i declared in Geography(). I want to bring them into main() and use them. how do i do this?

#include <iostream>
#include <ctime>
#include <string>

using namespace std;

class names
{
public:
string Name;

};





void geography(int plainsRan, int riverRan, int forestRan, int mountainRan )//starting function to set landscape
{
srand(time(0));

plainsRan = rand();
plainsRan %= 3;
riverRan = rand();
riverRan %= 3;
forestRan = rand();
forestRan %= 3;
mountainRan = rand();
mountainRan %= 3;

cout<<"Your party approaches a plot of land surrounded by "<<endl;
cout<<"Plain(s) \t"<<plainsRan<<endl
<<"River(s)\t"<<riverRan<<endl
<<"Forest(s)\t"<<forestRan<<endl
<<"Mountain(s)\t"<<mountainRan<<endl;
cout<<"Would you like to start a settlement here?"<<endl;

}

int main()
{
names settlement;
int user_choice = 2;
while(user_choice != 1)
{
geography(1,2,3,4);
cout<<"[1] yes\n"
<<"[2] no\n";
cin>>user_choice;
system("clear"); //system clear needs to change to cls for windows
}
cout<<"what will you name your settlement?"<<endl;
cin.ignore();//flush the input to make getline work
getline(cin, settlement.Name);
system("clear"); //change to cls for windows

}
This is the first part of the game to establish the landscape. It will be like a civilization game that utilizes your geography's resources to make and build a thriving community.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstdlib>
#include <ctime>

struct geography
{
    enum { MAX_PLAINS = 5, MAX_RIVERS = 4, MAX_FORESTS = 3, MAX_MOUNTAINS = 2 };

    int plains ;
    int rivers ;
    int forests ;
    int mountains ;
};

geography make_geography() // return a random geography object
{
    return // see: http://www.stroustrup.com/C++11FAQ.html#uniform-init
    {
        std::rand() % (geography::MAX_PLAINS+1),
        std::rand() % (geography::MAX_RIVERS+1),
        std::rand() % (geography::MAX_FORESTS+1),
        std::rand() % (geography::MAX_MOUNTAINS+1)
    };
}

int main()
{
    // seed the random number generator once, at the start of the program
    // consider using facilities provided by the C++ random number library
    // http://en.cppreference.com/w/cpp/numeric/random
    std::srand( std::time(nullptr) ) ;

    const geography game_geography = make_geography() ;

    std::cout << "Your party approaches a plot of land surrounded by\n"
              << game_geography.plains << " plains, "
              << game_geography.rivers << " rivers, "
              << game_geography.forests << " forests "
              << "and " << game_geography.mountains << " mountains\n"
              << "\nWould you like to start a settlement here (y/n)?\n" ;

    // ...
}

http://coliru.stacked-crooked.com/a/567081db8571a1d3
thank you, this code is way over my head so i'll have to study and understand it
> this code is way over my head so i'll have to study and understand it

This is functionally equivalent code; it does the same thing. But it is somewhat less C++-like; somewhat more like the kind of code that a career teacher would write. Perhaps having a look at this code would help in understanding the code as originally posted.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std ;

// constants (adjust as required)
const int MAX_PLAINS = 5 ;
const int MAX_RIVERS = 4 ;
const int MAX_FORESTS = 3 ;
const int MAX_MOUNTAINS = 2 ;

struct geography // a structure to hold the game geography information
{
    int num_plains ;
    int num_rivers ;
    int num_forests ;
    int num_mountains ;
};

geography make_geography() // return a geography object
{
    geography game_geography ;

    // fill up with randomly generated values
    game_geography.num_plains = rand() % (MAX_PLAINS+1) ; // [0,MAX_PLAINS]
    game_geography.num_rivers = rand() % (MAX_RIVERS+1) ; // etc.
    game_geography.num_forests = rand() % (MAX_FORESTS+1) ;
    game_geography.num_mountains = rand() % (MAX_MOUNTAINS+1) ;

    return game_geography ;
}

int main()
{
    // seed the random number generator once, at the start of the program
    // consider using facilities provided by the C++ random number library
    // http://en.cppreference.com/w/cpp/numeric/random
    srand( time(0) ) ;

    const geography game_geography = make_geography() ;

    const int num_plains = game_geography.num_plains ;
    const int num_rivers = game_geography.num_rivers ;
    const int num_forests = game_geography.num_forests ;
    const int num_mountains = game_geography.num_mountains ;

    cout << "Your party approaches a plot of land surrounded by" << endl
         << num_plains << " plains, "
         << num_rivers << " rivers, "
         << num_forests << " forests "
         << "and " << num_mountains << " mountains" << endl
         << "\nWould you like to start a settlement here (y/n)?" << endl ;

    // ...
}

http://coliru.stacked-crooked.com/a/435bb14c81afc68e
closed account (48T7M4Gy)
Bit late for the party but nevertheless.

(It remains to be seen why geography() needs to have parameters passed to it ... )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

class names
{
public:
    string Name;
};

char geography(int plainsRan, int riverRan, int forestRan, int mountainRan )//starting function to set landscape
{
    char answer = ' ';
    
    srand(time(0));
    
    plainsRan = rand() % 3;
    riverRan = rand() % 3;
    forestRan = rand() % 3;
    mountainRan = rand() % 3;
    
    cout
    << "Your party approaches a plot of land surrounded by\n"
    << "   Plain(s): " << plainsRan << '\n'
    << "   River(s): " << riverRan << '\n'
    << "  Forest(s): " << forestRan << '\n'
    << "Mountain(s): "<<mountainRan << '\n'
    << "Would you like to start a settlement here? [1] yes, [2] no: ";
    
    while( cin >> answer and answer != '1' and answer != '2')
    {
        cout << "Answer must be 1 or 2: ";
        cin.ignore(1000, '\n');
    }
    return answer;
}

int main()
{
    names settlement;
    
    if ( geography(1, 2, 3, 4) == '1' )
    {
        cout << "What will you name your settlement?: ";
        cin.ignore(1000, '\n');//flush the input to make getline work
        getline(cin, settlement.Name);
        
        cout << "*** The settlement name is: " << settlement.Name << '\n';
    }
    else
        cout << "That's it folks\n";

    return 0;
}
Last edited on
Topic archived. No new replies allowed.