Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP. i have written the program but for some reason my input and output files a

ID: 3662723 • Letter: H

Question

HELP. i have written the program but for some reason my input and output files are not working!!

Write a program which allows the user to perform simple tasks on a calculator. A series of functions allows the user to select an operation to perform and then enter operands.

The first function displays a menu, giving the user the choice of typing in any one of the following:

+, -, *, /, or % A

X M S Q

representing the usual arithmetic operators representing the average of two numbers representing the maximum of two numbers representing the minimum of two numbers representing the square of a number

indicating the user wants to quit the program

The program reads the user's response into a variable of type char. Using a switch statement or a series of if statements, the program determines what function to call to process the user's request. For example, if the user enters +, another function asks for two integers. Then it finds the sum of the two integers. If the user enters X, a function asks for two integers and finds the larger of the two. If the user enters S, a function asks for one value and finds the square of that value. If the user enters Q, the program stops.

For each calculation performed, the program (i.e., the function) prints the user's original input and the result.

#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;

void menu();
void addition(ifstream& infile, ofstream &outfile);
void subtraction(ifstream& infile, ofstream &outfile);
void multiplication(ifstream& infile, ofstream &outfile);
void division(ifstream& infile, ofstream &outfile);
void mod(ifstream& infile, ofstream &outfile);
void average(ifstream& infile, ofstream &outfile);
void max(ifstream& infile, ofstream &outfile);
void min(ifstream& infile, ofstream &outfile);
void square(ifstream& infile, ofstream &outfile);

int main()
{
char choice;
bool not_done=true;

ifstream infile;
infile.open("calcinput.txt");

ofstream outfile("calcoutput.txt");

do
{ menu();
infile>>choice;
switch(choice)
{
case 'Q':
outfile<<"The Program has ended"<<endl;
not_done=false;
break;
case 'q':
outfile<<"The Program has ended"<<endl;
not_done=false;
break;
case '+':
addition(infile, outfile);
break;
case '-':
subtraction(infile, outfile);
break;
case '*':
multiplication(infile, outfile);
break;
case '/':
division(infile, outfile);
break;
case '%':
mod(infile, outfile);
break;
case 'A':
average(infile, outfile);
break;
case 'a':
average(infile, outfile);
break;
case 'X':
max(infile, outfile);
break;
case 'x':
max(infile, outfile);
break;
case 'M':
min(infile, outfile);
break;
case 'm':
min(infile, outfile);
break;
case 'S':
square(infile, outfile);
break;
case 's':
square(infile, outfile);
break;
default:
outfile<<"Invalid Entry."<<endl;
break;
}
}while(not_done);

infile.close();
outfile.close();

return 0;
}

void menu()
{
cout<<endl<<" Menu "<<endl;
cout<<"Select One of the Following:"<<endl;
cout<<" + -- Addition"<<endl;
cout<<" - -- Subtraction"<<endl;
cout<<" * -- Multiplication"<<endl;
cout<<" / -- Division"<<endl;
cout<<" % -- Modulus"<<endl;
cout<<" A -- Average"<<endl;
cout<<" X -- Maximum"<<endl;
cout<<" M -- Minimum"<<endl;
cout<<" S -- Square"<<endl;
cout<<" Q -- Quit"<<endl;
cout<<endl<<" Choose One Selection: ";
return;
}

void addition(ifstream& infile, ofstream &outfile)
{
int num1,num2,sum;
cout<< "Enter the augend"<<endl;
infile>>num1;
outfile<<num1<<endl;
cout<<"Enter the addend"<<endl;
infile>>num2;
outfile<<num2<<endl;
sum=num1+num2;
outfile<< "The sum is:"<<sum<<endl;
return;
}

void subtraction(ifstream& infile, ofstream &outfile)
{
int num1,num2,difference;
cout<< "Enter the subtrahend"<<endl;
infile>>num1;
outfile<<num1<<endl;
cout<<"Enter the minuend"<<endl;
infile>>num2;
outfile<<num2<<endl;
difference=num1-num2;
outfile<< "The difference is:"<<difference<<endl;
return;
}

void multiplication(ifstream& infile, ofstream &outfile)
{
int num1,num2,product;
cout<< "Enter the multiplicand"<<endl;
infile>>num1;
outfile<<num1<<endl;
cout<<"Enter the multiplier"<<endl;
infile>>num2;
outfile<<num2<<endl;
product=num1*num2;
outfile<< "The product is:"<<product<<endl;
return;
}

void division(ifstream& infile, ofstream &outfile)
{
int num1,num2,quotient;
cout<< "Enter the dividend"<<endl;
infile>>num1;
outfile<<num1<<endl;
cout<<"Enter the divisor"<<endl;
infile>>num2;
outfile<<num2<<endl;
quotient=num1/num2;
outfile<< "The quotient is:"<<quotient<<endl;
return;
}

void mod(ifstream& infile, ofstream &outfile)
{
int num1,num2,module;
cout<< "Enter a number to find the remainder"<<endl;
infile>>num1;
outfile<<num1<<endl;
cout<<" Enter a number to mod"<<endl;
infile>>num2;
module=(num1%num2);
outfile<<" The module is:"<<endl;
return;
}

