I am writing some code that is manipulating strings. In this program there are two functions one to get the first/last name and one to get the middle inital. the first and last name function works properly however the middle inital function is not. This function should take an inital (or even a name if the user misreads) trim the string down to one character capitalize it and put a "." after the letter. I have experimented with just one letter and a full name and both times the variable being set in that function is "dle inital" here is my code:


*Lab Five Lab 5.cpp Takes user enter variables to determine areas, and volumes

for a right-circular cylinder

*Author: Mike Heintzman

*Date: October 17 2012

*Purpose: Lab 5 for COSC 1557E


*Input (keyboard):

*Output (screen):


//include input/output library
#include <iostream>
//This is included for our mather operations
#include <cmath>
//This is included for the getline() functions
#include <sstream>
//this is included for the string type
#include <string>
#include <conio.h> //just temporarily in the code to use the getch() keeping the window open for testing purposes

using namespace std;

//Prototype function for nameInfo()
string nameInfo(string part);

//prototype function to get the middle inital
string middleInital ();

//main function
int main(){

//Create a var for first name
string firstName;
//Create a var for last name
string lastName;
//Create a var for middle initial
string userInital;
//create a variable to store the full name in including the inital
string fullName;

//Explain what the program does
cout << " This Program will prompt you for parts of your name \n and preform functions on it."<<endl;

//Call the nameinfo function to set the firstname
firstName = nameInfo ("first");

//now prompt for the last name
cout <<" \n Thank you, " << firstName << endl;

//call the nameInfo function for the last name
lastName = nameInfo("last");

//Now prompt for the middle inital
cout <<"\n Alright " << firstName << " "<< lastName << " now we just need your middle inital" <<endl;

//call the middle inital function
userInital = middleInital();

fullName = firstName + " " + userInital + " " + lastName;
cout << "\n The full name is: " << fullName;

//define the nameInfo function. Part is set in the Main function to let us know what we are here for
string nameInfo (string part){

//create a variable to hold the users answer
string userInput;
//set a variable to know when to exit the loop
bool finished = false;

//put this in a while loop to allow changes
while (finished == false){

//Prompt the user to enter a name
cout <<
" \n Now please enter your " << part << " name.";

//Assign users input to a variable use cin instead of getline to only get the first name incase user types more
cin >> userInput;

//check to see if the entered input has any numbers in it.
if (userInput.find_first_of("0123456789`~!@#$%^&*()-_=+[{]}\|;:,<.>/?") != userInput.npos){

//if in here the name has a number in it

//clear the screen of clutter

//prompt the user to try again
cout << " \n Sorry the name you have entered cannot be accepted. \n Please only use letters A-Z.";


//name is good set finished to true
finished = true;



//Make sure each letter case is propper
for (int i = 0; i < userInput.length();i++){

//if it is the first letter we capitalize it
if (i == 0){
//this is taking the first letter (i at zero) and making it a capital
userInput[i] = toupper(userInput[i]);
//if it is not the first letter then do this
//this makes every character except the first one lower
userInput[i] = tolower(userInput[i]);
return (userInput);


//write the definition for the middleInital variable

string middleInital(){

//set a variable for the middle inital (var is just called inital because function is called middleinital)
string inital;

//create a variable to know when we are finished the loop
bool finished = false;

//create a while statment to run through error testing
while (finished == false) {
//prompt the user to enter their middle inital
cout <<"\n Please enter your middle inital ";
//set userinput to the variable
cin >> inital;

//cehck to see if user entered anything
if (inital.length() ==  0 || inital.find("0123456789`~!@#$%^&*()-_=+[{]}\|;:,<.>/?") != inital.npos ){

//if we are here then there was nothign entered. clear the screen

//inform the user of the error
cout <<" \n Sorry but your answer appeared to be invalid." << endl;

//check to see if user has a number in the name


//there were no errors
finished =true;



//Now we just do some variable minipulate incase the user decided to enter a full name
inital = toupper(inital[0]) + ".";

//return the inital
return (inital);


Here's the correction, you can't combine all that like you did

//Now we just do some variable minipulate incase the user decided to enter a full name

inital = inital.substr(0,1) + ".";
inital[0] = toupper(inital[0]);

//return the inital
return (inital);

This article has been dead for over six months. Start a new discussion instead.