I am having trouble figuring out how to pull part of a string variable out for an assignment.
I must get a first name, last name and zip code, then print these out. This I acheived. Then, I must assign a 'user id' assembled from the first two letters of the first and last name, and the first three letters of the zip code. This is where I am stuck. I solved everything else.
I am in my fourth week of a beginner class - can anyone give me a clue or reference materials? Thank you:
This is the assignment:
Write a program that reads the user's first name and then last name and then the middle initial. The user must be able to enter more than one word for the last name such as De Leon. Then, ask for the user's zip code.
Then, read 3 characters from the user and create a username made up of the
first two letters of the first name, fname2
followed by the first two letters of the last name, lname2
followed by the last 3 digits of the zip code, zip
followed by the character whose ASCII value is the average of the ASCII values of the three characters entered by the user, avg2
followed by the ASCII value of the average character thus computed.
Display the username.
For example, if the user enters Chris DeLeon for his name and 91401 for the zip code and A, D, E for the three characters, the username will be ChDe401C67 where C is the average of ADE that the user entered and 67 is the ASCII value of that average character (C in this case).
Note that the characters that the user will enter could be spaces or tabs or any other character.
Put comments in your source code including your name, class title, assignment # and date.
The following is a sample run of the program:
Enter your first name: Chris
Enter your last name: De Leon
Enter your middle initial: M
Enter zip code: 91401
Enter three characters: ADE
Chris M. De Leon, your username is: ChDe401C67
This is what I acheived thus far:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string fname; // Input student first name//
string fname2; // Input student first name, conversion //
string lname, lname2; // Input student last name, conversion //
char initial; // Input middle initial //
int zip, zip2; // Input zip code, conversion //
char char1, char2, char3; // Input three characters//
int a, b, c, average; // ASCII conversion of characters to number, average of the three//
char avg2; // Average of characters tranlsated to ASCII//
cout << "Please enter your first name: " << endl;
getline(cin, fname);
cout << "Please enter your second name: " << endl;
getline(cin, lname); // create variable to store first two letters of last name//
cout << "Please enter your middle initial: " << endl;
cin.get(initial);
cin.ignore(); // skip the newline character//
cout << "Please enter your five digit zip code: " << endl;
cin >>zip;
cin.ignore(); // skip the newline character//
cout << "Please enter three characters: " << endl;
cin >> char1;
cin >> char2;
cin >> char3;
cin.ignore(); // skip the newline character//
a = static_cast<int>(char1); //convert char1 to integer//
b = static_cast<int>(char2); //convert char2 to integer//
c = static_cast<int>(char3); //convert char3 to integer//
average = (a + b + c)/3; //average the numeric ASCII values of the characters//
avg2 = static_cast<char>(average); //convert the average to an ASCII character//