void average(ifstream& infile, ofstream &outfile)
{
int num1,num2,average;
cout<< "Enter one of two number you wish to find the average of"<<endl;
infile>>num1;
outfile<<num1<<endl;
cout<<"Enter the other numeber"<<endl;
infile>>num2;
outfile<<num2<<endl;
average=(num1+num2)/2;
outfile<< "The average is:"<<average<<endl;
return;
}

void max(ifstream& infile, ofstream &outfile)
{
int num1,num2,max;
cout<< "Enter one of two numbers you wish to find the maximum of"<<endl;
infile>>num1;
outfile<<num1<<endl;
cout<<"Enter the other numeber"<<endl;
infile>>num2;
outfile<<num2<<endl;
if(num1>num2)
max=num1;
else
max=num2;
outfile<< "The maximum is:"<<max<<endl;
return;
}

void min(ifstream& infile, ofstream &outfile)
{
int num1,num2,min;
cout<< "Enter one of two numbers you wish to find the minimum of"<<endl;
infile>>num1;
outfile<<num1<<endl;
cout<<"Enter the other numeber"<<endl;
infile>>num2;
outfile<<num2<<endl;
if(num1<num2)
min=num1;
else
min=num2;
outfile<< "The minimum is:"<<min<<endl;
return;
}

void square(ifstream& infile, ofstream &outfile)
{
int num1,square;
cout<<"Enter the number you wish to square"<<endl;
infile>>num1;
outfile<<num1<<endl;
square= num1*num1;
outfile<<"The square of the number is:"<<square<<endl;
return;
}

Explanation / Answer

/**
Note: Since reading input from standard keyboard doesnot need
ifstream to read . Remove the ifstream statements from the code below
*/
/**C++ program for simple arithmeatic calculator. The program shows a menu
of options to select the opeator then asks for the operands .
Then finds the results and save it to the output text file called
calcoutput.txt and exit from the program if q or Q is entered.
*/
#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;

//modify the return type of menu as char
char menu();
void addition(ofstream &outfile);
void subtraction(ofstream &outfile);
void multiplication( ofstream &outfile);
void division(ofstream &outfile);
void mod(ofstream &outfile);
void average(ofstream &outfile);
void max(ofstream &outfile);
void min(ofstream &outfile);
void square(ofstream &outfile);

int main()
{
    char choice;
    bool not_done=true;
   //Open output text file object
    ofstream outfile("calcoutput.txt");

    do
    {
       choice =menu();      
        switch(choice)
        {
            case 'Q':
                outfile<<"The Program has ended"<<endl;
                not_done=false;
                break;
            case 'q':
                outfile<<"The Program has ended"<<endl;
                not_done=false;
                break;
            case '+':
                addition(outfile);
                break;
            case '-':
                subtraction(outfile);
                break;
            case '*':
                multiplication(outfile);
                break;
            case '/':
                division(outfile);
                break;
            case '%':
                mod( outfile);
                break;
            case 'A':
                average( outfile);
                break;
            case 'a':
                average(outfile);
                break;
            case 'X':
                max( outfile);
                break;
            case 'x':
                max(outfile);
                break;
            case 'M':
                min( outfile);
                break;
            case 'm':
                min( outfile);
                break;
            case 'S':
                square(outfile);
                break;
            case 's':
                square(outfile);
                break;
            default:
                outfile<<"Invalid Entry."<<endl;
                break;
        }

    }while(not_done);
  
    outfile.close();


   system("pause");
    return 0;
}

char menu()
{

   char ch;
    cout<<endl<<" Menu "<<endl;
    cout<<"Select One of the Following:"<<endl;
    cout<<"      + -- Addition"<<endl;
    cout<<"      - -- Subtraction"<<endl;
    cout<<"      * -- Multiplication"<<endl;
    cout<<"      / -- Division"<<endl;
    cout<<"      % -- Modulus"<<endl;
    cout<<"      A -- Average"<<endl;
    cout<<"      X -- Maximum"<<endl;
    cout<<"      M -- Minimum"<<endl;
    cout<<"      S -- Square"<<endl;
    cout<<"      Q -- Quit"<<endl;
    cout<<endl<<" Choose One Selection: ";

   //read user input of choice
   cin>>ch;

   //return user input
    return ch;

}

