-
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
-
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
-
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;
}