Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division by zero and invalid input.
Your program should print Denominator must be nonzero and reprompt for a valid denominator when 0 is entered for a denominator. Please specify what should go in the divisionByZero.h file and the changes made to main.cpp Please provide output.
Thank you in advance!
main.cpp so far
#include <iostream>
using namespace std;
void addFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac);
void subtractFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac);
void multiplyFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac);
void divideFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac);
void menu();
void getFraction(int choice,int fractNo, int& numerator, int& denominator);
void showResult(char operation, int num1, int num2, int den1, int den2,
int numfrac, int denfrac);
int main()
{
int numerator1, numerator2, denominator1, denominator2;
int resultnum, resultdeno;
int choice;
menu();
cin >> choice;
while (choice != 9)
{
cout << "For fraction 1" << endl;
getFraction(choice, 1, numerator1, denominator1);
cout << endl;
cout << "For fraction 2" << endl;
getFraction(choice, 2, numerator2, denominator2);
cout << endl;
switch (choice)
{
case 1:
addFractions(numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
showResult('+', numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
break;
case 2:
subtractFractions(numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
showResult('-', numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
break;
case 3:
multiplyFractions(numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
showResult('*', numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
break;
case 4:
divideFractions(numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
showResult('/', numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
break;
default:
cout << "Invalid selection." << endl;
}
menu();
cin >> choice;
}
return 0;
}
void addFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac)
{
numfrac = num1 * den2 + num2 * den1;
denfrac = den1 * den2;
}
void subtractFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac)
{
numfrac = num1 * den2 - num2 * den1;
denfrac = den1 * den2;
}
void multiplyFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac)
{
numfrac = num1 * num2;
denfrac = den1 * den2;
}
void divideFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac)
{
numfrac = num1 * den2;
denfrac = num2 * den1;
}
void menu()
{
cout << "This program performs operations on fraction. Enter"
<< endl;
cout << "1 : To add fraction" << endl;
cout << "2 : To subtract fraction" << endl;
cout << "3 : To multiply fraction" << endl;
cout << "4 : To divide fraction" << endl;
cout << "9 : To exit the program" << endl;
}
void getFraction(int choice, int fractNo, int& numerator, int& denominator)
{
cout << "Enter the numerator: ";
cin >> numerator;
cout << endl;
if (choice == 4 && fractNo == 2 && numerator == 0)
{
cout << "To divide, the second fraction must be nonzero."
<< endl;
while (numerator == 0)
{
cout << "Enter a nonzero number for the numerator: ";
cin >> numerator;
cout << endl;
}
}
cout << "Enter the denominator: ";
cin >> denominator;
cout << endl;
}
void showResult(char operation, int num1, int num2, int den1, int den2,
int numfrac, int denfrac)
{
cout << num1 << "/" << den1;
switch (operation)
{
case '+':
cout << " + ";
break;
case '-':
cout << " - ";
break;
case '*':
cout << " * ";
break;
case '/':
cout << " / ";
break;
default:
cout << "Invalid operation." << endl;
}
cout << num2 << "/" << den2 << " = " << numfrac << "/"
<< denfrac << endl;
}
#include <iostream>
using namespace std;
void addFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac);
void subtractFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac);
void multiplyFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac);
void divideFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac);
void menu();
void getFraction(int choice,int fractNo, int& numerator, int& denominator);
void showResult(char operation, int num1, int num2, int den1, int den2,
int numfrac, int denfrac);
int main()
{
int numerator1, numerator2, denominator1, denominator2;
int resultnum, resultdeno;
int choice;
menu();
cin >> choice;
while (choice != 9)
{
cout << "For fraction 1" << endl;
getFraction(choice, 1, numerator1, denominator1);
cout << endl;
cout << "For fraction 2" << endl;
getFraction(choice, 2, numerator2, denominator2);
cout << endl;
switch (choice)
{
case 1:
addFractions(numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
showResult('+', numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
break;
case 2:
subtractFractions(numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
showResult('-', numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
break;
case 3:
multiplyFractions(numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
showResult('*', numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
break;
case 4:
divideFractions(numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
showResult('/', numerator1, numerator2,
denominator1, denominator2,
resultnum, resultdeno);
break;
default:
cout << "Invalid selection." << endl;
}
menu();
cin >> choice;
}
return 0;
}
void addFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac)
{
numfrac = num1 * den2 + num2 * den1;
denfrac = den1 * den2;
}
void subtractFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac)
{
numfrac = num1 * den2 - num2 * den1;
denfrac = den1 * den2;
}
void multiplyFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac)
{
numfrac = num1 * num2;
denfrac = den1 * den2;
}
void divideFractions(int num1, int num2, int den1, int den2,
int& numfrac, int& denfrac)
{
numfrac = num1 * den2;
denfrac = num2 * den1;
}
void menu()
{
cout << "This program performs operations on fraction. Enter"
<< endl;
cout << "1 : To add fraction" << endl;
cout << "2 : To subtract fraction" << endl;
cout << "3 : To multiply fraction" << endl;
cout << "4 : To divide fraction" << endl;
cout << "9 : To exit the program" << endl;
}
void getFraction(int choice, int fractNo, int& numerator, int& denominator)
{
cout << "Enter the numerator: ";
cin >> numerator;
cout << endl;
if (choice == 4 && fractNo == 2 && numerator == 0)
{
cout << "To divide, the second fraction must be nonzero."
<< endl;
while (numerator == 0)
{
cout << "Enter a nonzero number for the numerator: ";
cin >> numerator;
cout << endl;
}
}
cout << "Enter the denominator: ";
cin >> denominator;
// if denominator is 0
while (denominator == 0)
{
cout << "Denominator must be nonzero\n";
cout << "Enter a nonzero number for the denominator: ";
cin >> denominator;
cout << endl;
}
cout << endl;
}
void showResult(char operation, int num1, int num2, int den1, int den2,
int numfrac, int denfrac)
{
cout << num1 << "/" << den1;
switch (operation)
{
case '+':
cout << " + ";
break;
case '-':
cout << " - ";
break;
case '*':
cout << " * ";
break;
case '/':
cout << " / ";
break;
default:
cout << "Invalid operation." << endl;
}
cout << num2 << "/" << den2 << " = " << numfrac << "/"
<< denfrac << endl;
}
Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division...
SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){ int num1,num2; cout << "Enter two numbers "<< endl; cout << "First :"; cin >> num1; cout << "Second :"; cin >>num2; int result=num1+num2; cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result; ...
#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 <<...
I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...
This question is about C++! Honestly, I'm not sure what I am doing right or wrong. Using just functions, nothing else, I am trying to create a program that uses nothing but functions to take in two fractions, add these fractions, divide these fractions, and then display the simplified fraction all from within a menu. I would very much appreciate any leads, aid, or description on what I've done right and wrong. Thanks! #include <iostream> using namespace std; //Description: Function...
C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...
c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...
C++ Object Oriented assignment Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given. INSTRUCTIONS Create a fraction class and add your Name to the name fraction and use this...
Hello i am having a bit of trouble with a verified exit for my program. For some reason when trying to exit it loops to the team selection function? C++ Visual Studio 2017 #include "cPlayer.h" char chChoice1 = ' '; char chChoice3 = ' '; cPlayer::cPlayer() { } cPlayer::~cPlayer() { } void cPlayer::fMenu() { char chChoice3 = ' '; do { cout << "\n\t--Menu--" << endl; cout << "1) Enter Player Name" <<...
Need to write a program in Visual Studio CLR CONSOL APPLICATION for date of birth , addition , substraction . After perfoming additon, there should be an statement " do you wish to continue ? Press Yes to continue or E to exit. Once YES is pressed , then substraction followed by date of birth. If exit is presses then the screen should get closed. i have written a program , so please update that #include "stdafx.h" # include <iostream>...