Language is C++ 2) Numbers Write a program that: a. Asks the user for an input i
ID: 3760739 • Letter: L
Question
Language is C++
2) Numbers Write a program that: a. Asks the user for an input in the main. The input can be anything, alphanumeric and symbols, can included spaces, and can be lowercase and uppercase. b. If the user inputs neither ALL alphabetic nor ALL numeric, display error and exit. c. If the user inputs ALL alphabetic, check if each word correspond to a number. If all corresponded, display the number in Arabic numeral. If not, display is not a number and exit. d. If the user inputs ALL numeric, display the number in words. Note: ''FiveTwoSixEight'' ''5268''. Don?t worn about the -teen, -ty, hundred, etc. Hint: Do the numeric to words first.Explanation / Answer
Answer:
(a),(b),(c)
#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string, int> reference;
string ones[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
void preserveOne(){
for(int i = 0; i < 11; i++){
ref[ones[i]] = i;
}
}
int main(){
//set up
preserveOne();
string test;
cout<<"Enter The Word : ";
cin>>test;
string buffer;
for(int i = 0; i < test.length(); i++){
buffer.push_back(test.at(i));
map<string, int>::iterator it = ref.find(buffer);
if(it != ref.end()){
cout << (*it).second;
buffer = "";
}
}
cout<<"Error !. Please Enter Valid Input Alphabetical Word Of Number Without Spaces In Between ";
cout << endl;
system("pause");
return 0;
}
d)
#include<iostream>
using namespace std;
int main(){
int n,i=0,j,digit;
char * word[1000];
cout<<"Enter any integer: ";
cin>>n;
while(n){
digit = n %10;
number = n /10;
switch(digit){
case 0: word[i++] = "zero"; break;
case 1: word[i++] = "one"; break;
case 2: word[i++] = "two"; break;
case 3: word[i++] = "three"; break;
case 4: word[i++] = "four"; break;
case 5: word[i++] = "five"; break;
case 6: word[i++] = "six"; break;
case 7: word[i++] = "seven"; break;
case 8: word[i++] = "eight"; break;
case 9: word[i++] = "nine"; break;
}
}
for(j=i-1;j>=0;j--){
cout<<word[j];
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.