Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have the following C++ code which needs comments for each line of code explaining what each line of code does, I am only missing

I have the following C++ code which needs comments for each line of code explaining what each line of code does, I am only missing comments for a few lines of code which am not sure how to put the comments in my own words.

#include  //allows us to use cin,cout and endl #include  //allows to read data from a file and write data to a file using namespace std; //allows us to use cin,cout and endl without having to type the prefix std:: const int NUM_DIVISORS = 5; const int DIVISORS[] = {3, 5, 7, 9, 11}; int main() // used to start the program { int num; //this variable will store the number that the user inputs ofstream outfile("numbers.txt"); // open file for writing do { cout << "Enter a positive number (-1 to stop): "; //displays text on the console prompting user to enter a number cin >> num; if (num > 0) { outfile << num << endl; // write number to file } } while (num > 0); outfile.close(); // close file ifstream infile("numbers.txt"); // open file for reading while (infile >> num) { cout << "Number: " << num << " "; for (int i = 0; i < NUM_DIVISORS; i++) { if (num % DIVISORS[i] == 0) { cout << "Divisible by " << DIVISORS[i] << ": Yes "; } else { cout << "Divisible by " << DIVISORS[i] << ": No "; } } cout << endl; } infile.close(); // close file cout << endl; return 0; //ends the main function (program) }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions