Thread: assigning string problem

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

    assigning string problem

    I am trying to write a simple hello program with a box of astericks around the output but I can't get the astericks to show up.

    This is what I have so far.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
    
    cout << "Please enter your name." << endl;
       string name;
         cin >> name;
    
       string k = name;
    
       k.assign(0, '*');
    
    string aster = "*";
    
    aster.assign(16, '*');
    
    cout << aster + k << endl;
    
    string reply = "*   Hello, " + name + "!" + "   *";
    
    cout << reply << endl;
    
    cout << aster + k << endl;
    
    return 0;
    }

    Code tags added by kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    93
    is this helpful? I believe what u are trying to output is

    **********
    *Hello,Bob! *
    ***********

    if this is correct i believe the below code will work.. haven't tested it but should do fine..

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
    
    string name;   // Declare your variables first..;
    int length;      // Storing length of name string;
    
    cout << "Please enter your name." << endl;
    cin >> name;
    length = name.length() + 15;     // 15 is from "*----Hello!---*
    
    for(x = 0;x < length; x++)
      {
        cout << "*";
      }
    cout << endl <<  "*   Hello, " << name << "!   *";
    
    for(x = 0;x < length; x++)
      {
        cout << "*";
      }
    
    return 0;
    }
    Last edited by tegwin; 11-06-2002 at 06:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-23-2007, 12:28 PM
  2. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 11:52 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM