Writiing a C++ program

Hi,

I'm a CS student. I'm new to this field. The most challenging part for me is how to start the program. I'm not looking for the full/final answer, I just need some guidance.

here is the questions,
Write a program (test_char.cpp) that asks the user to input a character. Then,:
- Check to see if the character entered is one of the following choices:

1. an uppercase letter
2. A lower case letter.
3. A digit
4. A whitespace (including single space, newline, vertical tab, regular tab)
5. None of the above
- you can't use of the cctype library functions (isapha, isdigit, etc)
- Allow the user to repeat the process as many times as he/she wants.


Thank you!

Last edited on
1
2
3
4
5
6
7
8
9
10
char x;
cin<<a;
swich (a){
case (1):
/*write your code here*/
break;
case(2):
/*write your code here*/
break;
etc...
closed account (91vUpfjN)
of course, cin is great, getline is better. :D (tip: set the delimeter to space)

oh and a while loop wouldn't go a miss either.
Last edited on
Are we allowed to assume ASCII or some kind of character encoding where a-z, A-Z, and 0-9 are contiguous? (I don't know how many people still use e.g. EBCDIC, but...)

If so, you can check if the character is >= 'a' and <= 'z' for lowercase, >= 'A' and <= 'Z' for uppercase, and similarly for digits.

Otherwise, you can just as easily make an array:
1
2
3
const std::string lowercase = "abcdefghijklmnopqrstuvwxyz";
const std::string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const std::string digits = "0123456789";
and then look for the character in each of those strings (perhaps using std::string::find).
Thank you so much!!
Ok so this is what I have so far,
#include <iostream>
using namespace std;
int main()
{
int lower_case, upper_case, count;
char c;
if (c >= 'a' && c <= 'z')
cout << lower_case;
if (c >= 'A' && c <= 'Z')
cout << upper_case;
if (c >= 97 && c <= 122)
cout << lower_case;
if (c >= 65 && c >= 90);
cout << upper_case;

count++;
cout << "c\ " << c << "\" has " << count <<"whitespace\n";

return 0;

}
when I try to run the program it says variables: c, lower_case and upper_case hasn't been initialized yet. I don't know what to do
1
2
3
int lower_case=0;
int upper_case=0;
int count=0;


your getting the initialize error because your outputting before actually reading anything into the variables.

 
cin>>c


that might actually work better with a get command someone with a bit more experience could probably advise better.
Last edited on
I was actually able to finish the first part of my assignment.
#include <iostream>
#include <string>
using namespace std;
int main()

{
int lowercase, uppercase;
char input = 'c', newline = '\n', singlespace = ' ', verticaltab = '\v', horizantaltab = '\t';

cout << "enter the input\n";
cin.get(input);
{
if (input >= 97 && input <= 122)
cout << " this is a lowercase input\n";
}
{
if (input >= 65 && input <= 90)
cout << " this is an uppercase input\n";
}
{
if (input >= '0' && input <= '9')
cout << " this is a digit input\n";
}
{
if (input == '\n' && input == ' ' && input =='\v' && input == '\t')
cout << " this is a whitespace input\n";
}
if (input == '%')
cout << "none of the above\n";
return 0;
}

Thanks everyone
Topic archived. No new replies allowed.