differentiate numeric and alphabetic?

I've tried searching, but maybe I'm just using the wrong terminology.

Is there a way in C++ to differentiate numeric and alphanumeric data?

my example:

1
2
3
4
5
6
7
8
int x = 0;

cin >> x;

if(x > 0 && x < 10)
    doThis();
else
    errorMsg();


if the user enters the letter "P" for example, instead of a numeric value, my program goes crazy, or just ends. is there a way to do something hypothetically like:

1
2
else if(x == "alphanumeric")
    errorMsg();


Last edited on
You could first read a character, test if it is a number and then do what you want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#inlude <iostram>
#include <ctype.h>
// in ctype.h is located the isdigit() function
using namespace std;
//...
char test;
int x;
cin.get(test);
if ( isdigit(test) )//tests wether test is == to '0', '1', '2' ...
{
      cin.putback(test);//puts test back to the stream
      cin >> x;
}
else
     errorMsg();
Last edited on
i'm having trouble implementing your suggestion. maybe if i show you my code you could help me?

(im looking into isdigit, isalnum, atoi & strtol as we speak)

(also note: this is my finished homework, you would not be helping me do my homework. but it's been bugging me all semester that i can enter an alphabetical letter and make my little program crash!)

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <iomanip>


using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::ios;
using std::setiosflags;


//function prototypes
void intro();
void selection(int &);
void getIncrease(double &);
void increasePrice(double, double[], int);
void displayPrices(double []);
void liner();
void quiter();


int main()
{ 
    //declare variables
    double rate = 0.0;
    int s    = 0;
    
    //set decimal precision
    cout << setiosflags(ios::fixed) << setprecision(2);

    //declare array
    double price[10] = {10.5, 25.5, 9.75, 6.0, 35.0, 100.4, 10.65, .56, 14.75, 4.78};
	
    //processing/call functions
    intro();
    while(s >= 0 && s <= 9)
       {
          selection(s);      
          if(s >= 0 && s <= 9)
              {
                     getIncrease(rate);
                     increasePrice(rate, price, s);
              }      //end if 
       }//end while
    cout << endl;
    liner();
    displayPrices(price);
    liner();
	
    //pause before quitting
    quiter();
    return 0;
	
}  //end of main function



//*****function definitions*****//



void intro()
{
    cout << endl;
    cout << "    Ch11AppE05.cpp - updates prices of selected items" << endl;
    cout << "    Created/revised by <hankof1983> on <10/2/2008>" << endl << endl;
    liner();
    cout << "    Enter item number you chose to modify." << endl; 
    cout << "    >> When finished, enter '0'" << endl << endl;
    liner();
}   //end of intro function    
    
    
    
void selection(int &s)
{
    cout << "    Enter item number: ";
    cin >> s;
    s = s - 1;
    while(s > 9 || s < -1)
          {
              cout << endl << "    ERROR: Please enter an item number 1-10," << endl;
              cout << "           or 0 to finish." << endl << endl;
              cout << "    Enter item number: ";
              cin >> s;
              s = s - 1;
          }   //end while       
}   //end of selection function  
  
  
  
void getIncrease(double &r)
{
      cout << "    Enter price increase %";
      cin >> r;
      r = r * 0.01;
      cout << endl;
}    //end of getIncrease function



void increasePrice(double rate, double price[], int s)
{
     price[s] = price[s] + (price[s] * rate);
     rate = 0.0;
}    //end of increasePrices function



void displayPrices(double price[])
{
     cout << "    NEW PRICES:" << endl << endl;
     for (int x = 0; x < 10; x = x + 1)
         cout << "    Item " << x + 1 << ": $" << price[x] << endl;
     //end for
     cout << endl;
}    //end of displayPrices function 



void quiter()
{
    cout << "    Press ENTER to quit...";
    cin.ignore(1);
    cin.get();
}   //end of quiter function



void liner()
{
     cout << "-------------------------------------------------------------------------" << endl << endl;
}    //end of liner function 
Last edited on
basically, what i want is to be able to determine during the selection(), if the user enters an alphabetic character, string etc, so i can tell him "NO, TRY AGAIN."

i don't need you to do it, but i need to know what to use.

ALSO feel free to critique me on any of my coding structure/use etc. I would love any criticism!
Last edited on
Look carefully at Bazzy's code...

you need to read the input directly into a char, then test to see if the char is a digit.
Your code is attempting to read directly into a int.
ahh i see. so i've successfully implemented an isdigit() check....

but how do I got about converting that char to an int value?

the cin.putback() is throwing something off for my program.

i know atoi() converts a string to an int, but is there anything similar for char?
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
void selection(int &s)
{
    char test= ' ';
    cout << "    Enter item number: ";
    cin >> test;

    while (!isdigit(test))
          {
              cout << endl << "    ERROR: Please enter an item number 1-10," << endl;
              cout << "           or 0 to finish." << endl << endl;
              cout << "    Enter item number: ";
              cin >> test;
              s = atoi (test);
              cout << endl;
          }                                   
    s = s - 1;
    while(s > 9 || s < -1)
          {
              cout << endl << "    ERROR: Please enter an item number 1-10," << endl;
              cout << "           or 0 to finish." << endl << endl;
              cout << "    Enter item number: ";
              cin >> s;
              s = s - 1;
          }   //end while       
}   //end of selection function   



with this i'm getting errors:

invalid conversion from `char' to `const char*'
&
initializing argument 1 of `int atoi(const char*)'
Chars have integer values:

1
2
3
4
char Char1, Char2;
Char1 = ' ';
Char2 = 32;
//these both are spaces (IIRC) 


I don't remember what #s 1-0 are but you could proabably find that somewhere.

Oh yeah...I just noticed you are using a single char...if they put in 10 you will only get '1'.
ungh i should've known that about char's.....

so that throws out my isdigit() check, because I have to have 11 options for user input, #1-10, and 0 to quit.

is this something i just shouldn't worry about at this level of c++? it seems like there should be a simpler method of data authenticating...

i simply want to distinguish between numeric and alphabetic data.
Last edited on
okay here's my new attempt.....

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
void selection(int &s)
{
    cout << "    Enter item number: ";
    cin >> s;
    
    while ( !( cin>> s ) ) 
    {
          cin.clear();  
          cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
          cout << endl << "    ERROR1: Please enter an item number 1-10," << endl;
          cout << "           or 0 to finish." << endl << endl;
          cout << "    Enter item number: ";
          cin >> s;
    }     //end while

    s = s - 1;                      
    while(s > 9 || s < -1)
          {
              cout << endl << "    ERROR2: Please enter an item number 1-10," << endl;
              cout << "           or 0 to finish." << endl << endl;
              cout << "    Enter item number: ";
              cin >> s;
              s = s - 1;
          }   //end while     
}   //end of selection function   


BUT! i have to enter my item numbers [int s] twice for them to be recognized, and I can't figure out why.... if i enter an alphabetic character, i get my correct error with only 1 entry. I'm of course assuming it has something to do with my while().... but what?
Last edited on
here's my current output as an example:

-------------------------------------------------------------------------

Enter item number you chose to modify.
>> When finished, enter '0'

-------------------------------------------------------------------------

Enter item number: f

ERROR1: Please enter an item number 1-10,
or 0 to finish.

Enter item number: 10
10
Enter price increase %100

Enter item number: 0
0

-------------------------------------------------------------------------

NEW PRICES:

Item 1: $10.50
Item 2: $25.50
Item 3: $9.75
Item 4: $6.00
Item 5: $35.00
Item 6: $100.40
Item 7: $10.65
Item 8: $0.56
Item 9: $14.75
Item 10: $9.56

-------------------------------------------------------------------------

Press ENTER to quit...
Last edited on
Yeah, you don't need the first cin if you have it in your while loop, since the one as the condition for your while loop will run to get the data for !cin.

And if you have an error in the first one, then the second one automatically returns an error, so you can't tell you have to input the # again.
Last edited on
ZOMG SUCCESS! i've been trying to get this right all day. here's what worked:

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
void selection(int &s)
{
    cout << "    Enter item number: ";
    
    while( !( cin >> s ) ) 
    {
          if( !( cin >> s ) )
              {
                     cin.clear();  
                     cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
              }
          cout << endl << "    ERROR1: Please enter an item number 1-10," << endl;
          cout << "           or 0 to finish." << endl << endl;
          cout << "    Enter item number: ";
    }     //end while

    s = s - 1;
                          
    while(s > 9 || s < -1)
          {
              cout << endl << "    ERROR2: Please enter an item number 1-10," << endl;
              cout << "           or 0 to finish." << endl << endl;
              cout << "    Enter item number: ";
              cin >> s;
              s = s - 1;
          }   //end while     
}   //end of selection function   
Last edited on
btw thank all of you for the help. hope no one hates me yet, because i'm sure i'll have plenty questions in the future.... MWAHAHAHHA

ill try and contribute when i can as well.

CHEERS!
just an update for anyones future reference.... i condensed the selection function to get the same result:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void selection(int &s)
{
    cout << "    Enter item number: ";
    
    while( !( cin >> s ) || s > 10 || s < 0 ) 
    {
          cin.clear();  //clear input stream
          cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
          cout << endl << "    ERROR: Please enter an item number 1-10," << endl;
          cout << "           or 0 to finish." << endl << endl;
          cout << "    Enter item number: ";
    }     //end while
    
    s = s - 1;
    
}   //end of selection function   
Topic archived. No new replies allowed.