0
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>

using namespace std;

void getName ( string first1_name, string last1_name, string first2_name, string last2_name );
string getLectureSection ( );
void getLabSection ( string lab_section1, string lab_section2 );
void printIdInfo ( ostream&out, string first1_name, string last1_name, string first2_name,
                  string last2_name, string CLASS, string lecture_section,
                  string lab_section1, string lab_section2, string DUE_DATE );
void readExpression ( ifstream&, double operand1, char operatr, double operand2 );
void echoExpression ( ostream&out, double operand1, char operatr, double operand2 );
void evaluateExpression ( ostream&out, double operand1, char operatr, double operand2 );
void writeFileLocation ( );

int main()
{
   const string CLASS = "C.S.1428",
                DUE_DATE = "11/15/17";

   string first1_name,
          last1_name,
          first2_name,
          last2_name,
          lecture_section,
          lab_section1,
          lab_section2;

   double   operand1,
            operand2;

   char     operatr;

   ifstream fin;
   fin.open("prog5_002in.txt");

   if(!fin)
   {
        cout << "Program Terminated." << endl
             << "Input file failed to open!" << endl;
        return 1;
   }

   ofstream fout;
   fout.open("prog5_002out.txt"); 

   if(!fout)
   {
        cout << "Program Terminated." << endl
             << "Output file failed to open!" << endl;
        return 2;
   }

   getName ( first1_name, last1_name, first2_name, last2_name );
   lecture_section = getLectureSection ( );
   getLabSection ( lab_section1, lab_section2 );
   printIdInfo ( fout, first1_name, last1_name, first2_name, last2_name,
                 CLASS, lecture_section,
                 lab_section1, lab_section2, DUE_DATE );

   readExpression ( fin, operand1, operatr, operand2 );
   while ( operatr != '?' )
   {
       echoExpression ( fout, operand1, operatr, operand2 );
       evaluateExpression ( fout, operand1, operatr, operand2 );
       readExpression ( fin, operand1, operatr, operand2 );
   }

   cout << endl << endl;
   printIdInfo ( cout, first1_name, last1_name, first2_name, last2_name,
                 CLASS, lecture_section, lab_section1, lab_section2, DUE_DATE );
   writeFileLocation ( );

   fin.close();
   fout.close();

   system("PAUSE>NUL");

   return 0;
}


    void getName(string first1_name, string last1_name, string first2_name, string last2_name)
    {
        cout << "Enter your first name: ";
        cin >> first1_name;
        cout << endl << "Enter your last name: ";
        cin >> last1_name;
        cout << endl << endl;

        cout << "Enter your first name: ";
        cin >> first2_name;
        cout << endl << "Enter your last name: ";
        cin >> last2_name;
        cout << endl << endl;
    }

    string getLectureSection()
    {
        string lecture_section;

        cout << "Enter your three digit lecture section: ";
        cin >> lecture_section;
        cout << endl << endl;

        return lecture_section;
    }

    void getLabSection(string lab_section1, string lab_section2)
    {
        cout << "Enter your two digit lab section number: ";
        cin >> lab_section1;
        cout << endl;
        cout << "Enter your two digit lab section number: ";
        cin >> lab_section2;
    }

    void printIdInfo ( ostream&out, string first1_name, string last1_name, string first2_name,
                       string last2_name, string CLASS,string lecture_section,string lab_section1,
                       string lab_section2,string DUE_DATE )
    {
        out << first1_name << " " << last1_name << " & "
             << first2_name << " " << last2_name << endl
             << CLASS << lecture_section << endl
             << "Lab Section: " << "L" << lab_section1
             << "L" << lab_section2 << endl
             << DUE_DATE << endl << endl;
    }

    void readExpression (ifstream &fin, double operand1, char operatr, double operand2)
    {
        fin >> operand1, operatr, operand2;
    }


    void echoExpression(ostream&fout, double operand1,char operatr, double operand2)
    {
        fout << operand1 << operatr << operand2;
    }

    void evaluateExpression(ostream&fout, double operand1, char operatr, double operand2)
    {
        double expression;
        switch (operatr)
        {
            case '+' :
            expression = (operand1 + operand2);
            fout << " = " << expression;
            break;

        case '-' :
            expression = (operand1 - operand2);
            fout << " = " << expression;
            break;

        case '*' :
            expression = (operand1 * operand2);
            fout << " = " << expression;
            break;

        case '/' :
            if (operand2 == 0)
            {
                fout << endl << "Division by zero produces an "
                     << "undefined result." << endl;
            }
            else
            {
                expression = (operand1 / operand2);
                fout << " = " << setprecision(1) << fixed << expression;
            }
            break;

        default :
            fout << operand1 << " " << operatr << " " << operand2;
            fout << endl << "Encountered unknown operator.";
        }
    }

void writeFileLocation()
{
    cout << "Program results have been written to Prog5_002out.txt";
}

I'm very new to coding in general especially c++ and this is a program I've been working on for an extra credit assignment in my class. For the most part I've gotten it to run but I don't know how to display the statements from the printIdInfo function because I'm not sure how to leave it open ended to call once for fout and then again for cout. So I haven't been able to get the cout call working but it semi works with fout.

I also don't think that I'm doing anything remotely correct with my function call for readExpression, I don't seem to be correctly finding the operand1, operatr, and operand2 from the input file I have. At the moment the output file loops (indefinitely?) just displaying encountered unknown operator which I want it to do in the case of the $ symbol. But I'm really not sure at what point I've gone wrong.

This isn't really "due" but I can turn it in by Friday for bonus points. I'm stumped somewhere along and I think it's just because I rushed through it but any pointers would be appreciated. Thank you.

(Input File)
123.5 + 59.3 

198.7 / -26

125 $ 28 

89.3 * 2.5 

178.9 - 326.8

198.7 / 0 

34.0 ? 10
Thientvse
  • 1,753
  • 1
  • 14
  • 23

1 Answers1

0

Your readExpression function reads properly from fin, but does nothing with the result. You define the signature of readExpression as:

void readExpression(std::ifstream &, double, char, double);

You are doing the correct thing with the std::ifstream by passing a reference to that argument (with &). However, you are passing the last three arguments by value, which essentially means a temporary copy of each argument is being created for use within the function. You are therefore reading in from fin and putting the results into temporary variables that are obliterated when the function exits. If you want the function to modify the contents of the last three arguments (similar to how it is modifying the std::ifstream), pass them by reference. Change the signature to:

void readExpression(std::ifstream &, double &, char &, double &);

Then after the call to readExpression(fin, operand1, operatr, operand2), you should find that operand1, operatr, and operand2 contain what you expect them to contain.

See this Wikipedia article for more info about references.

P.S., Try to avoid using namespace std whenever possible.

PaSTE
  • 4,050
  • 18
  • 26