The Requirement (What you need to do)
You are asked to write a program that takes as input a dollar amount, and then displays the dollar amount in English (similar to how you would write the amount in a check).
Use case (Scenario)
$PrintDollar Enter the dollar amount:$23.45 It's twenty three and 45/100 Try again(y/n):n Bye!
Error handling: You are required to handle the following error inputs. If the input is any of the following case, your program should display appropriate error message, and ask the user to try again.
Note on handling error in input operation
bool wrongInput;
do {
//attempt to read an int, a char, and another int
cin >>dollar >>ch >> cents;
if (cin.fail()) //in case the abvoe fails, e.g., ten dollor five cents...
{
cout <<"Wrong input types. Try again:\n";
cin.clear(); //clear the error flags in cin
cin.ignore (2048,'\n'); //ignore everthing in the input buffer, up to 2048 char,
//up to the newline char =< ignore the rest of the line
wrongInput = true;
}
else
wrongInput = false;
} while (wrongInput); Code:
CODE:
#include<bits/stdc++.h>
using namespace std;
const string space = "";
const string single_digit[] = { space, "One ", "Two ", "Three ", "Four ", "Five ",
"Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ",
"Twelve ", "Thirteen ", "Fourteen ", "Fifteen ",
"Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " };
const string ten[] = { space, space, "Twenty ", "Thirty ", "Forty ", "Fifty ",
"Sixty ", "Seventy ", "Eighty ", "Ninety " };
string convert2word(int num, string s)
{
// if n is zero
if (num == 0) {
return space;
}
// split n if it is more than 19
if (num > 19) {
return ten[num / 10] + single_digit[num % 10] + s;
}
else {
return single_digit[num] + s;
}
}
string numberToWords(unsigned long long int num)
{
// string to store word representation of given number
string result;
// this handles digits at ones & tens place
result = convert2word((num % 100), "");
if (num > 100 && num % 100) {
result = result;
}
// this handles digit at hundreds place
result = convert2word(((num / 100) % 10), "Hundred ") + result;
// this handles digits at thousands & tens thousands place
result = convert2word(((num / 1000) % 100), "Thousand ") + result;
return result;
}
int countDecimal(double x)
{
stringstream ss;
ss << abs(x-(long long int)x);
string s;
ss >> s;
return s.length()-2;
}
int main(int argc,char* argv[]){
double dollar_amount;
char response;
char choice;
bool run = true;
int a;
while(run){
mainmenu:cout<<"PrintDollar"<<"\n";
cout<<"Enter the dollar amount"<<"\n";
while(!(cin>>dollar_amount)){
cin.clear();
cin.ignore(123,'\n');
cout<<"Error !! Please Enter the Valid Amount\n";
goto mainmenu;
}
cout <<"\nIt's "<<numberToWords(abs(dollar_amount));
double dollar_amount1=abs(dollar_amount);
double dollar_amount2 = dollar_amount1 - int(dollar_amount1);
if(countDecimal(dollar_amount)==1){
cout<<"and "<<dollar_amount2*10<<"/10";
}else{
cout<<"and "<<dollar_amount2*100<<"/100";
}
do{
cout<<"\nTry again(y/n)"<<endl;
cin >> choice;
choice = tolower(choice);
}while (choice != 'n' && choice != 'y');
if(choice =='n'){
run = false;
}
}
cout<<"Bye!\n";
return 0;
}
Output Screenshot:
The Requirement (What you need to do) You are asked to write a program that takes...
#include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...
Professor Dolittle has asked some computer science students to write a program that will help him calculate his final grades. Professor Dolittle gives two midterms and a final exam. Each of these is worth 100 points. In addition, he gives a number of homework assignments during the semester. Each homework assignment is worth 100 points. At the end of the semester, Professor Dolittle wants to calculate the median score on the homework assignments for the semester. He believes that the...
This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...
Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...
Write a C++ program that uses a structure to store the following inventory information in a file: ⦁ Item description, Quantity on hand, Wholesale cost, Retail cost, and Date added to inventory. ⦁ Use a char array for item description and date. ⦁ The program should have a menu that allows the user to perform the following tasks: i. Add a new record at the end of the file. ii. Display any record in the file. iii. Change any record...
C++ language I am having some trouble with user validation, can someone look at my code and tell me what I am doing wrong: char firstInital, lastInitial; int userAge; std::cout << "Program 1-2: Get user initials and age in days\n "; std::cout << "-------------------------------------------------------------------------\n"; std::cout << "Please enter the first letter of your first name: \n " << flush; std::cin >> firstInital; std::cout << "Please enter the first letter of your last name: \n...
C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record { int age; string name; }; int check(record r[], int n, string nm) { for (int i = 0; i < n;...
Based on this program modify the source code so that it will able to save the student record into a database txt and able to display and modify from a database txt as well. you will also need to able to modify the code so it will accept name such as "john kenny " and the progrom should also ask for the student id number. #include<iostream> #include<stdlib.h> using namespace std; struct st { int roll; char name[50]; char grade; struct...
I am working on this switch program everything seems to be ok but the compiler is giving me an error on the final closing brace and wanted to know where I am making an error #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int choice; char repeat; double MidTerm = 0; double FinalExam = 0; double Quiz1 = 0; double Quiz2 = 0; double MidTermGrade = 1, FinalExamGrade = 2, Quiz1Grade = 3, Quiz2Grade = 4,...
LW: Class Constructors Objectives Write a default constructor for a class. Note that this constructor is used to build the object anytime an argument is not provided when it is defined. Write a parameterized constructor that takes arguments that are used to initialize the class’s member variables Labwork Download the code associated with this lab: ColorConstructor.zip Main.cpp Color.h Color.cpp Compile and run the code. The first line of output of the program should display nonsensical integers for the values of...