Function to write string 3 times

This is a discussion on Function to write string 3 times within the C++ Programming forums, part of the General Programming Boards category; Hi. I'm trying to write a function that will write a string 3 times to the standard output. This is ...

  1. #1
    Giggs
    Guest

    Smile Function to write string 3 times

    Hi. I'm trying to write a function that will write a string 3 times to the standard output.

    This is how far I've got:

    // a function to write a string 3 times to standard output

    #include <iostream>
    #include <string>

    using std::cout; using std::endl;
    using std::cin; using std::string;

    string name3times(string);

    int main()
    {
    cout << "Type your first name: ";
    string name;
    cin >> name;
    cout << "Hello, " << name3times(name) << endl;

    return 0;
    }

    string name3times(string name)
    {
    return
    cout << name + "!" << endl;
    cout << name + "!" << endl;
    cout << name + "!" << endl;
    }

    I keep getting the following error message from the compiler:

    ex5-2b.cc: In function `class string name3times(basic_string<char,string_char_traits<ch ar>,__default_alloc_template<false,0> >)':
    ex5-2b.cc:24: conversion from `ostream' to non-scalar type `string' requested

    Thanks for any help in advance!

    Giggs

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    name3times should return a string, not print it. The caller of name3times should actually print the output of name3times (IE, main). name3times should only return a string containing the name 3 times (apparently seperated by '!'. This name3times should work.

    Code:
    string name3times(string name)
    {
    return name + "!" + name + "!" + name + "!";
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    11
    Thanks for the quick reply SilentStrike.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Beyond style, the actual problem here is, as I understand, you don't seperate output with a + but by more <<.

    Also, you have a return at the very beginning of your function. You wish to return before anything is done?

    Here is how your function should have looked.

    Code:
    void name3times(string name)
     {
     for(int c = 0; c < 3; c++
     cout << name << "!" << endl;
     }
    Note, that does not return anything, and uses a loop and the << insetad of +.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    11

    Question Another question

    Hi. Here is another question. I'm trying to write a function that will write a string a given number of times (specified by user). How would I do this?

    Code:
    /* a function to write a string a given number of times, as specified in a
       second parameter */
    
    #include <iostream>
    #include <string>
    
    using std::cout;  using std::endl;
    using std::cin;   using std::string;
    
    string output(string, int);
    
    int main()
    {
      int n = 0;
    
      cout << "Write something: ";
      string input;
      cin >> input;
      cout << "Number of times to display it: ";
      cin >> n;
      cout << output(input, n);
    
      return 0;
    }
    
    string output(string input, int n)
    {
      return input + '\n' * n;
    }
    Obviously 'return input + '\n' * n;' doesn't work.

    giggsy

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    int main(void)
    {
       ...
       for(int i = 0; i < n; i++)
          cout << output(input);
       ...
    }
    
    string output(string input)
    {
      return input + '\n';
    }
    Or
    Code:
    int main(void)
    {
       ...
       output(input, n);
       ...
    }
    
    void output(string input, int n)
    {
       for(int i = 0; i < n; i++)
          cout << input + '\n' * n;
    }

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    11
    Thanks Monster. The first suggestion worked fine. But the second one didn't work. This is how I implemented it:

    Code:
    /* a function to write a string a given number of times, as specified in a
       second parameter */
    
    #include <iostream>
    #include <string>
    
    using std::cout;  using std::endl;
    using std::cin;   using std::string;
    
    void output(string, int);
    
    int main()
    {
      int n = 0;
    
      cout << "Write something: ";
      string input;
      cin >> input;
      cout << "Number of times to display it: ";
      cin >> n;
      cout << output(input, n);
    
      return 0;
    }
    
    void output(string input, int n)
    {
      for(int i = 0; i < n; i++)
        cout << input + '\n' * n;
    }
    What did I do wrong?

    giggsy

  8. #8
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    you should call it as
    Code:
    output(input, n);
    not
    Code:
    cout<<output(input, n);
    the return type of the function is void, so it doesn't return anything for cout to print. the printing is done inside the function.
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    11

    Question

    Ok. I tried the following:

    Code:
    /* a function to write a string a given number of times, as specified in a
       second parameter */
    
    #include <iostream>
    #include <string>
    
    using std::cout;  using std::endl;
    using std::cin;   using std::string;
    
    void output(string, int);
    
    int main()
    {
      int n = 0;
    
      cout << "Write something: ";
      string input;
      cin >> input;
      cout << "Number of times to display it: ";
      cin >> n;
      output(input, n);
    
      return 0;
    }
    
    void output(string input, int n)
    {
      for(int i = 0; i < n; i++)
        cout << input + '\n' * n;
    }
    But I get the following error from the compiler:

    Code:
    ex5-2c.cc: In function `void output(basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >, int)':
    ex5-2c.cc:29: no match for `string & + int'

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Code:
    void output(string input, int n)
    {
      for(int i = 0; i < n; i++)
        cout << input + '\n' * n;
    }
    read your error message,

    Code:
    cout << input + '\n' * n;
    Is not correct syntax for cout.

    Try

    Code:
    cout << input << endl;

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    11

    Cool

    Thanks orbitz it works fine. Can someone explain to me what the following line does and how it works:

    Code:
    for(int i = 0; i < n; i++)
    I haven't studied it yet on my course.

    Cheers.

  12. #12
    Registered User
    Join Date
    Dec 2002
    Posts
    2
    I'm a novice and this is my first post.

    for(int i = 0; i < n; i++) // starts a for loop that initializes i as 0, executes as long as i is less than n, and adds 1 to i everytime it loops.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Originally posted by EnderFK
    I'm a novice and this is my first post.

    for(int i = 0; i < n; i++) // starts a for loop that initializes i as 0, executes as long as i is less than n, and adds 1 to i everytime it loops.
    Thanks for the post

  14. #14
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Monster
    Code:
    void output(string input, int n)
    {
       for(int i = 0; i < n; i++)
          cout << input + '\n' * n;
    }
    Ehhh, sorry about that giggsy. I posted some code that I didn't test. But I see other people helped you well.

    B.t.w. in stead of using std::cout; using std::endl; etc you can add this line to your code: using namespace std;
    By doing this you tell the compiler you want to access all the objects (and functions) of the namespace std directly.

    Cheers,
    Monster

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    11

    Smile

    Thanks for everyone's help!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  3. Replies: 6
    Last Post: 04-21-2006, 09:49 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21