In: Computer Science
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
#include <iostream>
#include<string.h>
#include <bits/stdc++.h>
using namespace std;
//driver program
int main()
{
string no;
int i;
//read the number
cout<<"Enter the Number ";
cin>>no;
//validate the number
for(i=0;no[i]!='\0';i++)
{
if(no[i]>='0' && no[i]<='9')
continue;
else
{
cout<<endl<<"Invalid
Number";
exit(0);
}
}
//display in words
for(i=0;no[i]!='\0';i++)
{
switch(no[i])
{
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 '0':
cout<<"Zero ";
break;
}
}
}
OUTPUT