void forr() { ofstream fout; fout.open(\"FileName.TXT\"); fout<<For loop is prin
ID: 3873458 • Letter: V
Question
void forr()
{
ofstream fout;
fout.open("FileName.TXT");
fout<<For loop is printed:<<endl;
int counter=1;
for(counter=1;counter<=10;counter++)
fout<<counter<<endl;
fout.close();
}
void whl()
{
ofstream fout;
fout.open("FileName.TXT");
fout<<While loop is printed:<<endl;
int counter=1;
while(counter<=10)
fout<<counter<<endl;
fout.close();
}
void doo()
{
ofstream fout;
fout.open("FileName.TXT");
fout<<Do While loop is printed:<<endl;
int counter=1;
do
{
fout<<counter<<endl;
}
while(counter<10);
fout.close();
}
Using the above code...
Create a Menu Driven Program [see below] to ask for how many repetitions you want for each LOOP TYPE.
Your Menu should be this:
Repetition Selection for Looping.
1. While Loop - how many?
2. Do Loop – how many?
3. For Loop – how many?
4. Quit!
You must use a CASE/SWITCH, WITH A QUIT AND DEFAULT ERROR MESSAGE FOR LOOPING INCORRECT CHOICES.
All Error Message must be relevant and be printed on the screen.
A new Menu must be displayed on the console each time a menu choice is made.
All choices must be tested and printed in the text file.
You should use the Tab command [ ] to center your menu on the console and the to move it down to the center.
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
void forr()
{
ofstream fout;
fout.open("FileName.TXT");
fout<<"For loop is printed:"<<endl;
int counter=1;
for(counter=1;counter<=10;counter++)
fout<<counter<<endl;
fout.close();
}
void whl()
{
ofstream fout;
fout.open("FileName.TXT");
fout<<"While loop is printed:"<<endl;
int counter=1;
while(counter<=10){
fout<<counter<<endl;
counter++;
}
fout.close();
}
void doo()
{
ofstream fout;
fout.open("FileName.TXT");
fout<<"Do While loop is printed:"<<endl;
int counter=1;
do
{
fout<<counter<<endl;
counter++;
}
while(counter<10);
fout.close();
}
int main(){
int ch;
int n;
do {
cout << " 1.While Loop-how many?"<<endl;
cout << " 2.Do Loop-how many?"<<endl;
cout << " 3.For Loop-how many?"<<endl;
cout << " 4.Quit"<<endl;
cout << " Enter your choice :";
cin >> ch;
if (ch == 4)
break;
switch (ch) {
case 1: cout << " How many : ";
cin >> n;
for (int i = 0; i<n; i++)
whl();
break;
case 2: cout << " How many : " ;
cin >> n;
for (int i = 0; i<n; i++)
doo();
break;
case 3: cout << " How many : ";
cin >> n;
for (int i = 0; i<n; i++)
forr();
break;
default: cout << " Incorrect choice " ;
break;
}
} while (1);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.