finding if entered character is uppercase, lowercase, or a number

I was already trying to write a console based simple program to find if the entered character is uppercase, lowercase, or a number (which could be a floating point value).

Someone reference me to this page:
http://www.cplusplus.com/reference/iostream/istream/peek/

Please have a look on the code which also contains my questions and comments. Thanks. I understand it take a lot time and energy to help with such stuff. I genuinely appreciate your help. Please don't forget I'm an outright beginner.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// istream peek
#include <iostream>
using namespace std;

int main ()
{
  char c; // what is c here?
  int n; // n is an integer value, right?
  char str[256]; // could you please tell what this is?

  cout << "Enter a number or a word: ";
  c=cin.peek(); // what is this? i have seen terms with () are at the top?

  if ( (c >= '0') && (c <= '9') ) // can't c be less than 0 and greater than 9?
  {
    cin >> n;
    cout << "You have entered number " << n << endl;
  }
  else
  {
    cin >> str;
    cout << " You have entered word " << str << endl;
  }

  return 0; // what function does it serve here?
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main ()
{
  char c; // what is c here? this is a Temporary holder of info of a single character type. 
  int n; // n is an integer value, right? This is a holder of number type.
  char str[256]; // could you please tell what this is?   this is a block of data of single character type 256 elements long.

  cout << "Enter a number or a word: ";
  c=cin.peek(); // what is this? i have seen terms with () are at the top?
  // Peek is basically looking at the first character that was entered in.  

  if ( (c >= '0') && (c <= '9') ) // can't c be less than 0 and greater than 9?  Correct.
  // tests similar to this one would test for 'a' to 'z' for a lowercase or 'A' to 'Z' for uppercase.
  {
     // If it falls in this we need to populate n for a actual integer.
    cin >> n;
    cout << "You have entered number " << n << endl;
  }
  else
  {
      //If we fall here we are filling str[256] 
    cin >> str;
    cout << " You have entered word " << str << endl;
  }

  return 0; // what function does it serve here?
  // if you look at the top it has 'int' in front of 'main'  This is returning an integer type that 'main' is requiring.  If it isn't present you might get a warning in a compiler.
}
Last edited on
Thanks a lot, Azagaros. I really appreciate your help.

Unfortunately I wasn't able to understand most of it because of my own limitations. Perhaps I will return to this thread some time later.

Best wishes
Jackson
Topic archived. No new replies allowed.