So whats seems to be the problem? What code do you have so far?
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
I seriously doubt that those are the only "commands" you can use. The more likely scenario is that you pulled that list from memory and those are the only ones you remember using before. This is evidenced by the randomness and you listing the cin and cout objects, but not listing the header which is required to use them.
The keys to making the most of a programming class are research and experimentation. Take the time to research the language, then experiment with any new commands you find that look relevant to your situation.
I suggest you start by creating your program's backbone (i.e. #includes, using statements, main(), etc.). Then, once you do, ask your prof about the header, do some research, and start experimenting.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
OK, I think I get it. You don't have to have "case" for each character, but you can use logical operand because based on ASCII each character has it's own number.
Example: if you want to check for lower case characters
if ((ch>='a') && (ch<='z'))
{
cout <<"lower case " <<ch;
}
In theory, this is correct. In practice it's not safe to assume that the alphabetical characters have contiguous values and/or aren't mixed with other characters. While you're likely to be working with some variant of ASCII or Unicode (where that assumption holds for the latin alphabet), there are other character sets where your code would break in subtle ways, such as EBCDIC . Further, other languages beyond English won't work properly with your test.
The safest option is, as usual, the standard library. std::islower() will give you the correct result regardless of character set:
// Test ch under the current locale
if (std::islower(ch, std::locale("")))
{
std::cout << "Lower case: " << ch << '\n';
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I tested it in practice and it worked...
It's not required to compile. toupper() is an overloaded function, and transform() cannot differentiate between the overloads. If it works for you then that means you're relying on behavior specific to your compiler.
However, even if you fix the problem by wrapping toupper(), it's still not a good solution:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
struct to_upper_case {
int operator()(int ch) {
return toupper((unsigned char)ch);
}
};
int main()
{
string ch;
cout << "Insert String: ";
cin >> ch;
string ch2 = ch;
transform(ch2.begin(), ch2.end(), ch2.begin(), to_upper_case());
if (ch == ch2)
cout << "upper case '" << ch << "'\n";
else
cout << "lower case '" << ch << "'\n";
}
The problem is that you're duplicating the string when you don't need to. The test checks all characters in ch for matching case, which can be done in place quite easily given a minor change:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
struct is_upper_case {
int operator()(int ch) {
return isupper((unsigned char)ch);
}
};
int main()
{
string ch;
cout << "Insert String: ";
cin >> ch;
bool all_upper = all_of(ch.begin(), ch.end(), is_upper_case());
if (all_upper)
cout << "upper case '" << ch << "'\n";
else
cout << "lower case '" << ch << "'\n";
}
Or prior to C++0x (where std::all_of() was defined) you might choose an option such as find_if():
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
struct is_lower_case {
int operator()(int ch) {
return islower((unsigned char)ch);
}
};
int main()
{
string ch;
cout << "Insert String: ";
cin >> ch;
bool all_upper = find_if(ch.begin(), ch.end(), is_lower_case()) == ch.end();
if (all_upper)
cout << "upper case '" << ch << "'\n";
else
cout << "lower case '" << ch << "'\n";
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401