cin not returning typed in value.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**********************************************

*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

*Specification:

*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;
getch();
}

//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
system("CLS");

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

}else{

//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
}else{
//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
system("CLS");

//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

}else{

//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);

}
Last edited on
inital = toupper(inital[0]) + "."; type issues int + const char* will increment the pointer.
Use
1
2
inital = toupper(inital[0]);
inital += std::string(".");
Topic archived. No new replies allowed.