0
string arr_function(string arr[], int max) {
    for (int i = 0; i < max; i++) {
        cout << "\n\t"
             << "[ " << arr[i] << " ]";
    }
    return 0;
}

int main() {
    string arr[4] = {"Baseball", "Basketball", "Football", "Soccer"};
    int one, two;

    srand(time(NULL));
    one = rand() % 4 + 1;
    two = rand() % 4 + 1;
    cout << arr_function(arr, one) << " " << arr_function(arr, two);

    cout << endl;
    return 0;
}

I have this function that prints an array. The program takes a random index from the array and prints it. I have several problems with it. It prints more than "1" and I only want "1" random sport.

The two calls to the function are because I want to randomize two different indexes and print possibly two random names (they could be the same though).

My other problem is that both function prints the name in a vertical line. I want to be able to print the two function in a horizontal line. I tried doing the following:

Change the void to string. I tried return arr[i], arr[max], etc. but that's wrong. I don't know how to return this type of function.

What I want to do with the output is

cout << arr_function(arr, one) << " " << arr_function(arr, two) << endl;

The final output I'm trying to get should look like,

[Baseball] [Basketball]
pjs
  • 18,696
  • 4
  • 27
  • 56
Joe
  • 83
  • 9
  • 3
    If you only want to print two values, why is `arr_function` looping? – Stephen Newell Jan 07 '21 at 21:21
  • "It prints more than "1" and I only want "1" random sport." Read what the arr_function does. Are you familiar with for loops and indexing ? – Vélimir Jan 07 '21 at 21:22
  • 1
    If you don't want a newline, why are explicitly printing them? Your calls to rand can can also result in getting an out-of-bounds index value. And you're getting the same sport both times because `rand()` is not very good. – sweenish Jan 07 '21 at 21:25
  • Start the program in a debugger and step through it. That will help you understand what is going on. – Werner Henze Jan 07 '21 at 21:25
  • I thought we have to use a loop to output an array? @Sho, I not new but there are some things I still have to learn – Joe Jan 07 '21 at 21:29
  • 1
    And do you want to output the entire array, or just two elements? There's a pretty major difference. – sweenish Jan 07 '21 at 21:30
  • @sweenish, that makes sense. I want to print whatever the rand() index choses. Which means, since I'm trying to call the function twice then output only two elements. – Joe Jan 07 '21 at 21:31
  • What is the output when you run your code? – Code-Apprentice Jan 07 '21 at 22:15
  • Multiple array elements instead of just one like (sometimes it returns three, two, etc.) It also prints it in vertical line. – Joe Jan 07 '21 at 22:19

2 Answers2

0

There's a lot of "interesting" stuff going on. Your function is all over the place. It's written to return a string, but you only ever return 0. If you do want it to return a string, then the function probably shouldn't be doing the printing. The name is as generic and non-helpful as it gets. I can chalk that up to creating the small example, so I'm less concerned with that; it just needs mentioning.

For the ease of printing, I will keep the return type, which means I will remove the printing from the function and move it to main().

Other changes I've made include using C++ array objects and C++ random number generation. Unless a situation really demands it, prefer C++ classes and libraries for C++.

I've also eliminated the need for the extra int variables and multiple std::cout statements.

You're likely using using namespace std;. That's a bad practice.

#include <array>
#include <iostream>
#include <random>  // Superior to rand()
#include <string>

constexpr int CAPACITY = 4;

std::string get_random_sport(std::array<std::string, CAPACITY> arr) {
  static std::mt19937 prng;
  static std::uniform_int_distribution<int> dist(0, arr.size() - 1);

  return arr[dist(prng)];
}

int main() {
  std::array<std::string, CAPACITY> sports{"Baseball", "Basketball", "Football",
                                           "Soccer"};

  std::cout << "[" << get_random_sport(sports) << "] ["
            << get_random_sport(sports) << "]\n";

  return 0;
}
sweenish
  • 4,793
  • 3
  • 12
  • 23
  • Sorry, but it was meant to be a void at first. I was testing it and decided to make it a string and see the output, so I left it like that. Could you explain to be what's the array – Joe Jan 07 '21 at 22:17
  • https://stackoverflow.com/a/1452738/6119582 This answers your question about `using namespace std;`. You can look up `std::array` here: https://en.cppreference.com/w/cpp/container/array . I will leave the function as returning a string since I feel it to be the "cleaner" option. – sweenish Jan 07 '21 at 22:22
0

Remember that a program does exactly what you tell it to, no more and no less. In the case that you want the output to print right next to each other, then you shouldn't print a "\n" since this causes the printing to move to the next line.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268