Error using isdigit

I wrote a program to write the multiplication table of the given factors. I tried using isdigit to write an error message when the user enters a char instead of a digit. It worked. Problem is is it gives an error message when I enter a single digit no. for the primary factor. Please help me solve this logical error.

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
#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
	int i,j,factor1;
	char yn,y,n;
	int x,z;
	do            
	{
		system("cls");
		cout<<"Enter Primary Factor";
		cin>>factor1;
		x=factor1;
		if(isdigit(x)==0)   //Error message. Logical error is 
                                          // probably here
		{
			cout<<"Error. Please enter a digit";
			exit(0);	
		} 
		else
		cout<<"Enter the Secondary Factor limit:";
		cin>>j;
		for(int i=0;i<=j;i++)
		{
			cout<<factor1<<"x"<<i<<"="<<factor1*i<<endl;
		}
		system("pause");
		cout<<"Enter new table(y/n)?";
		cin>>yn;
	}while(yn!='n');
return 0;
}
Last edited on
Hello delloskill,

Welcome to the forum.

Line 14 is redundant and not of any use.

"isdigit()" is used on a "char" or a single "char" of a string. You are trying to use "isdigit" on an "int" and it is not working.

This would work better for what you want:
1
2
3
4
5
6
7
8
9
10
std::cin>>factor1;
while(!std::cin)
{
    std::cout  <<  "\n An error message of your choice" << std::endl;

    std::cin.clear();  // <--- Clears the state bits of cin.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    
    std::cout<<"Enter Primary Factor";
    std::cin >> factor1;

This works best when entering into a numeric variable. If anything other than a number is entered for "factor1" the "cin" stream will fail and the while loop is entered. After the error message you need to reset the state bits on the stream and the ignore statement will clear the input buffer before you enter something new. This will stay in the while loop until a good number is entered into "factor1".

Hope that helps,

Andy
Wow...
Thanks for the help, though I am still new to C++, I'll try the above fragment(it looks terrifying).

Also, clear() doesn't work in my compiler(Dev C++).

Can you suggest an alternative?
Can you post your edited code and verbatim error message? cin.clear() is not a new concept as far as compilers go, although Dev C++ is known to have problems...
Knowing how to interpret and fix error messages is an important skill in C++.

You also have poor indentation.
1
2
3
		else
		cout<<"Enter the Secondary Factor limit:";
		cin>>j;

This else statement is only being applied to the cout line. I'm not sure if that's what you want. Probably not.

isdigit is not doing what you think it's doing. isdigit works on a single character, say, '2' (note: different than the number 2), and returns true if the character is a digit character ('0', '1', '2' ... '9').
isdigit('a') --> returns false
isdigit('3') --> returns true

Andy's code should be fine, but if you still can't get it to work, this should work almost as well.
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
#include<iostream>
#include<ctype.h>

using namespace std;
int main()
{
	char yn;
	do      
	{
		int factor1;
		int factor2;
		
		cout << "Enter Primary Factor: ";
		if (!(cin >> factor1))
		{
			cout << "Error. Please enter a digit";
			return 1;	
		}

		cout << "Enter the Secondary Factor limit: ";
		if (!(cin >> factor2))
		{
			cout << "Error. Please enter a digit";
			return 1;	
		}
		
		for (int i = 0; i <= factor2; i++)
		{
			cout << factor1<< "x" << i << "=" << factor1*i << endl;
		}

		cout << "\nEnter new table(y/n)?";
		cin >> yn;
		
	} while (yn != 'n');
	
	return 0;
}
Last edited on
Hey, it works. Thanks.
Hello delloskill,

I agree with Ganado "DEV C++" is not the best compiler or IDE to use. I do not know if you compile you programs from a command line or through an IDE, but something you might check or try is to add this "-std=C++11" to the command line arguments.

Thanks for the help, though I am still new to C++, I'll try the above fragment(it looks terrifying).

Actually if is not that bad. The "std::" may not be something you are use to seeing, but not only should you get sue to seeing it, you should also get use to using it while it is easy. You may not understand everything in the ignore statement, so join the club, I do not understand everything well, but I do know how to use it and that is all that counts. Another way to write the ignore statement is std::cin.ignore(100000, '\n');. What is on the lhs of the comma is just a fancy way of making a very large number, in this case it is the maximum number of characters that the input buffer can hold. When you understand how ignore works it is not that scary.

One of the nice things about C++ is that you do not always have to know or understand how something works, but how to use it. Understanding will come later as you learn or ask questions.

Hope that helps,

Andy
Topic archived. No new replies allowed.