• 0

[C++] Changing Case


Question

bool getName(ifstream& inFile, ofstream& outFile)

{

customer Temp;

if (inFile >> Temp.id >> Temp.firstName >> Temp.lastName)

{

outFile << tolower(string Temp.lastName) << ','

<< Temp.firstName << endl;

cout << "ID# " << Temp.id;

return true;

}

else

{

return false;

}

}

This is C++. Temp.lastName and Temp.firstName are in Caps. How do I make it so it formats it so the first letter of the first and last name are caps and the rest arent?

Edited by aitf311
Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Please remember to mention the name of the programming language in your topic title. It helps people to help you more easily.

So topics should be "[Language] Subject", e.g "[VB] Reading text files".

Thank you.

Link to comment
Share on other sites

  • 0

just remember that strings in c++ are just arrays of characters with a null terminator as the last character

in the array. What you need to do is just access the first character and change it, something like this should work:

After you have lowered the string, do this

Temp.lastName[0]=toupper(Temp.lastName[0]);
Temp.firstName[0]=toupper(Temp.lastName[0]);

And that should work. Hope that helps

Link to comment
Share on other sites

  • 0

Causes 2 errors for me:

C:\Program Files\Microsoft Visual Studio\MyProjects\Proj8\main.cpp(106) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

C:\Program Files\Microsoft Visual Studio\MyProjects\Proj8\main.cpp(106) : error C2143: syntax error : missing ';' before '<<'

This is supposed to make an all caps name turn into first capped rest lowercased like MARK--->Mark

Link to comment
Share on other sites

  • 0
Causes 2 errors for me:
Both errors are on line 106 (according to the compiler). Therefore, please post lines 105 - 107.
This is supposed to make an all caps name turn into first capped rest lowercased like MARK--->Mark

In that case, you need to make all the letters except the first lowercase.

Sorta like this:

for (int i = 1; i &lt; Temp.lastName.length(); i ++)
    Temp.lastName[i] = tolower(Temp.lastName[i]);

Link to comment
Share on other sites

  • 0

SO

bool getName(ifstream& inFile, ofstream& outFile)

{

customer Temp;

if (inFile >> Temp.id >> Temp.firstName >> Temp.lastName)

{

outFile <<

for (int i = 1; i < Temp.lastName.length(); i ++)

Temp.lastName = tolower(Temp.lastName); << ','

<< Temp.firstName << endl;

cout << "ID# " << Temp.id;

return true;

}

else

{

return false;

}

}

??

Link to comment
Share on other sites

  • 0

bool getName(ifstream& inFile, ofstream& outFile)

{

customer Temp;

if (inFile >> Temp.id >> Temp.firstName >> Temp.lastName)

{

for (int i = 1; i < Temp.lastName.length(); i ++)

Temp.lastName = tolower(Temp.lastName);

for (i = 1; i < Temp.firstName.length(); i ++)

Temp.firstName = tolower(Temp.firstName);

outFile << Temp.lastName << ','

<< Temp.firstName << endl;

cout << "ID# " << Temp.id;

return true;

}

else

{

return false;

}

}

Is the finished product, thanks alot guys

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.