-1

My Question is : Write a program which takes a number as input from user between range(1-99) and converts the number into words by using multiple switch cases. If user press Enter only the program should Exit. [enter image description here][1]

enter code here
enter code here

My solution for this question is below but I didn't work properly because character variable one get one char. kindly look at & help me to find solution for this problem. enter code here`

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char a='a';

cout<<"Enter a number: ";


a=getche(); 

switch(a)
{
    case 1:     cout<<"One";    break;
    case 2:     cout<<"Two";    break;
    case 3:     cout<<"Three";  break;
    case 4:     cout<<"Four";   break;
    case 5:     cout<<"Five";   break;
    case 6:     cout<<"Six";    break;
    case 7:     cout<<"Seven";  break;
    case 8:     cout<<"Eight";  break;
    case 9:     cout<<"Nine";   break;
    case 10:    cout<<"Ten";    break;
    case 11:    cout<<"Eleven"; break;
    case 12:    cout<<"Twelve"; break;
    case 13:    cout<<"Thirteen";   break;
    case 14:    cout<<"Fourteen";   break;
    case 15:    cout<<"Fifteen";    break;
    case 16:    cout<<"Sixteen";    break;
    case 17:    cout<<"Seventeen";  break;
    case 18:    cout<<"Eighteen";   break;
    case 19:    cout<<"Nineteen";   break;
    case 20:    cout<<"Twenty";     break;
    case 21:    cout<<"Twenty One"; break;
    case 22:    cout<<"twenty two"; break;
    case 23:    cout<<"twenty three";   break;
    case 24:    cout<<"twenty four";    break;
    case 25:    cout<<"twenty five";    break;
    case 26:    cout<<"twenty six";     break;
    case 27:    cout<<"twenty seven";   break;
    case 28:    cout<<"twenty eight ";  break;
    case 29:    cout<<"twenty nine ";   break;
    case 30:    cout<<"thirty";         break;
    case 31:    cout<<"thirty one";     break;
    case 32:    cout<<"thirty two";     break;
    case 33:    cout<<"thirty three";   break;
    case 34:    cout<<"thirty four";    break;
    case 35:    cout<<"thirty five";    break;
    case 36:    cout<<"thirty six";     break;
    case 37:    cout<<"thirty seven";   break;
    case 38:    cout<<"thirty eight";   break;
    case 39:    cout<<"thirty nine";    break;
    case 40:    cout<<"forty";          break;
    case 41:    cout<<"forty one";      break;
    case 42:    cout<<"forty two";      break;
    case 43:    cout<<"forty three";    break;
    case 44:    cout<<"forty four";     break;
    case 45:    cout<<"forty five";     break;
    case 46:    cout<<"forty six";      break;
    case 47:    cout<<"forty seven";    break;
    case 48:    cout<<"forty eight";    break;
    case 49:    cout<<"fourty nine";    break;
    case 50:    cout<<"fifty";          break;
    case 51:    cout<<"fifty one";      break;
    case 52:    cout<<"fifty two";      break;
    case 53:    cout<<"fifty three";    break;
    case 54:    cout<<"fifty four";     break;
    case 55:    cout<<"fifty five";     break;
    case 56:    cout<<"fifty six";      break;
    case 57:    cout<<"fifty seven";    break;
    case 58:    cout<<"fifty eight";    break;
    case 59:    cout<<"fifty nine";     break;
    case 60:    cout<<"sixty";          break;
    case 61:    cout<<"sixty one";      break;
    case 62:    cout<<"sixty two";      break;
    case 63:    cout<<"sixty three";    break;
    case 64:    cout<<"sixty four";     break;
    case 65:    cout<<"sixty five";     break;
    case 66:    cout<<"sixty six";      break;
    case 67:    cout<<"sixty seven";    break;
    case 68:    cout<<"sixty eight";    break;
    case 69:    cout<<"sixty nine";     break;
    case 70:    cout<<"seventy";        break;
    case 71:    cout<<"seventy one";    break;
    case 72:    cout<<"seventy two";    break;
    case 73:    cout<<"seventy three";  break;
    case 74:    cout<<"seventy four";   break;
    case 75:    cout<<"seventy five";   break;
    case 76:    cout<<"seventy six";    break;
    case 77:    cout<<"seventy seven";  break;
    case 78:    cout<<"seventy eight";  break;
    case 79:    cout<<"seventy nine";   break;
    case 80:    cout<<"eighty";         break;
    case 81:    cout<<"eighty one";     break;
    case 82:    cout<<"eighty two";     break;
    case 83:    cout<<"eighty three";   break;
    case 84:    cout<<"eighty four";    break;
    case 85:    cout<<"eighty five";    break;
    case 86:    cout<<"eighty six";     break;
    case 87:    cout<<"eighty seven";   break;
    case 89:    cout<<"eighty nine";    break;
    case 90:    cout<<"ninety";         break;
    case 91:    cout<<"ninety one";     break;
    case 92:    cout<<"ninety two";     break;
    case 93:    cout<<"ninety three";   break;
    case 94:    cout<<"ninety four";    break;
    case 95:    cout<<"ninety five";    break;
    case 96:    cout<<"ninety six";     break;
    case 97:    cout<<"ninety seven";   break;
    case 98:    cout<<"ninety eight";   break;
    case 99:    cout<<"ninety nine";    break;
    case '\n': break;   
    default:
            cout<<"Try Again!";             
} 
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • With the program you show, what is the exact input you give it? What is the expected output? What is the actual output? You *do* know what [`getche`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getche-getwche) does and what it returns? – Some programmer dude May 07 '18 at 07:25
  • Using `getche()`, you will get individual characters. So, you have to write a "parser" to build up the intended numbers. I.e. make input a loop. Check `a`: `if (a >= '0' && a <= '9')`. Convert character to a digit: `int digit = a - '0';`. Build number: `number = 10 * number + digit;`. (Don't forget to initialize `number` with 0 before input loop.) The check for ENTER can be done as alternative to check for digit: `if (a == '\n' || a == '\r')`. – Scheff's Cat May 07 '18 at 07:29
  • It's my mistake I forgot to indent every case as char variable initialized like is single quotes case '1': case '2': and so on. – awaishalepota May 07 '18 at 07:29
  • Yeah, but this won't work for number >= 10... – Scheff's Cat May 07 '18 at 07:30
  • It asks the user to enter any number between range 1 to 99 and I am entering eg. Enter a number: 2 so the output will be Two and if i press 45 it should display fifty five but I didn't, and the last thing If user pressed Enter the the program should Exit. – awaishalepota May 07 '18 at 07:32
  • Well this is what you described in your question. I sketched an algorithm to solve this above... – Scheff's Cat May 07 '18 at 07:33
  • kindly share the algorithm, I am trying to solve this. – awaishalepota May 07 '18 at 07:41
  • Did you read the reference I linked to? Are you aware of that `getche` reads *one* character? What do you think you should do to read two characters? Also, do you *have* to use `getche`? Why not use the standard `scanf` function like most other people? Or `fgets` in combination with `sscanf`? – Some programmer dude May 07 '18 at 07:44
  • Yes read it, ohh no I didn't aware of that getche() only reads one character – awaishalepota May 07 '18 at 07:48
  • I don't know what should I do to read two character actually that's solution for my problem I think so, scanf function? can you tell me more about this, fgets in combination with sscanf? I don't know about these functions – awaishalepota May 07 '18 at 07:50
  • Oh wait, you're programming in C++. Then it's even simpler: `cin >> my_int_variable;`. If you don't know about that, then you should [get a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and start over from the very beginning. Or don't skip class. Or scrap whatever tutorial you're currently using. – Some programmer dude May 07 '18 at 07:52

2 Answers2

0
  1. You are using a char , and you wish to store in that variable a number which could take two characters
  2. You need to convert the input into an int before going into the switch statement
gameerik
  • 55
  • 1
  • 8
  • 1. I changed the char to int but same problem It doesn't give the same output that I want, I gives and error because of getche(); function, getch(); at the run time only gets one character whether I declare it as an char or int – awaishalepota May 07 '18 at 07:45
  • Unless your task requires getche , use cin , if it does require it , read @Scheff ‘s comment – gameerik May 07 '18 at 07:47
  • using cin>> my compiler gives an error the didn't Exit on Enter key either can you have an better idea how can I use cin>> in my case? – awaishalepota May 07 '18 at 07:52
0

instead of (it reads single character instead of a number)

char a='a';
cout<<"Enter a number: ";
a=getche(); 

you can simply read integer from stream:

int a;
cout<<"Enter a number: ";
cin>>a;
Andrei R.
  • 2,374
  • 1
  • 13
  • 27
  • int a; cout<<"Enter a number: "; cin a; If I read integer from stream so how can I make my program to exit when Enter key pressed ??? – awaishalepota May 07 '18 at 10:11