-1

I'm "helping" a friend with something he's trying to do in C++, but I'm stuck because of the limitations: he isn't supposed to use any library functions. The objective is to take user input and determine if the input is an uppercase letter, a lowercase letter, a number, or some other character (like *, &, or #).

Here's what I have so far:

#include<string>
#include<vector>
#include<iostream>
using namespace std;

int main() {
    string x = "";

    vector<string> uppercaseAlphabet = {"A","B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    vector<string> lowercaseAlphabet = {"a","b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

    cout<<"\nEnter a character.\n";
    getline(cin, x);
    if(std::find(uppercaseAlphabet.begin(), uppercaseAlphabet.end(), x) != uppercaseAlphabet.end()) {
        cout<<"The input was an uppercase character.";
    }
    if(std::find(lowercaseAlphabet.begin(), lowercaseAlphabet.end(), x) != lowercaseAlphabet.end()) {
        cout<<"The input was a lowercase character.";
    }

    return 0;

}

This isn't elegant, but it works so far. My problem is finding out when the user inputs a number. What's the best way to approach this?

I also don't know if I've violated the specifications by using things like find, begin, and end. Let me know if you think this wouldn't be allowed.

This is my first time writing any C++ so I'm sure my code doesn't follow any conventions. It also might be bad simply because of the using namespace std; line.

rustyshackleford
  • 692
  • 1
  • 7
  • 22
  • 2
    `std::find()` is a library function. – Barmar Oct 16 '17 at 20:30
  • What does "Without Standard Library functions" even mean? Why not just smash around in assembly mode? You'll have to skip containers if you're avoiding all of this because of some arbitrary constraints, but that ends up being basically C code. Assignments like this are ridiculous. It's like trying to teach English by making you write a poem using only single letter words. – tadman Oct 16 '17 at 20:30
  • @Barmar that's what I figured. I suppose I'll end up having to write something similar myself. – rustyshackleford Oct 16 '17 at 20:31
  • 5
    Assignments like this are such a waste of time... – Baum mit Augen Oct 16 '17 at 20:31
  • @tadman I feel the same way. Is this going to end up looking like a mess? – rustyshackleford Oct 16 '17 at 20:32
  • 2
    `std::getline` is a library function too. :P Truth is, C++ is quite solipsistic and damn near useless without the standard library. – cHao Oct 16 '17 at 20:32
  • @vipertherapper It will be a mess. If I got this assignment I'd make it as messy and obfuscated as possible, but still achieve every single objective of the assignment to the letter. Use templates judiciously. Use pointers to pointers to pointers to references to pointers for no other reason than because they're not "Standard Library" functions. That's just me, though. I won't fail this assignment for being a jerk! – tadman Oct 16 '17 at 20:33
  • @tadman That's so annoying. I don't know what my friend's professor is going for. I don't use C++ at all so I don't even know where to begin. – rustyshackleford Oct 16 '17 at 20:36
  • 1
    @vipertherapper Sadly this is not atypical. Most C++ "professors" have no idea what they're doing and are using training material that's woefully out of date, or exercises that amount to student brutality. Do what you can to get the best possible grade, then forget everything you've "learned" here and use the Standard Library to do things properly in any important code you're writing. – tadman Oct 16 '17 at 20:39
  • You can't take input without a library function. –  Oct 16 '17 at 20:40
  • 1
    @tadman Well said. – Ron Oct 16 '17 at 20:41
  • @tadman I write mostly Python and this isn't my assignment. I'm not surprised that C++ professors assign this kind of thing, though. I can't imagine writing anything useful with these kind of constraints. Do they think this will benefit their students? – rustyshackleford Oct 16 '17 at 20:42
  • @vipertherapper C++ isn't a hard language to teach, it's actually a lot easier to be productive in than C, but it can be taught in a way that makes it way worse. C++14 and C++17 add a lot of convenience features that make teaching easier, but it's like the majority of courses are stuck with pre C++98 material. The C++ tag here gets a worrying amount of questions relating to *Borland C++*, a product that's been dead for over seventeen years. – tadman Oct 16 '17 at 20:48
  • 1
    @tadman Not to mention TurboC++ 3, which is older than the first standard iirc. :/ – Baum mit Augen Oct 16 '17 at 20:51
  • @BaummitAugen It's like a trip down memory lane, isn't it? – tadman Oct 16 '17 at 20:51
  • @tadman I completely missed that computing era, I was in elementary back then. – Baum mit Augen Oct 16 '17 at 20:53

1 Answers1

2

I'm assuming the input is always expected to be a single character. You can determine the type of character by doing a comparison of ASCII values. https://en.wikipedia.org/wiki/ASCII

#include<string>
#include <cstdio>
#include<iostream>
using namespace std;

int main() {
    char x;

    cout<<"\nEnter a character.\n";
    x=getchar();
    if(x>='A' && x <= 'Z') {cout<<"Uppercase character";}
    else if(x>='a' && x <= 'z') {cout<<"Lowercase character";}
    else if(x>='0' && x <= '9') {cout<<"Number";}
    else {cout<<"Other character";}
    return 0;

}
SPMP
  • 1,181
  • 1
  • 9
  • 24
  • Note, this assumes ASCII or some other encoding compatible with it. A popular assumption, but not necessarily correct. (EBCDIC still exists.) – cHao Oct 16 '17 at 20:37
  • was halfway done writing this as an answer when you posted it. This is how i would do it. You might need some while loops for multiple digit numbers, but this should be the start you need to get you back at your computer and fiddling with it. – victor Oct 16 '17 at 20:37
  • This looks good, but doesn't `getchar()` count as a standard library function? Excuse me if that's an ignorant question. I've never dabbled with C++. – rustyshackleford Oct 16 '17 at 20:39
  • I'm wondering if you'd run afoul of the rules for using `getchar` since that's *technically* a library function. Would using `argv` be better? This assignment might be a contradiction since there's really no other way to get input other than that. – tadman Oct 16 '17 at 20:40
  • The `<<` and `>>` stream operators are also library functions. You need a more precise statement of what functions are and aren't allowed. – Fred Larson Oct 16 '17 at 20:41
  • I think it's reasonable to use standard input and output functions. No programming 101 assingments would need the student to write replacements for those. – SPMP Oct 16 '17 at 20:41
  • 1
    @vipertherapper: It is a library function...but without using library functions for I/O, you're stuck with OS APIs, writing assembly language or working with MMIO bullshit. C++ has no I/O functionality built into the language itself; it's all implemented via libraries. – cHao Oct 16 '17 at 20:41
  • 1
    @tadman I'm not too worried. If my friend gets points knocked off for using `getchar`, then so be it. – rustyshackleford Oct 16 '17 at 20:47
  • @cHao Yep, and that'd be pretty unreasonable for my friend's intro to programming class. Thanks for the input. – rustyshackleford Oct 16 '17 at 20:47
  • 1
    `using namespace std;` is bad practice. – Baum mit Augen Oct 16 '17 at 20:49
  • I usually avoid that in header files, but should it be avoided in source files too? – SPMP Oct 16 '17 at 20:52
  • Yeah, don't do that there either. Many reasons, see e.g. https://stackoverflow.com/q/1452721/3002139 – Baum mit Augen Oct 16 '17 at 20:54