Very basic... getting a name capitalized

Hello,

I'm having an issue with my little program. I've been able to do variations where I can get all letters capitalized, but I can't figure out what to add to my prog that will capitalize only the first letter of a name.

Here is what I have so far:

#include <iostream>
#include <string>
using namespace std;


int main()
{
string lowerCase;
string upperCase;
char letter;

cout << "Please enter a name " << endl;
cin >> lowerCase;


upperCase = (lowerCase[0] - 32);
cout << upperCase << endl;

system("PAUSE");
return 0;
}
(lowerCase[0] - 32) returns a character, not a string

set upperCase = lowerCase and modify the1st character in upperCase
You're setting the string upperCase to be just the first letter of lowerCase. One way to change this is by taking out the upperCase variable and the operation, then this:
lowerCase[0] -= 32;

A revised version of your code would be the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;


int main()
{
string lowerCase;

cout << "Please enter a name\n>";
cin >> lowerCase;

lowerCase[0] -= 32;
cout << lowerCase << endl;

cin.clear();
cin.sync();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return 0;
}


EDIT: Or do what Bazzy said. Bazzy's smart. :D
Last edited on
Thank you... I was able to figure out what I did wrong :)
This solution only works if your string is in lowercase. I used this to capitalize in my project that I am working on and quickly realized if the letter is already capital you will get a different character. This is how I did it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

int main() {
    string m;
    char h;
    cin >> m;
    if (m[0] > 90) {
       h = m[0] - 32;
       m[0] = h;
       }
    cout << m;
    cin >> m;
    return 0;
}
     

Thank you for giving me the idea on how to do this, though. Very helpful.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    string lowerCase;
    
    cin >> lowerCase;
    
    lowerCase[0] = toupper(lowerCase[0]);
    cout << lowerCase << endl;
    
    system("pause");
    return 0;
}
Last edited on
Here's a little module for you to do that kind of stuff.
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
// stringcase.hpp
//
// Copyright (c) 2009 Michael Thomas Greer
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt

#pragma once
#ifndef STRINGCASE_HPP
#define STRINGCASE_HPP

#include <algorithm>
#include <cctype>
#include <functional>
#include <string>

template <typename CharType, typename TraitsType, typename AllocType>
std::basic_string <CharType, TraitsType, AllocType>
uppercase( const std::basic_string <CharType, TraitsType, AllocType> & s )
  {
  std::basic_string <CharType, TraitsType, AllocType> result( s.length(), '\0' );
  std::transform(
    s.begin(),
    s.end(),
    result.begin(),
    std::ptr_fun <int, int> ( std::toupper )
    );
  return result;
  }

template <typename CharType, typename TraitsType, typename AllocType>
std::basic_string <CharType, TraitsType, AllocType>
lowercase( const std::basic_string <CharType, TraitsType, AllocType> & s )
  {
  std::basic_string <CharType, TraitsType, AllocType> result( s.length(), '\0' );
  std::transform(
    s.begin(),
    s.end(),
    result.begin(),
    std::ptr_fun <int, int> ( std::tolower )
    );
  return result;
  }

template <typename CharType, typename TraitsType, typename AllocType>
std::basic_string <CharType, TraitsType, AllocType>
titlecase( const std::basic_string <CharType, TraitsType, AllocType> & s )
  {
  std::basic_string <CharType, TraitsType, AllocType> result = lowercase( s );
  typename std::basic_string <CharType, TraitsType, AllocType> ::iterator iter = result.begin();
  while (iter != result.end())
    {
    *iter = toupper( *iter );
    iter = std::find_if( iter, result.end(), std::not1( std::ptr_fun <int, int> ( std::isalpha ) ) );
    iter = std::find_if( iter, result.end(),            std::ptr_fun <int, int> ( std::isalpha )   );
    }
  return result;
  }

#endif

// end stringcase.hpp 

And, of course, an example of how to use it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

#include "stringcase.hpp"

int main()
  {
  string a( "Hello world!" );

  cout << a << endl;
  cout << lowercase( a ) << endl;
  cout << uppercase( a ) << endl;
  cout << titlecase( a ) << endl;

  return 0;
  }

This is all a bit fancier than it need be... you can get rid of all the template stuff if you like and just work on char strings:
1
2
3
4
std::string toupper( const std::string& s )
  {
  ...
  }

Also, the titlecase function capitalizes the first letter of every sequence of alphabetic characters in the string, not just s[0]. I don't know if you need it that fancy or not. A simpler version would be:
1
2
3
4
5
6
std::string titlecase( const std::string& s )
  {
  std::string result = lowercase( s );
  result[ 0 ] = toupper( result[ 0 ] );
  return result;
  }

Sorry for all the boilerplate... I know this stuff is fairly obvious, but it is necessary. Feel free to chop out the things you need.

Hope this helps.
Topic archived. No new replies allowed.