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

C++ HELP /* Your must provide 4 menu options Press 1 for author name and id Pres

ID: 3768051 • Letter: C

Question

C++ HELP

/*
Your must provide 4 menu options
Press 1 for author name and id
Press 2 to read from file
Press 3 to write to file
Press 4 to Exit

When the user selects option 1 : print your name and student id
When the user selects option 2 : ask the user to input a filename. Then open this file and read in the data (described below)
When the user selects option 3 : ask the user to input a filename. Then open this file and write the data to the file (described below)
When the user selects option 4 : Exit the program.

After a selection is made prompt the user for another selection. Repeat this process until option 4 is chosen.

input file format:
----------
x
y z
----------
where x is the number of data items present in the file
y is a fraction numerator
z is a fraction denominator.

output file format:
----------
y / z = k
----------
Where y is the numerator of a fraction from the input file,
z is the denominator of a fraction from the input file,
k is the numeric value of the fraction (.5 for 1/2, .75 for 3/4 and so on)

Each fraction from the input file should be printed to the output file when prompted.
Also your program should never crash (assuming the user doesn't type a non number into the menu selection)
so check that a file can be read before reading

Your program should be able to handle any number of fractions, and should not waste space reserving memory
for fractions that won't be used. Also avoid memory leaks.
*/

Explanation / Answer

Here is the code for you. If you need any refinements, just get back to me.

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

class Fractions
{
int numerator;
int denominator;
public:
   Fractions()
   {
   numerator = 1;
   denominator = 1;
   }
   void setNumerator(int num)
   {
   numerator = num;
   }
   void setDenominator(int den)
   {
   denominator = den;
   }
   int getNumerator()
   {
   return numerator;
   }
   int getDenominator()
   {
   return denominator;
   }
   double getRealValue()
   {
   return (double)numerator/denominator;
   }
};
void printAuthNameAndID()
{
cout<<"Name: Mark Aubrey.   ID: 123456"<<endl;
}
void writeToFile(Fractions f[], int n)
{
string fileName;
cout<<"Enter the name of the file to write to: ";
cin>>fileName;
ofstream output;
output.open(fileName);
for(int i = 0; i < n; i++)
output<<f[i].getNumerator()<<" / "<<f[i].getDenominator()<<" = "<<f[i].getRealValue()<<endl;
}

int readFromFile(Fractions f[])
{
int n, num, den;
ifstream input;
string fileName;
cout<<"Enter the name of the file to read from: ";
cin>>fileName;
input.open(fileName);
input>>n;
for(int i = 0; i < n; i++)
{
input>>num>>den;
f[i].setNumerator(num);
f[i].setDenominator(den);
}
return n;
}
int main()
{
int choice, num;
Fractions f[20];
while(true)
{
cout<<"Press 1 for author name and id"<<endl;
cout<<"Press 2 to read from file"<<endl;
cout<<"Press 3 to write to file"<<endl;
cout<<"Press 4 to Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:   printAuthNameAndID(); break;
case 2:  
           //f = new Fractions[num];
           num = readFromFile(f);      
           break;
case 3:   writeToFile(f, num);
       break;
case 4:   return 0;
default:   cout<<"Invalid menu option."<<endl;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote