Skipping my for loop

Here is one of the functions in a program I am writing. For some reason I cannot figure out why the for loop wont run. The for loop is right before the return, any advice related or not is appreciated. The lines cout << userInput[i] << " " << i <<endl; where just for me to see if anything is happening in the loop and like expected they did not show up.

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
//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]);
cout << userInput[i] << " " << i <<endl;
//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]);
cout << userInput[i] << " " << i <<endl;
}
}
return (userInput);

}
Last edited on
 
for (int i = 0; i > userInput.length();i++){

The comparison there should be <, not >. Unless the string's length is somehow negative, your loop won't work.
Last edited on
so it should be i >= userInput.length()?
No, like I said it should be i < userInput.length(). Think about it. Doing it the other way makes absolutely no sense.
Right I forgot how for loops work in C++ im thinking of PHP
Topic archived. No new replies allowed.