problem with toupper()
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: problem with toupper()

  1. #1
    Join Date
    Apr 2013
    Posts
    42

    problem with toupper()

    Hi,

    I am having trouble with this line of code:

    cout << "Lower to upper: t" << toupper(str);

    The str is underlined and it won't compile.

    Code:
    #include<iostream>
    #include<string>
    #include<cctype>
    
    using namespace std;
    
    void upr(const string & str);
    
    int main()
    {
    	string strinpt;
    	char quit = 'Q';
    	while (quit != 'q')
    	{
    		cout << "Enter phrase in lower case to be turned into upper case: " << "\n";
    		getline(cin, strinpt);
    		upr(strinpt);
    		cout << "Do you want stop entry? " << "\n";
    		cin >> quit;
    	}
    	return 0;
    }
    void upr(const string & str)
    {
    	 cout << "OutPut: " << str << "\n";
    	 cout << "Lower to upper: t" << toupper(str);
    }
    Any help would be appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: problem with toupper()

    toupper is used to convert a single character to uppercase, not a string.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2013
    Posts
    42

    Re: problem with toupper()

    Thank you

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    Re: problem with toupper()

    If you are using windows, there is the function CharUpperBuff(..) which converts lowercase characters in a buffer to uppercase characters. Also CharLowerBuff(..) which converts uppercase characters in a buffer to lowercase.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2013
    Posts
    42

    Re: problem with toupper()

    Hi,
    I am having a problem accessing the string that is passed to the upr function. I want to make the string an array. I have tried many iterations of code that I thought might work, but to no avail. Could someone please give me some direction?

    This line is flagged with &str the culprit.


    Code:
    #include<iostream>
    #include<string>
    #include<cctype>
    
    using namespace std;
    
    void upr(const string & str);
    
    int main()
    {
    	string strinpt;
    	char quit = 'Q';
    	while (quit != 'q')
    	{
    		cout << "Enter phrase in lower case to be turned into upper case: " << "\n";
    		getline(cin, strinpt);
    		upr(strinpt);
    		cout << "Do you want stop entry? " << "\n";
    		cin >> quit;
    	}
    	return 0;
    }
    void upr(const string & str)
    {
    	char* pstr(&str);
    	int i = 0;
    	cout << "Lower to Upper: ";
    	for (i = 0; i < 40; i++)
    		cout << toupper(pstr + i);
    }

  6. #6
    Join Date
    Apr 2013
    Posts
    42

    Re: problem with toupper()

    Hi 2kaud,
    Thanks for the input. This is a exercise in passing parameters in reference. I need as much programming as I can get. Learning is the major issue here. At least for me. Since I can't talk to the "book" I have no mentor. That is why these forum are so critical for everyone.

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,962

    Re: problem with toupper()

    Quote Originally Posted by FenixRising View Post
    I am having a problem accessing the string that is passed to the upr function. I want to make the string an array. I have tried many iterations of code that I thought might work, but to no avail. Could someone please give me some direction?
    The direction: std::string documentation

    The hint: You have to always start with finding and reading through official documentation on any problem you face. Of course, to do that you have to be able to formulate your problem. Properly formulated problem is 50% solved problem.

    Quote Originally Posted by FenixRising View Post
    This line is flagged with &str the culprit.
    Right. And taking an address of std::string never makes it be a character array address. Those two types are different, and you have to find a way to express your intention for compiler more explicit. If you carefully read the link above, you can find std::string::c_str() that seems what you were looking for.


    const char* c_str() const;

    Get C string equivalent
    Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.

    This array includes the same sequence of characters that make up the value of the string object plus an additional terminating null-character ('\0') at the end.
    The lesson: classes typically provide member functions (or operators sometimes) that transform objects to their congeneric types, or just expose internal generic types they wrap.
    Best regards,
    Igor

  8. #8
    Join Date
    Apr 2013
    Posts
    42

    Re: problem with toupper()

    Hi Igor,
    The exercise that I am working on specifically says to use toupper to take the lower case phrase and make it a uppercase phrase out of it. All the solutions presented to me have been using some other functionality.
    There must be an easy way to solve this exercise.

    Thanks,
    Bob

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    Re: problem with toupper()

    There is. Igor gave you the hint as to how this can be done. Look at the c_str() string function.

    http://www.cplusplus.com/reference/string/string/c_str/

    See if you can alter your upr function using this. You might also want to look at the length() string function as well.

    http://www.cplusplus.com/reference/s...string/length/
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,426

    Re: problem with toupper()

    Quote Originally Posted by FenixRising View Post
    Hi Igor,
    The exercise that I am working on specifically says to use toupper to take the lower case phrase and make it a uppercase phrase out of it. All the solutions presented to me have been using some other functionality.
    There must be an easy way to solve this exercise.
    Look at the exercise in general terms.

    What if you had an array of numbers, and I told you to add 1 to each number? How would you do it?

    All you're doing is taking an array of whatever, and doing some sort of transformation on each of the elements. What construct in C++ allows you to go through each element?

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Apr 1999
    Posts
    27,426

    Re: problem with toupper()

    Code:
    void upr(const string & str)
    First, if I called this function, how would I get back the converted string? You don't return a new string, and the argument you're passing cannot be changed (since it is const). So right there, there is no provision made to return the changed string. Once the function returns, all of that work you did internal in the function goes away in a puff of smoke.

    So you need to fix this so that a new string is returned, and/or the argument passed is successfully changed.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Apr 2013
    Posts
    42

    Re: problem with toupper()

    Hi 2kaud,
    I have looked at the documentation and have changed my program. I am assuming that cstr is a pointer and that pointer arithmetic should work. But, I still have errors.


    Error 1 error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. - Which include file is strcpy_s in?
    Error 2 error C2664: 'int toupper(int)' : cannot convert parameter 1 from 'const char *' to 'int' 35 1


    Code:
    #include<iostream>
    #include<string>
    #include<cstring>
    #include<cctype>
    
    using namespace std;
    
    void upr(string & str);
    
    int main()
    {
    	string strinpt;
    	char quit = 'Q';
    	while (quit != 'q')
    	{
    		cout << "Enter phrase in lower case to be turned into upper case: " << "\n";
    		getline(cin, strinpt);
    		upr(strinpt);
    		cout << "Do you want stop entry? " << "\n";
    		cin >> quit;
    	}
    	return 0;
    }
    void upr(string & str)
    {
    
    	char * cstr = new char[str.length() + 1];
    	std::strcpy(cstr, str.c_str());
    	
    	int i = 0;
    	do
    	{
    		cout << toupper(cstr + i);
    		i++;
    	}
    		while (i < 10);
    		cout << "\n";
    }

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    Re: problem with toupper()

    Try this. Note that it doesn't convert the string as mentioned by Paul in post #11. It just outputs the string in uppercase as per your original upr function.

    Code:
    void upr(const string& str)
    {
    	for (const char* ptr = str.c_str(); *ptr; ++ptr)
    		cout << (char)toupper(*ptr);
    
    	cout << endl;
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: problem with toupper()

    Frankly, I think the use of c_str is a red herring here. You can effectively treat a std::string as if it were an array of char, so use that to your advantage. Don't over-complicate by calling c_str.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  15. #15
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    Re: problem with toupper()

    Quote Originally Posted by FenixRising View Post
    The exercise that I am working on specifically says to use toupper to take the lower case phrase and make it a uppercase phrase out of it. All the solutions presented to me have been using some other functionality.
    Further to your previous posts, have you decided upon a book yet from which to learn c++? Exactly what is the exercise requiring as there are different ways of defining and coding the upr function depending upon what is required.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center