Re: problem with toupper()
toupper is used to convert a single character to uppercase, not a string.
Re: problem with toupper()
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.
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);
}
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.
Re: problem with toupper()
Quote:
Originally Posted by
FenixRising
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
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.
Quote:
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.
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
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/
Re: problem with toupper()
Quote:
Originally Posted by
FenixRising
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
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
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";
}
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;
}
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.
Re: problem with toupper()
Quote:
Originally Posted by
FenixRising
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.