Hey guys! I'm requesting constructive criticism on the code that I've written so that I might get a better idea of how to strengthen my code while keeping it short and sweet, or even just pointing out unnecessary things or things that I should have added.
My assignment was:
Write a program to get a user’s name in Last, First format. Then create a “user id” for the person consisting of his first initial and up to the first 7 characters of the last name.
What is your name? Montoya, Inigo
Your user ID: imontoya
My code was successful, but I'd like to know if there was a better way to have written it or make it shorter.
My code is:
/* User ID Maker.cpp : Creates a user ID made of the first letter of the user's first
name and the first 7 letters of the user's last name.*/
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main(){
string strName;
cout << "Enter your full name in last, first format to create a user ID: ";
getline(cin, strName);
const string::size_type stCOMMA_LOC = strName.find(',');
if (stCOMMA_LOC != string::npos){ //Check that the user included the comma.
char cFirstLetter = strName[stCOMMA_LOC + 2]; //The first letter should appear 2 spaces after the comma.
string strUsername;
strUsername.push_back(tolower(cFirstLetter));
int nCounter = 0;
//Add the first seven letters to the username.
while (nCounter < stCOMMA_LOC && nCounter < 7)
strUsername.push_back(tolower(strName[nCounter++]));
cout << "Your username is: " << strUsername << endl;
}
else
cout << "You did not enter your name in the correct form." << endl;
return 0;
}
Thanks in advance for the help and criticism!