Upper/lower case

How can I make only my first letter upper case and all of the other letters lower case for the first, middle and last name. This program is a name formalize program, for an example if you wrote Mary Average User it would output User, Mary A. I want to be able to use it under #include <cctype> and it would be something like toupper and tolower with char. Please help me. Thanks


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

int main()

{
string name;
cout << "Enter your name: ";
getline(cin,name,'\n');

string first, last, middle, output;

string::size_type firstSpace = name.find(' ', 0);

first = name.substr(0, firstSpace);

string::size_type secondSpace = name.find(' ', firstSpace+1);

if(secondSpace == string::npos)
{

last = name.substr(firstSpace+1);

output = last + ", " + first ;
}
else
{

middle = name.substr(firstSpace+1, secondSpace-firstSpace-1);

last = name.substr(secondSpace+1);

output = last + ", " + first + " " + middle.at(0) + ".";
}

cout<< output <<endl ;
return 0;
}
closed account (E0p9LyTq)
Duplicate post:

http://www.cplusplus.com/forum/general/247113/
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
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>

int main()
{
    std::string full_name ;
    std::cout << "enter full name(first middle last): " ;
    std::getline( std::cin, full_name ) ;
    #ifndef NDEBUG
        std::cout << "\nuser input for full name: " << std::quoted(full_name) << '\n' ; 
    #endif 

    std::size_t pos = 0 ; // start tat the beginning of full_name

    std::string first_name, middle_name, last_name ;

    // skip leading white spaces
    while( pos < full_name.size() && std::isspace( full_name[pos] ) ) ++pos ;
    // extract first name
    for( ; pos < full_name.size() && !std::isspace( full_name[pos] ) ; ++pos )
        first_name += full_name[pos] ;

    // skip intermediate white white spaces
    while( pos < full_name.size() && std::isspace( full_name[pos] ) ) ++pos ;
    // extract middle name
    for( ; pos < full_name.size() && !std::isspace( full_name[pos] ) ; ++pos )
        middle_name += full_name[pos] ;

    // skip intermediate white white spaces
    while( pos < full_name.size() && std::isspace( full_name[pos] ) ) ++pos ;
    // extract last name
    for( ; pos < full_name.size() && !std::isspace( full_name[pos] ) ; ++pos )
        last_name += full_name[pos] ;

    // skip white white spaces
    while( pos < full_name.size() && std::isspace( full_name[pos] ) ) ++pos ;

    // if there are more characters left in the input string
    if( pos != full_name.size() ) std::cout << "error: too many name components\n" ;

    // if we reached end of the input string before we could read the last name
    else if( last_name.empty() ) std::cout << "error: too few name components\n" ;

    else
    {
        first_name.front() = std::toupper( first_name.front() ) ;
        last_name.front() = std::toupper( last_name.front() ) ;

        std::cout << last_name << ", " << first_name << ' '
                  << char( std::toupper( middle_name.front() ) ) << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/6f84d4876bab4bd2
How could I do it using this "static_cast<char>(std::toupper(name[0]))"
1
2
3
4
5
6
7
8
    else
    {
        first_name[0] = static_cast<char>( std::toupper( first_name[0] ) ) ;
        last_name[0] = static_cast<char>( std::toupper( last_name[0] ) ) ;

        std::cout << last_name << ", " << first_name << ' '
                  << static_cast<char>( std::toupper( middle_name[0] ) ) << '\n' ;
    }


There is an implicit conversion from int to char, which would be applied;
why do you want to use a static_cast when the result is being assigned to char?
Last edited on
My teacher has assigned me to use static_cast.
I have input the code you have provided me but when I try to run it shows this...


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

int main()

{

cout <<"Welcome to the Name Formalizing Program!!!/n";
string name;
cout << "Enter your name: ";
getline(cin,name,'\n');

string first, last, middle, output;

string::size_type firstSpace = name.find(' ', 0);

first = name.substr(0, firstSpace);

string::size_type secondSpace = name.find(' ', firstSpace+1);

if(secondSpace == string::npos)

else
{
first[0] = static_cast<char>( std::toupper( first[0] ) ) ;
last[0] = static_cast<char>( std::toupper( last[0] ) ) ;

std::cout << last << ", " << first << ' '
<< static_cast<char>( std::toupper( name[0] ) ) << '\n' ;
}
{

last = name.substr(firstSpace+1);

output = last + ", " + first ;
}
else
{

middle = name.substr(firstSpace+1, secondSpace-firstSpace-1);

last = name.substr(secondSpace+1);

output = last + ", " + first + " " + middle.at(0) + ".";
}


cout<< output <<endl ;
return 0;
}



output:
name.cpp***
name.cpp: In function ‘int main()’:
name.cpp:25:6: error: expected
primary-expression before ‘else’
else
^~~~
name.cpp:39:5: error:
‘else’ without a previous
‘if’
else
^~~~
please use code tags unless its a 1-2 liner. <> on the formatter to the side of the posting screen.

if(secondSpace == string::npos)

else

you can't do this. you must have a statement between if and else in c++.
you can do this

if(secondSpace == string::npos)
; //do nothing statement
else

but its terrible. it would be better to reverse the logic and skip the else:
if(secondSpace != string::npos)
code
Topic archived. No new replies allowed.