c++ - Capitalizing Strings Input from User -


i'm working on problem have:

the user input number of names in list , input names. have change strings every name has first character capitalized , rest should low letters.

if names input this:

2 elizabeth paul 

the output should this:

elizabeth paul 

this i've tried:

#include <iostream> #include<string> #include <cctype> #include <stdio.h> #include <ctype.h>  using namespace std; const int fylkjastaerd = 100; int main() {         int fjoldiord = 0;      //get number of students     cout << "enter number of students: ";     cin >> fjoldiord;      string nofn[fylkjastaerd];      //get student names     (int i=0; i<fjoldiord; i++)     {          cin >> nofn[i];           if (nofn.length()>0)          {              nofn[0] = std::toupper(nofn[0]);              (size_t = 1; < nofn.length(); i++)              {                   nofn[i] = std::tolower(nofn[i]);              }          }     }     cout << nofn[i] << endl;      return 0; } 

the program not compiling , gives me error:

request member lenght in nofn of non-class type 'std::string[100] 

question:

could please give me pointers on doing wrong?

note:

i've done simpler version of input name , works fine.

    string name;     cout << "please enter first name: ";     cin >> name;      if( !name.empty() )     {         name[0] = std::toupper( name[0] );          for( std::size_t = 1 ; < name.length() ; ++i )             name[i] = std::tolower( name[i] );     }     cout << name << endl;      return 0; } 

the code:

    string nofn[fylkjastaerd]; 

is array of strings. need add [index] string.

consequently can't ask length of nofn.

you can do

nofn[i].length(); 

to length of string @ position in array of strings.

further use variable name twice. pretty bad.

maybe work better (but have not tried myself):

//get student names (int i=0; i<fjoldiord; i++) {      cin >> nofn[i];       if (nofn[i].length()>0)      {          nofn[i][0] = std::toupper(nofn[i][0]);          (size_t j = 1; j < nofn[i].length(); j++)          {               nofn[i][j] = std::tolower(nofn[i][j]);          }      }  } cout << nofn[i] << endl; 

however...

i don't think program want. cin string stop @ space name in trunks - not 1 string name. name including spaces use getline this:

string name; getline(cin, name); 

this complicate upper/lower case code because have detect spaces. maybe do:

#include <iostream> #include<string> #include <cctype> #include <stdio.h> #include <ctype.h>  using namespace std;  void fixstring(string& s) {     bool douppercase = true;      (int i=0; < s.length(); i++)     {         if (s[i] == ' ')         {             douppercase = true;         }         else         {              if (douppercase)              {                  s[i] = std::toupper(s[i]);                  douppercase = false;              }              else              {                  s[i] = std::tolower(s[i]);              }          }     } }  const int fylkjastaerd = 100; int main() {         int fjoldiord = 0;      //get number of students     cout << "enter number of students: ";     cin >> fjoldiord;     while (cin.get() != '\n')     {         continue;     }      string nofn[fylkjastaerd];      //get student names     (int i=0; i<fjoldiord; i++)     {          getline(cin, nofn[i]);          fixstring(nofn[i]);          cout << nofn[i] << endl;     }      return 0; } 

Comments