0

I'm having trouble with why my code isn't being called properly. I feel like I have it correct 90% of the way. My code is meant to repeat the word "n" amount of times.

string repeat(string str, int n);
int main ()
{
    string repeat_main;
    repeat_main = repeat("str", 5);
}

string repeat(string str, int n)
{
    string repeated_str;
    int i = 0;
    cout << "enter the word you would like to repeat: ";
    cin >> str;
    cout << "how many times would you like it to repeat? ";
    cin >> n;

    while (i < n)
    {
        repeated_str += str;
        i++;
    }
    return repeated_str;
}

I try calling the repeat function into my main function it does not display what I am looking for.

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
Thegillian
  • 19
  • 5
  • 4
    If you want to display the resulting string, you must output it: `cout << repeat_main;` – paddy Mar 29 '23 at 05:10
  • 6
    Side-note: There's no point passing the string and count to the function if it's just going to read them from `cin` anyway. It's better to separate your input/output from the code that does the work. So the `main` function should be prompting the user and reading the values, which it should then give to your `repeat` function. That function should _only_ be responsible for repeating the string and returning the result. Then your `main` function should output the result. – paddy Mar 29 '23 at 05:13
  • 1
    *"I'm having trouble"* -- what sort of trouble? Does calling your function cause your computer to overheat? Please be specific so that the next person with the same question has a chance to find -- and benefit from -- your question. – JaMiT Mar 29 '23 at 06:10
  • You're supposed to do the user interaction in `main`. `repeat` should not input or output anything. – molbdnilo Mar 29 '23 at 06:26
  • 1
    you are calling the function correctly. Can you point to the line of code that should produce output? – 463035818_is_not_an_ai Mar 29 '23 at 06:58
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Mar 29 '23 at 08:57

2 Answers2

3

As far as I can tell, there are two issues with your code:

  • You need to forward declare the function if you plan to define it after main.
  • You need to cout << repeat_main so that the string is printed.
#include <string>
#include <iostream>

using namespace std;

string repeat(string str, int n);

int main ()
{
    cout << repeat("str", 5);
}

string repeat(string str, int n)
{
    string repeated_str;
    int i = 0;
    cout << "enter the word you would like to repeat: ";
    cin >> str;
    cout << "how many times would you like it to repeat? ";
    cin >> n;

    while (i < n)
    {
        repeated_str += str;
        i++;
    }

    return repeated_str;
}

As noted in comments, if you're just going to have your function prompt for input, then you can remove the useless parameters.

string repeat()
{
    string repeated_str;
    string str;
    int n;
    int i = 0;
    cout << "enter the word you would like to repeat: ";
    cin >> str;
    cout << "how many times would you like it to repeat? ";
    cin >> n;

    while (i < n)
    {
        repeated_str += str;
        i++;
    }

    return repeated_str;
}

Or keep those, and use them, handling the IO in your main.

string repeat(string str, int n)
{
    string repeated_str;
    int i = 0;

    while (i < n)
    {
        repeated_str += str;
        i++;
    }

    return repeated_str;
}
Chris
  • 26,361
  • 5
  • 21
  • 42
2

You was almost there. You didn't displayed the repeated string. I solved your problem while also improving readability and maintainability:

  • Always state your includes in code you post, it's never obvious.
  • As stated by @paddy, there is no point to make a function that ask for a user input. You are mixing input acquisition and input processing. In my code input acquisition is done right in the main().
  • A for() loop in more readable than a while() in your case. Maybe you are in a school that enforce utterly stupid code styles forbidding usage of for()? Know that you must not code like this outside of school.
  • Do not use using namespace, these exists to separate function names from various sources. It's barely okay there, you will run in troubles when you start adding multiple dependencies (both namespace defines their own string), don't get this bad habit of using namespace.
  • Use const std::string& in arguments unless you know don't need it. Without the &, you pass the param by value so all function invocation will make a copy of the string, with & you pass by reference so any action on the object in the function will occur on the object in the calling function. Beside ints, floats, booleans, pointers and enums, you want to pass by reference 99% of the time.
  • About the const above, it allow you to use const std::string objects into the function and (most important) prevent accidental string modification, compilation would fail.
  • You str param in your original answer did not made any sense. It's content was never used since it was passed by copy and fully erased by cin >> std, it should have been a variable. The code below make use of the str param.
#include <iostream>
#include <string>

std::string repeat(const std::string& str, int n);

int main ()
{
    // Acquiring inputs
    std::string str;
    int n;
    std::cout << "enter the word you would like to repeat: ";
    std::cin >> str;
    std::cout << "how many times would you like it to repeat? ";
    std::cin >> n;
    // Printing repeated string
    // I am not using an intermediate variable there
    std::cout << repeat(str, 5) << std::endl;
}

std::string repeat(const std::string& str, int n)
{
    std::string repeated_str;
    for (int i = 0; i < n; ++i) {
        repeated_str += str;
    }
    return repeated_str;
}
Devilish Spirits
  • 439
  • 4
  • 18