simple code problem

// This program is designed to print (to the screen) your first name on one line followed by two blank lines.
// Then print your last name on the next line, preceded by as many hyphens as characters in your first name.

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
string FIRSTNAME;
string LASTNAME;

cout << "Enter your first name, add a space, then enter your last name" << endl;
cout << "When you're finished, hit (enter) to execute the program" << endl;
cin >> FIRSTNAME >> LASTNAME;
cout << "Name: " << FIRSTNAME << endl;
cout << endl;
cout << endl;
cout << LASTNAME << endl;
system("PAUSE");

return EXIT_SUCCESS;
}



As you can see this program is designed to print the output:

FIRSTNAME


---------LASTNAME

But I can't figure out how to get the C++ program to read how many characters are in the first name and output a hyphen for each character you enter for FIRSTNAME before LASTNAME. Can anyone help? I've done a lot of research on if/else and that doesn't seem to be what I need. I think I need to use a variable, but I'm not sure.
Last edited on
I can think of two solutions for this - one involves stream manipulators (which you probably don't know about yet, though you're actually using one), the other one...

you know "for" loops right? Strings have a "length()" method that returns the number of characters in the screen. Put 1 and 1 together.
I just started c++ this week, I came from webbased languages and really don't know a thing about C++, so this is a pretty big learning curve, can you explain a "for" loop and give me an example so I can get a better idea of putting that together with the string length method?
Thank you, I did some research online for "for" loops and found some good information, I think I'm starting to understand it. I'll post up the final product of my program to get some feedback on it. Thanks!
Ok, I've tried a number of different things and I can't seem to get it working, can show me how the two would work together?
Last edited on
Show me what you have done so far.
// This program is designed to print (to the screen) your first name on one line followed by two blank lines.
// Then print your last name on the next line, preceded by as many hyphens as characters in your first name.

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
string FIRSTNAME;
string LASTNAME;

cout << "Enter your first name, add a space, then enter your last name" << endl;
cout << "When you're finished, hit (enter) to execute the program" << endl;
cin >> FIRSTNAME >> LASTNAME;
cout << "Name: " << FIRSTNAME << endl;
cout << endl;
cout << endl;

for ()
{
cout << - << LASTNAME << endl;
}
system("PAUSE");

return EXIT_SUCCESS;
}
I think I need to find out how to get the string length and store it without the computer showing it on a line, then use for() to say if string length is x, print - x amt of times. If I can see hows its done, I'll understand it a lot better.
I think I need to find out how to get the string length...
1
2
string name("Archibald");
int len = name.length();


len now has the value 9.

...then use for()...
1
2
3
4
for ( int i = 0; i < 10; ++i )
{
    std::cout << i << "\n";
} // for 


Prints out the numbers 0 to 9.
You should be able to solve your problem using that ;)
here goes your code

#include <iostream>
#include <string>
using namespace std;

int main ()
{
string fname;
string lname;

cout << "Enter your first name, add a space, then enter your last name" << endl;
cout << "When you're finished, hit (enter) to execute the program" << endl;
cin >> fname >> lname;
cout << "Name: " << fname << endl;
cout << endl;
cout << endl;
int len = fname.length();
for (int m = 0; m <len; m++)
cout << "-";
cout << lname << endl;
return 0;
}
Thanks a ton everyone, this makes MUCH more sense now.
Topic archived. No new replies allowed.