Ok, so basically I have an assignment in which I have to determine if what the user typed in is a number between 0-9, a letter or a symbol. The commands I can use are:

cout
endl 
system("pause") 
system("cls")
#include<iomanip> 
setw(#) 
setfill('ch') 
"\t" 
cin 
getline(cin,s) 
bool 
char 
int 
double 
string 
#include<string> 
#include<cmath> 
#include<windows.h> → directiva utilizada para poder el comando de ::Sleep()
::Sleep(#) 
n.length() 


&& 
|| 

IF/ELSE:
-----if(condición)
-----{
-------// código
-----}else{
-------// código
-----}
(t)x → changes the variable x to variable type t
SWITCH:
-----switch(variable)
-----{
----------case n : // código
-------------------break;

----------default : // código
--------------------break;
-----}
FOR:
-----for(contador; condición salida; actualización)
-----{
--------// código
-----}
WHILE:
-----while(condición salida)
-----{
----------// código
-----}
DO/WHILE:
-----do
-----{
-------// código
-----}while(condición salida);

-- Nothing else. I don't need to use all of them obviously.

Normally i don't like to get outside help but this is really important for a project I'm doing. If anyone could take the time to help me that would be wonderful.

Recommended Answers

All 9 Replies

So whats seems to be the problem? What code do you have so far?

Are you in programming class? because if you are, you need to go back and start reading your lecture notes. We can help with your problems but not doing the assignment for you.

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 <iostream> 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 <cctype> header, do some research, and start experimenting.

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 <iostream> 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 <cctype> header, do some research, and start experimenting.

Let me be more clear. These ARE the commands I can use since this is what he has taught so far, therefore being the ones he wants us to use, he did however say that he was going to teach is this week a tip so that we don't have to make a case for each letter in the alphabet. No these are not pulled from my memory, they are from the class webpage. There was some stuff in-between the list that I had to delete and that is why the list looks weird.

What I'm asking is for someone to tell me a code, using those commands, that identifies whether what the user inputted is a ASCII symbol or a number between 0-9. And yes I have read my lecture notes quite a bit, still can't find an answer.

Side-note: He said TIP: That switch wasn't gonna be the only thing that we were gonna need to use.

I'm about to start the 2nd attempt at the program, I'll post the code here when im done, although I'm probably gonna be horribly wrong but will try.

I'm about to start the 2nd attempt at the program, I'll post the code here when im done, although I'm probably gonna be horribly wrong but will try.

It might be a start to post your first attempt. If your having problems getting the details right, try missing them out. Writing a bare skeleton and then filling in the details later can really help. Starting from something like:

int main()
{
   char letter;  // Use this to store the user's letter
   
   /* Ask user for a letter */

   /* Store the letter */

   /* Do something to the letter */
   
   /* Print out the result */
   
   return 0;
}

And then working from there can really help get in your mind what needs to be done in what order.

Have fun, don't panic and think hard about it.

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;
  }

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';
}

Just a quick question on the check for character case... would this not also be an acceptable check?

#include <cctype>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <istream>
#include <iostream>
#include <string>

using namespace std;

int main(){

	string ch = "";
	string ch2 = "";
	string uppercase = "";

	cout << "Insert String\n";
	cin>>ch;
	cin.ignore();

	ch2 = ch;
	transform(ch2.begin(), ch2.end(), ch2.begin(), toupper);

	if (ch == ch2){

		cout << "upper case " << ch << " \n";
	}
	else{

		cout << "lower case " << ch << " \n";
	}
	cin.get();

    return 0;
}

I tested it in practice and it worked... can be adapted to check a set amount of characters in a string etc... just want a second opinion on its use?

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";
}
commented: Very explanatory and educational +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.