-3

I have an assignment where the user enters a student name in the format ( last name, first name). Can you help me figure out how to capitalize the first letter for both the first name and the last name?

I was using this to turn the user input into an array, so I could have the first letter capitalized, but when I did this, I had trouble getting it to work outside of the for loop.

for (int x = 0; x < fName.length(); x++) 
{ 
    fName[x] = tolower(fName[x]); 
} 
fName[0] = toupper(fName[0]);
Gardener
  • 2,591
  • 1
  • 13
  • 22
Mir.e.222
  • 23
  • 1
  • 2
  • 9
  • 3
    Which part are you stuck on? Hard to help when I don't know what the problem is. Or did you just want me to write the code for you? – john Apr 09 '19 at 19:32
  • 1
    As usual break the problem down, How do you loop through the latters of a string? How to you detect if a letter is the first letter of a name? How do you get the upper case version of a lower case letter? Answer the questions, put the pieces together and problem solved. – john Apr 09 '19 at 19:37
  • I was using this to turn the user input into an array, so I could have the first letter capitalized, but when I did this, I had trouble getting it to work otside of the for loop. for (int x = 0; x < fName.length(); x++) { fName[x] = tolower(fName[x]); } fName[0] = toupper(fName[0]); – Mir.e.222 Apr 09 '19 at 19:38
  • 1
    If you show any code you have written in the question, you are **much** more likely to get some help. – john Apr 09 '19 at 19:40
  • Read about C++ [``](http://www.cplusplus.com/reference/cctype/) – Basile Starynkevitch Apr 09 '19 at 19:40
  • 2
    A [mcve] is the *minimum* we need to be able to help you. But go research `toupper` and friends, I *guess*.. – Jesper Juhl Apr 09 '19 at 19:41
  • This is my first post here so i'm not too familiar with this site, my apologies. – Mir.e.222 Apr 09 '19 at 19:41
  • I see no need to use an array. Since you seem to have my questions 1 and 3 sorted, I suggest you have a good think about question 2. It's easier than you are thinking (probably). – john Apr 09 '19 at 19:43

1 Answers1

1

I used your code and just added some parsing around it. You really are very close.

I can't help myself. For user input, I always use getline() followed by a stringstream to parse the words from the line. I find it avoids a lot of edge cases that get me into quicksand.

When getline() gets an input, it returns true unless it has problems. If the user inputs Ctrl-d, it will return false. Ctrl-D is basically an EOF (end of file) code, which works well in this case (as long as you are not trying to input the Ctrl-d from inside your debugger. Mine does not like that.

Note that I am using std::string in place of an array. std::string can be treated like an array for subscripting, but it prints nicely and has other functions that make it better for processing character strings.

#include <iostream>
#include <string> // Allow you to use strings
#include <sstream>

int main(){

  std::string input_line;
  std::string fName;
  std::string lName;

  std::cout << "Please enter students as  <lastname>, <firstname>\n"
               "Press ctrl-D to exit\n";

  while(std::getline(std::cin, input_line)){
    std::istringstream ss(input_line);

    ss >> lName;
    // remove trailing comma.  We could leave it in and all would work, but
    // it just feels better to remove the comma and then add it back in
    // on the output.
    if(lName[lName.size() - 1] == ',')
      lName = lName.substr(0, lName.size() - 1); // Substring without the comma

    ss >> fName;

    for (int x = 0; x < fName.length(); x++)  // could start at x=1, but this works.
    {
      fName[x] = tolower(fName[x]);  // make all chars lower case
    }
    fName[0] = toupper(fName[0]);

    for (int x = 0; x < lName.length(); x++)
    {
      lName[x] = tolower(lName[x]);
    }
    lName[0] = toupper(lName[0]);

    std::cout << "Student: " << lName << ", " << fName << std::endl;
  }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Gardener
  • 2,591
  • 1
  • 13
  • 22