// 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.
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.
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!
// 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.
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;
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.
#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;
}