I am having trouble getting the last two statements to execute that should print the full name in the middle of the screen. any help is greatly appreciated. Thanks!!
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
string name;
int length;
string lname = " ";
string fname = " ";
int space;
int i;
char ans = 'y';
while (ans == 'Y' || ans == 'y')
{
cout << "What is your first and last name?" << endl;
getline(cin, name);
length = name.length();
for (i = 0; i < length; i++)
{
cout << name[i] << endl;
}
cout << left << setw(40 + (.5 * length)) << setfill(' ');
space = name.find(' ');
//cout << "Position = " << space << endl;
for (int i = 0; i < length; i++)
{
if (i < space)
{
fname += name[i];
}
elseif (i > space)
{
lname += name[i];
}
}
name = lname + ", " + fname;
length = name.length();
cout << setw(40 + (.5 * length)) << left << setfill(' ');
cout << "Would You Like To Continue?" << endl; // Move this here...
cin >> ans; // Move this here...
cin.ignore(); // Add this because you're
// using getline as well.
// (fixes the whole
// "getline skipping issue
// and many have when using
// getline with cin")
}
return 0;
}
Thanks a lot!! Helped greatly with the execution needed, but it is still not printing in the name in the middle along with it down the side. It is supposed to print the name down the left side the first time and then in the middle two times. Instructions are as follows:
Determine the length of the name and store the results in a variable.
Using a loop, print each of the characters of the name (array) on a separate line (from beginning of name list to end of name list).
The program should then, using a screen width of 80, print the name centers the name on the screen.
Next the system should add the function to find the blank space within the name and store the results in a variable.
Using the location of the space, use this and the appropriate string functions to store the first name in fname and the last name in lname (both string variables).
Next – concatenated (add) the pieces together with the appropriate comma to store the name as lname, fname into the original name identifier.
The program should then, using a screen width of 80, center the name on the screen, using the format: lname, fname (don’t forget the comma)
Place the entire program into a loop allowing the user to repeat the section as desired.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
string name;
int length;
string lname = ""; // I removed your space here.
string fname = ""; // I removed your space here.
// Strings will count spaces as a character
int space;
int i;
char ans = 'y';
while (ans == 'Y' || ans == 'y')
{
cout << "What is your first and last name?" << endl;
getline(cin, name);
length = name.length();
// Printing each character of the string on it's own line
for (i = 0; i < length; i++)
{
cout << name[i] << endl;
}
// cout << left << setw(40 + (.5 * length)) << setfill('');
// Here, we are printing the name the middle of a 80 width terminal screeen
cout << setw(40+name.size()/2) << name << endl;
// Here, you've already found the position of the space
space = name.find(' ');
//cout << "Position = " << space << endl;
/* There is a much better way to do this. I explain below...
for (int i = 0; i < length; i++)
{
if (i < space)
{
fname += name[i];
}
else if (i > space)
{
lname += name[i];
}
}
*/
// Below, we use the space variable to determine where the first and last
// name are within the string "name". By doing this, we will set fname and
// lname to their appropriate values.
// "I want fname to be equal to a specific part of the string name. For
// this reason, I will use the built-in substr function. Since the first
// name starts at the beginning, I will feed in parameters 0 and space,
// with 0 denoting the beginning of the name and space is where we should
// cut it off."
fname = name.substr(0, space);
// "I want lname to be the last name. For this reason, I will use the
// built-in substr function. The last name portions are the position after
// the space and the very ending of the name of the string."
lname = name.substr(space+1, name.size()-1);
// cout << fname << " " << lname << endl; // Because your steps don't say you
// need this, I would leave it out.
// Here, you concatenated the strings with comma
name = lname + ", " + fname; // Here, you concatenated the strings w/ comma
// You don't really need these parts now...
// length = name.length();
// cout << setw(40 + (.5 * length)) << left << setfill(' ');
// Print the new name in the middle of a 80 width terminal screen.
cout << setw(40+name.size()/2) << name << endl;
cout << "Would You Like To Continue?" << endl;
cin >> ans;
cin.ignore();
}
return 0;
}
The terminal emulator I use sets wrap at 80 characters, so it's centered nicely on mine. You can play around with iomanip library though...