Question

NEED HELP BEING SOLVED IN C++! USE ORIGINAL CODE TO ADD TO AS WELL!

Copy and modify your program from Lab 5a_z to read input from file inputPart5b.txt and write output to a file results Part5b.

#include<iostream>
using namespace std;
int main(){
  
for (int i = 0; i < 7; i++)
{
int x,y;
char opr;
cin>>x>>y>>opr;
  
switch(opr)
{
case '+':
cout<<x<<" + "<<y<<" = "<<x+y;
break;
case '-':
cout<<x<<" - "<<y<<" = "<<x-y;
break;
case '*':
cout<<x<<" * "<<y<<" = "<<x*y;
break;
case '/':
if(y == 0)
cout<<x<<" / "<<y<<" = "<<"ERROR";
else cout<<x<<" / "<<y<<" = "<<x/y<<"R"<<x%y;
break;
default : cout<<x<<" "<<opr<<" "<<y<<" = ILLEGAL";
}
cout<<"\n";
}
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <fstream>
#include<string>
#include<iostream>
using namespace std;

void readFile()
{
        string tempString = "";
        /* declared both input and output file */
        string inputFilename = "inputPart5b.txt";
        string outputFilename = "resultsPart5b.txt";
        
        /* open input file and output file. Check if file opened properly. if not then exit*/
        ifstream inputFile(inputFilename);  
        if (!inputFile.good())
        {
                cout << "Wrong Input file name ";
                exit(0) ;
        }

        ofstream outputFile(outputFilename);
        if (!inputFile.good())
        {
                cout << "Wrong output file name ";
                exit(0);
        }

        int op1, op2;
        char operatorr;
        int result;
        int lineno = 0;
        while (true)   /* infinite loop to read complete file */
        {
                lineno++;
                inputFile >> op1 >> op2 >> operatorr;
                if (inputFile.eof())   /* if end of file reached then break to come out of loop */
                        break;

                switch(operatorr)     /* switch case to parse operation based on operator */
                {
                case '+':
                        result = op1 + op2;
                        outputFile << op1 << " " << operatorr << " " << op2 << " = " << result << endl;
                        break;
                case '-':
                        result = op1 - op2;
                        outputFile << op1 << " " << operatorr << " " << op2 << " = " << result << endl;
                        break;
                case '*':
                        result = op1 * op2;
                        outputFile << op1 << " " << operatorr << " " << op2 << " = " << result << endl;
                        break;
                case '/':
                        if (op2 == 0)
                        {
                                outputFile << "ERROR encountered on line " << lineno<<endl;
                        }
                        else
                        {
                                result = op1 / op2;
                                outputFile << op1 << " " << operatorr << " " << op2 << "= " << result << endl;
                        }
                        break;
                case '^':
                        if (op1 == 0)     /* if first operand with operator ^ is 0 then print error encountered */
                        {
                                outputFile << "ERROR encountered on line " << lineno<<endl;
                        }
                        else
                        {
                                result = pow(op1, op2);
                                outputFile << op1 << " " << operatorr << " " << op2 << "= " << result << endl;
                        }
                        break;
                default:
                        outputFile << "ILLEGAL operator encountered on line " << lineno;
                        break;
                }
        }

        return;
}



int main()
{
        readFile();    /* read data from file */
        return 0;
}

input file screenshot:

inputPart5b.txt - Notepad File Edit Format View Help 10 11 + 10 2 / 45 43 6 7* 50 / 7 8 DOA

outputfile screenshot

results Part5b.txt - Notepad File Edit Format View Help ho + 11 = 21 10 / 2= 5 45 43 2 6 * 7 = 42 ERROR encountered on line 5

Please upvote if u like answer otherwise comment for doubts.

Add a comment
Know the answer?
Add Answer to:
NEED HELP BEING SOLVED IN C++! USE ORIGINAL CODE TO ADD TO AS WELL! #include<iostream> using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is C++ code for parking fee management program #include <iostream> #include <iomanip> using namespace std;...

    This is C++ code for parking fee management program #include <iostream> #include <iomanip> using namespace std; void input(char& car, int& ihour,int& imin, int& ohour, int& omin); void time(char car, int ihour, int imin, int ohour, int omin, int& phour, int& pmin, int& round, double& total); void parkingCharge (char car, int round, double& total); void print(char car, int ihour, int imin, int ohour, int omin, int phour, int pmin, int round, double total); int main() { char car; int ihour; int...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #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 <<...

  • How to code up the postfixEval function using the instructions in the comments of the function?...

    How to code up the postfixEval function using the instructions in the comments of the function? // postfixEvalTest.cpp #include "Token.h" #include <iostream> #include <vector> #include <stack> using namespace std; // postfix evaluate function prototype double postfixEval(vector<Token> &expr); int main() { vector<Token> postfix; postfix.push_back(Token(VALUE,4.0)); postfix.push_back(Token(VALUE,3.0)); postfix.push_back(Token(VALUE,2.0)); postfix.push_back(Token(OPERATOR,'*')); postfix.push_back(Token(OPERATOR,'+')); postfix.push_back(Token(VALUE,18.0)); postfix.push_back(Token(VALUE,6.0)); postfix.push_back(Token(OPERATOR,'/')); postfix.push_back(Token(OPERATOR,'-')); double result = 0.0; //result = postfixEval(postfix); cout << "The result of 4 3 2 * + 18 6 / - .... is " << result << endl; vector<Token>...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • For the following task, I have written code in C and need help in determining the...

    For the following task, I have written code in C and need help in determining the cause(s) of a segmentation fault which occurs when run. **It prints the message on line 47 "printf("Reading the input file and writing data to output file simultaneously..."); then results in a segmentation fault (core dumped) I am using mobaXterm v11.0 (GNU nano 2.0.9) CSV (comma-separated values) is a popular file format to store tabular kind of data. Each record is in a separate line...

  • Here is the code I have so far. I'm trying to figure out how to implement...

    Here is the code I have so far. I'm trying to figure out how to implement a boolean and use precedence. The 2nd expression should be 14 but it comes out as 28 so I'm definitely not understanding. #include <stack> #include <iostream> #include <string> using namespace std; // Function to find precedence of // operators. int precedence(char op) {    if (op == '+' || op == '-')        return 1;    if (op == '*' || op ==...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • I'm having trouble writing this code, can some help me? Step 1: Capturing the input The...

    I'm having trouble writing this code, can some help me? Step 1: Capturing the input The first step is to write a functionGetInput()that inputs the expression from the keyboard and returns the tokens---the operands and operators---in a queue. You must write this function. To make the input easier to process, the operands and operators will be separated by one or more spaces, and the expression will be followed by #. For example, here’s a valid input to your program: 6...

  • Breaking it Down Problem One problem that I find in new Java programmers is a tendency...

    Breaking it Down Problem One problem that I find in new Java programmers is a tendency to put too much code in "main()". It is important to learn how to break a program down into methods and classes. To give you practice at this, I have a program that has too much code in main. Your job will be to restructure the code into appropriate methods to make it more readable. I will provide you with lots of coaching on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT