Question

Hi I'm having trouble here with my code which tells in words what digit is enterd and the code is
//C++ program to recognize integer value (0-9) and print out in words
#include <iostream>
#include <stdlib.h> // header file for exit(0)
using namespace std;
/////////////////////////////////////////////////////////////////////
void recognize_digit( int ) ; // function for recognizing digit
int main()
{
int input ;
// take input from user
cout<<"Enter a digit from 0 to 9 "; cin>>input;

if ( input >= 0 && input <= 9)
recognize_digit( input ); // pass input to function
else if ( input >= 'a' && input<= 'z' )
{
cout<<"You have\'nt entered the right digit";
exit(0);
}
else
{
cout<<"You have\'nt entered the right digit";
exit(0);
}
return 0;
}
void recognize_digit( int n )
{
switch ( n )
{
case 0 :
cout<<"Digit entered is \"zero\"";
break ;
case 1 :
cout<<"Digit entered is \"one\"";
break ;
case 2 :
cout<<"Digit entered is \"two\"";
break ;
case 3 :
cout<<"Digit entered is \"three\"";
break ;
case 4 :
cout<<"Digit entered is \"four\"";
break ;
case 5 :
cout<<"Digit entered is \"five\"";
break ;
case 6 :
cout<<"Digit entered is \"six\"";
break ;
case 7 :
cout<<"Digit entered is \"seven\"";
break ;
case 8 :
cout<<"Digit entered is \"eight\"";
break ;
case 9 :
cout<<"Digit entered is \"nine\"";
break ;
}
}
The problem is that the code works fine if i enter a digit or try to enter two or more digits but when I enter a character e.g 'a' then it shows the "Digit entered is zero ". How can i fix it?
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
#include <iostream>
#include <limits>
#include <string>
using namespace std;


//======================================================================


template <typename T> T getValue( string prompt, T minVal = numeric_limits<T>::min, T maxVal = numeric_limits<T>::max )
{
   T value;

   cout << prompt;
   cin >> value;

   if ( !cin )                                                             // Unable to accept input
   {
       cin.clear();
       cin.ignore( 1000, '\n' );                                           // Tidy up and empty the stream
       return getValue<T>( "Invalid entry; try again: ", minVal, maxVal ); // Recursive call
   }

   if ( value < minVal || value > maxVal )                                 // Out of range
   {
       return getValue<T>( "Out of range; try again: ", minVal, maxVal );  // Recursive call
   }

   return value;                                                           // Good value
}


//======================================================================


int main()
{
   string words[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

   int n = getValue<int>( "Input a digit: ", 0, 9 );
   cout << "You entered ... " << words[n] << '\n';
}


//====================================================================== 

Input a digit: hello
Invalid entry; try again: -9
Out of range; try again: 100 
Out of range; try again: a
Invalid entry; try again: 5
You entered ... five
thanks for help "lastchance" . It would be more helpful if you please point out the deficiencies of my code
mani shah wrote:
It would be more helpful if you please point out the deficiencies of my code


- It's in the wrong section of the Forum

- It's not in code tags and hence:
- it can't be run with the online compiler so we are forced to download it;
- it doesn't show indenting so it's difficult to read.

- It includes an old-style header #include <stdlib.h>

- It's excessively verbose:
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
switch ( n )
{
case 0 :
cout<<"Digit entered is \"zero\"";
break ;
case 1 :
cout<<"Digit entered is \"one\"";
break ;
case 2 :
cout<<"Digit entered is \"two\"";
break ;
case 3 :
cout<<"Digit entered is \"three\"";
break ;
case 4 :
cout<<"Digit entered is \"four\"";
break ;
case 5 :
cout<<"Digit entered is \"five\"";
break ;
case 6 :
cout<<"Digit entered is \"six\"";
break ;
case 7 :
cout<<"Digit entered is \"seven\"";
break ;
case 8 :
cout<<"Digit entered is \"eight\"";
break ;
case 9 :
cout<<"Digit entered is \"nine\"";
break ;
}


- You declared input to be an int and then ...
1
2
cin>>input;
if ( input >= 0 && input <= 9)


So what do you think happens if you input something other than an int?
Do you think the stream (cin) will be in a valid state?
Do you think input will have anything sensible in? Almost certainly undefined ... which leaves the compiler free to do anything it likes, possibly leaving input as some unset value, which may or may not be 0.

You could look at my "unhelpful" code to see what could be done about invalid input ... or ignore it. The choice is yours.

Last edited on
Your "unhelpful" code is very precious for me as it has some new things that I need to learn.I hope you and others like you on this platform will help me out on other problems.
"- It's in the wrong section of the Forum

- It's not in code tags and hence:
- it can't be run with the online compiler so we are forced to download it; "
The reason behind these is that I'm totally new to this regarding forum ,code tags etc
Thanks!
Well, the description of this section of the forum, The Lounge, is:

Discussions about this website, or other topics not related to C++ programming

so I would assume that one is obvious. As for code tags:

http://www.cplusplus.com/articles/z13hAqkS/

should explain everything.
Last edited on
Topic archived. No new replies allowed.