
Code: Select all
char* temp = new char[10];
temp = "question";Code: Select all
#include <algorithm>
#include <string>
inline void make_upper_case( std::string& s )
{ std::transform( s.begin(), s.end(), s.begin(), toupper ); }
Thank you for your help and sorry for my ignorance.main.cc:12: error: no matching function for call to `transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
main.cc: In function `int main()':
main.cc:28: error: invalid initialization of reference of type 'std::string&' from expression of type 'char*'
main.cc:12: error: in passing argument 1 of `void make_upper_case(std::string&)'
main.cc:24: warning: unused variable 'cap'
make: *** [main.o] Error 1
Code: Select all
std::string s( "question" );
make_upper_case( s ); // s is "QUESTION"Code: Select all
inline std::string make_upper_case_copy( std::string const& in )
{
std::string out( in );
std::transform( out.begin(), out.end(), out.begin(), toupper );
return out;
}Code: Select all
std::string s( "question" );
char* c = "answer";
std::string upper = make_upper_case_copy( s ); // upper is "QUESTION"
upper = make_upper_case_copy( c ); // upper is "ANSWER"
upper = make_upper_case_copy( "question" ); // upper is "QUESTION" 
I don't think you can use transform() in this instance. I also don't believe I can use a string in order to enter the characters from the cin. I, however, am not sure. I did purchase Accelerated C++ as it is apparent I need a better reference than I have currently.char * response=new char[10];
cout << "prompt user for entry.\n"
Then cin>>response;
response=make_upper_case(response);
This is not a complete program.jmack1010 wrote:I am having a very basic, but annoying problem with the function toupper and strings.
char *temp = new char[10];
temp="question";
while (*temp != '\0'){
if(islower(*temp))
*temp=toupper(*temp);
temp++;
}
It compiles, but the string temp is empty instead of capitalized. I would greatly appreciate any insight. Thank you.
Joe
Code: Select all
#include <iostream>
using namespace std;
int main()
{
char *temp = new char[10];
strcpy( temp, "Question\0" );
char *p = temp;
while( *p != 0 )
*p = toupper( *p++ );
cout << temp << endl;
delete [] temp;
}Code: Select all
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s = "Question";
transform( s.begin(), s.end(), s.begin(), (int(*)(int))toupper );
cout << s << endl;
}Code: Select all
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
inline void make_upper_case( std::string& s )
{ std::transform( s.begin(), s.end(), s.begin(), ::toupper ); }
int main()
{
std::cout << "Please enter your first name: ";
std::string name;
std::cin >> name;
make_upper_case( name );
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
Great! It's a wonderful book that will serve you well. Have a look at Chapter 1 to see how to use std::string.I did purchase Accelerated C++
See int2str's explanation.If you know why my original code would not capitalize the string please let me know.

the compiler error went away. Why must I cast a string as an int(*)int. I don't understand exactly what is going on.transform( s.begin(), s.end(), s.begin(), (int(*)(int))toupper );
The problem is that there are multiple versions of the std::toupper function defined in the STL. They are templated and use the type of the argument and return type to resolve which templated function to use. When the function is passed as an argument, without an obvious parameter, the compiler does not know how to resolve the template. Putting in the cast indicates to the compiler which parameter format to use.jmack1010 wrote:After I cast toupper as int2str suggested:the compiler error went away. Why must I cast a string as an int(*)int. I don't understand exactly what is going on.transform( s.begin(), s.end(), s.begin(), (int(*)(int))toupper );
Code: Select all
std::transform( s.begin(), s.end(), s.begin(), ::toupper );
Why must I cast a string as an int(*)int. I don't understand exactly what is going on.
Code: Select all
(int(*)(int))toupperCode: Select all
template<typename _CharT> _CharT toupper(_CharT, const locale&);