//Remove the ifstream object and use cin to read from keyboard
void addition(ofstream &outfile)
{
    int num1,num2,sum;
    cout<< "Enter the augend"<<endl;
    cin>>num1;
    outfile<<num1<<endl;
    cout<<"Enter the addend"<<endl;
    cin>>num2;
    outfile<<num2<<endl;
    sum=num1+num2;
    outfile<< "The sum is:"<<sum<<endl;
    return;
}
//Remove the ifstream object and use cin to read from keyboard
void subtraction(ofstream &outfile)
{
    int num1,num2,difference;
    cout<< "Enter the subtrahend"<<endl;
    cin>>num1;
    outfile<<num1<<endl;
    cout<<"Enter the minuend"<<endl;
    cin>>num2;
    outfile<<num2<<endl;
    difference=num1-num2;
    outfile<< "The difference is:"<<difference<<endl;
    return;
}
//Remove the ifstream object and use cin to read from keyboard
void multiplication(ofstream &outfile)
{
    int num1,num2,product;
    cout<< "Enter the multiplicand"<<endl;
    cin>>num1;
    outfile<<num1<<endl;
    cout<<"Enter the multiplier"<<endl;
    cin>>num2;
    outfile<<num2<<endl;
    product=num1*num2;
    outfile<< "The product is:"<<product<<endl;
    return;
}
//Remove the ifstream object and use cin to read from keyboard
void division(ofstream &outfile)
{
    int num1,num2,quotient;
    cout<< "Enter the dividend"<<endl;
    cin>>num1;
    outfile<<num1<<endl;
    cout<<"Enter the divisor"<<endl;
    cin>>num2;
    outfile<<num2<<endl;
    quotient=num1/num2;
    outfile<< "The quotient is:"<<quotient<<endl;
    return;
}
//Remove the ifstream object and use cin to read from keyboard
void mod(ofstream &outfile)
{
    int num1,num2,module;
    cout<< "Enter a number to find the remainder"<<endl;
    cin>>num1;
    outfile<<num1<<endl;
    cout<<" Enter a number to mod"<<endl;
    cin>>num2;
    module=(num1%num2);
    outfile<<" The module is:"<<endl;
    return;
}
//Remove the ifstream object and use cin to read from keyboard
void average(ofstream &outfile)
{
    int num1,num2,average;
    cout<< "Enter one of two number you wish to find the average of"<<endl;
    cin>>num1;
    outfile<<num1<<endl;
    cout<<"Enter the other numeber"<<endl;
    cin>>num2;
    outfile<<num2<<endl;
    average=(num1+num2)/2;
    outfile<< "The average is:"<<average<<endl;
    return;
}
//Remove the ifstream object and use cin to read from keyboard
void max(ofstream &outfile)
{
    int num1,num2,max;
    cout<< "Enter one of two numbers you wish to find the maximum of"<<endl;
    cin>>num1;
    outfile<<num1<<endl;
    cout<<"Enter the other numeber"<<endl;
    cin>>num2;
    outfile<<num2<<endl;
    if(num1>num2)
        max=num1;
    else
        max=num2;
    outfile<< "The maximum is:"<<max<<endl;
    return;
}
//Remove the ifstream object and use cin to read from keyboard
void min(ofstream &outfile)
{
    int num1,num2,min;
    cout<< "Enter one of two numbers you wish to find the minimum of"<<endl;
    cin>>num1;
    outfile<<num1<<endl;
    cout<<"Enter the other numeber"<<endl;
    cin>>num2;
    outfile<<num2<<endl;
    if(num1<num2)
        min=num1;
    else
        min=num2;
    outfile<< "The minimum is:"<<min<<endl;
    return;
}
//Remove the ifstream object and use cin to read from keyboard
void square(ofstream &outfile)
{
    int num1,square;
    cout<<"Enter the number you wish to square"<<endl;
    cin>>num1;
    outfile<<num1<<endl;
    square= num1*num1;
    outfile<<"The square of the number is:"<<square<<endl;
    return;
}

---------------------------------------------------------------------------------------------------------------------

Sample output:


                                 Menu
Select One of the Following:
             + -- Addition
             - -- Subtraction
             * -- Multiplication
             / -- Division
             % -- Modulus
             A -- Average
             X -- Maximum
             M -- Minimum
             S -- Square
             Q -- Quit

         Choose One Selection: +
Enter the augend
5
Enter the addend
5

                                 Menu
Select One of the Following:
             + -- Addition
             - -- Subtraction
             * -- Multiplication
             / -- Division
             % -- Modulus
             A -- Average
             X -- Maximum
             M -- Minimum
             S -- Square
             Q -- Quit

         Choose One Selection: -
Enter the subtrahend
5
Enter the minuend
5

                                 Menu
Select One of the Following:
             + -- Addition
             - -- Subtraction
             * -- Multiplication
             / -- Division
             % -- Modulus
             A -- Average
             X -- Maximum
             M -- Minimum
             S -- Square
             Q -- Quit

         Choose One Selection: *
Enter the multiplicand
5
Enter the multiplier
5

                                 Menu
Select One of the Following:
             + -- Addition
             - -- Subtraction
             * -- Multiplication
             / -- Division
             % -- Modulus
             A -- Average
             X -- Maximum
             M -- Minimum
             S -- Square
             Q -- Quit

         Choose One Selection: /
Enter the dividend
5
Enter the divisor
5

                                 Menu
Select One of the Following:
             + -- Addition
             - -- Subtraction
             * -- Multiplication
             / -- Division
             % -- Modulus
             A -- Average
             X -- Maximum
             M -- Minimum
             S -- Square
             Q -- Quit

         Choose One Selection: q

----------------------------------------------------------------------------------------

output text file caloutput.txt

5
5
The sum is:10
5
5
The difference is:0
5
5
The product is:25
5
5
The quotient is:1
The Program has ended