Help on overloading certain variable types

I'm writing a program where i take in an unsigned long long variable but I also want to give the user the option to quit by inputting "q".

For example, here's what my user prompt looks like.

Please enter in a number less than 20 digits long or enter a 'q' to quit:

I played around with isalpha() and isdigit() but i couldn't get them to work for me.

How would i check the user input to verify if a number or a char was input?
Last edited on
Read the user's input in a string. Then see if the input is letter q. If it is, then just quit. If it is not, try to parse the input as an unsigned long long integer. How? The most common C++ way is to use a stringstream (or wstringstream if you are using Unicode, which you should).
This was an assignment for my class but the due date for it has already passed. I'm still curious how
to finish it up all the way though.

I have to read the user data into an unsigned long long, not a string. Somehow I have to compare
the value in the unsigned long long variable to each of the variable types listed and see if the input
value will cause them to overflow or not. If it does I need to output a "T" and if not a "F". Right now
I have them hard-coded in because I'm not sure how to go about writing the code to see if they
overflow or not.

I'm still running into problems casting certain values into char and unsigned char so I just cast them
to int and left them alone for now.

Here's my code
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
   unsigned long long number;
   
   cout << "Enter a number (q to quit): ";
   cin >> number;
   
   if (cin.fail())
      return 0;
   
   do
   {
      cout << "\nOriginal input value: " << number << endl << endl;
      
      cout << "   Data Type       Bytes      Value (Dec)           Value (Hex)       Overflow" << endl;
      cout << "------------------ -----  --------------------  --------------------  --------" << endl;                                                                                                                                                                     /*fix*/
      cout << "bool"               << setw(18) << sizeof(bool)                                           << setw(24) << dec << static_cast<bool>              (number)                    << setw(22) << hex        << static_cast<bool>              (number) << setw(9) << "N/A" << endl;
      cout << "char"               << setw(18) << sizeof(char)                                           << setw(24) << dec << static_cast<int> /*fix*/       (number)                    << setw(22) << hex        << static_cast<int> /*fix*/       (number) << setw(7) << "T"   << endl;   
      cout << "unsigned char"      << setw(9)  << sizeof(unsigned char)                                  << setw(24) << dec << static_cast<int> /*fix*/       (number)                    << setw(22) << hex        << static_cast<int> /*fix*/       (number) << setw(7) << "T"   << endl;
      cout << "short"              << setw(17) << sizeof(short)                                          << setw(24) << dec << static_cast<short>             (number)                    << setw(22) << hex        << static_cast<short>             (number) << setw(7) << "T"   << endl;
      cout << "unsigned short"     << setw(8)  << sizeof(unsigned short)                                 << setw(24) << dec << static_cast<unsigned short>    (number)                    << setw(22) << hex        << static_cast<unsigned short>    (number) << setw(7) << "F"   << endl;
      cout << "int"                << setw(19) << sizeof(int)                                            << setw(24) << dec << static_cast<int>               (number)                    << setw(22) << hex        << static_cast<int>               (number) << setw(7) << "F"   << endl;
      cout << "unsigned int"       << setw(10) << sizeof(unsigned int)                                   << setw(24) << dec << static_cast<unsigned int>      (number)                    << setw(22) << hex        << static_cast<unsigned int>      (number) << setw(7) << "F"   << endl;
      cout << "long"               << setw(18) << sizeof(long)                                           << setw(24) << dec << static_cast<long>              (number)                    << setw(22) << hex        << static_cast<long>              (number) << setw(7) << "F"   << endl;
      cout << "unsigned long"      << setw(9)  << sizeof(unsigned long)                                  << setw(24) << dec << static_cast<unsigned long>     (number)                    << setw(22) << hex        << static_cast<unsigned long>     (number) << setw(7) << "F"   << endl;
      cout << "long long"          << setw(13) << sizeof(long long)                                      << setw(24) << dec << static_cast<long long>         (number)                    << setw(22) << hex        << static_cast<long long>         (number) << setw(7) << "F"   << endl;
      cout << "unsigned long long" << setw(4)  << sizeof(unsigned long long)                             << setw(24) << dec << static_cast<unsigned long long>(number)                    << setw(22) << hex        << static_cast<unsigned long long>(number) << setw(7) << "F"   << endl;
      cout << "float"              << setw(17) << sizeof(float)              << fixed << setprecision(0) << setw(24) << dec << static_cast<float>             (number) << setprecision(5) << setw(22) << scientific << static_cast<float>             (number) << setw(7) << "F"   << endl;
      cout << "double"             << setw(16) << sizeof(double)             << fixed << setprecision(0) << setw(24) << dec << static_cast<double>            (number) << setprecision(5) << setw(22) << scientific << static_cast<double>            (number) << setw(7) << "F"   << endl;
      cout << "long double"        << setw(11) << sizeof(long double)        << fixed << setprecision(0) << setw(24) << dec << static_cast<long double>       (number) << setprecision(5) << setw(22) << scientific << static_cast<long double>       (number) << setw(7) << "F"   << endl;
                                                                                                                                                                                                                                                                             /*fix*/
      cout << "\nEnter a number (q to quit): ";
      cin >> number;
   }
   while (!cin.fail()); 
   
   return 0;
}
Topic archived. No new replies allowed.