Question

In: Computer Science

C++ programming question Write a program that will read input from a text file called "theNumbers.txt"...

C++ programming question

  1. Write a program that will read input from a text file called "theNumbers.txt" (you will have to provide your own when debugging). The text file that is to be opened is formatted a certain way, namely, there is always one integer, one character (representing an operation), another integer, and then a new line character, with spaces between each item. A sample text file is provided below.

theNumbers.txt

144 + 26

3 * 18

88 / 4

22 – 8

16 / 2

20 * 20

-Your Program should read in all items, compute the answer to each expression (any decimals should be rounded to the nearest tenth), and output the answers to an output file of the user's choosing. The formatting of the output is that there is a set width of 10 characters for each answer, left aligned, with a new line character every 3 answers. A sample “answers.txt” is provided below. (not drawn to scale)

answers.txt

17000      54      22

14               8            400

-You may assume that the name of the text file that will be outputted to is always 20 characters or less.

-You may assume that there are only 4 possible operations ( + - *   /), add, subtract, multiply, and decimal division, and that no other characters will show up as the middle symbol.

- Appropriate error messages should be displayed if the file cannot be opened.

- Extra Credit: The answers in the output file ONLY show a decimal if the answer is not a whole number answer.

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include<iostream>

#include<iomanip>

#include<fstream>

using namespace std;

int main(){

    int num1, num2, n;

double result;

    char op;

    ifstream inFile;

    inFile.open("theNumbers.txt");

    if(inFile.fail()){

        cout<<"Unable to open file"<<endl;

        return -1;

    }

    ofstream outFile;

    outFile.open("answers.txt");

    n = 1;

    while(!inFile.eof()){

        inFile>>num1>>op>>num2;

        if(op=='+')

            result = num1 + num2;

        else if(op=='-')

            result = num1 - num2;

        else if(op=='*')

            result = num1 * num2;

        else

            result = (double)num1 / num2;

        outFile<<setw(10)<<left<<result;

        if(n%3==0)

            outFile<<endl;

        n++;

    }

    cout<<"Successfully saved into answers.txt"<<endl;

    outFile.close();

    inFile.close();

    return 0;

}


Related Solutions

C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
Write a menu-driven program to read 3 employees' information from a text file called “ EmployeeFile”...
Write a menu-driven program to read 3 employees' information from a text file called “ EmployeeFile” for this example but that can be edited to add more employees later. Each employee has a name field, ID fields, and a Salary Record. The Salary Record has weekly hours, rate per hour, gross salary, Tax, and net salary field. 1.1 Implement the project with queue using pointers 1.2 Write push and pop functions for queue using pointers C++ EmployeeField.txt example John_Doe 123456...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT