function that accepts an integer and returns a string?

How to go about making a function that accepts an integer and returns a string with any one of 5 strings containing the name of the object. For example object number 3 might be "Pen". Object 4 might be "Paper".

To do this would I just use an array?
Yes a string array could do what you want, or a switch statement. Also you could do this with a class builder/ constructor. Honestly though the string array would be the easiest
How can I get this function to accept an integer value that is generated by a random number generator?
What I'm trying to figure out is, the user will enter a number and that number will be the corresponding array size consisting of different strings. A random number generator will generate integers that correspond to the array subscripts, which each subscript has it's own string word. Does this make sense?
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
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>

int main()
{
      // http://www.mochima.com/tutorials/vectors.html
      std::vector<std::string> things ; // a resizeable array of names of things

      std::size_t n ;
      std::cout << "number of things? " ;
      std::cin >> n ;

      std::cout << "please enter " << n << " nouns\n" ;

      std::string str ;
      while( things.size() < n )
      {
          std::cin >> str ;

          // http://www.cplusplus.com/reference/vector/vector/push_back/
          things.push_back(str) ;
      }

      // the the sequence of things now contains things in the order
      // they were entered by the user. now shuffle the sequence randomly
      // so that each thing ends up in a random position

      // http://www.cplusplus.com/reference/cstdlib/srand/
      std::srand( std::time(0) ) ; // seed the rng

      // http://www.cplusplus.com/reference/algorithm/random_shuffle/
      std::random_shuffle( things.begin(), things.end() ) ;

      // print out the random positions of things
      for( std::size_t i = 0 ; i < things.size() ; ++i )
          std::cout << i << ". " << things[i] << '\n' ;
}
Topic archived. No new replies allowed.