Use while or do while loop for this program so an unspecified number of sets of
ID: 3543632 • Letter: U
Question
Use while or do while loop for this program so an unspecified number of sets of data may be read and processed. Read input values from file, process the data as requested, and terminate the loop when the sentinel is encountered. Do not use sentinel as a value in your calculation. Since a user may only desire one type of calculation, not all three parameter will be used. Set up your input file to contain height radius and calculation type of each cylinder listed in sequential order. The first entry in the file is a phrase describes how the file is set up. Read this phrase and print it. The last entry in file is sentinel to indicate the end of data.
Set up your data file to contain the following values to test program. The first number is the height, second is radius and last is calculation type.
1.0 1.0 C
1.0 1.0 V
1.0 1.0 S
1.0 1.0 D
1010.0 250.0 C
12.3 3.25 V
123.0 43.0 S
12.3 3.25 K
1.12 2.13 C
You will switch on character based on the calculation type requested. Calculation type K and D are invalid types so those cases print input values and appropriate message and continue processing. Use 2 decimal places of precision. Print out Height radius and requested calculation values. Also print our number of data sets successfully processed.
Use while or do while loop for this program so an unspecified number of sets of data may be read and processed. Read input values from file, process the data as requested, and terminate the loop when the sentinel is encountered. Do not use sentinel as a value in your calculation. Since a user may only desire one type of calculation, not all three parameter will be used. Set up your input file to contain height radius and calculation type of each cylinder listed in sequential order. The first entry in the file is a phrase describes how the file is set up. Read this phrase and print it. The last entry in file is sentinel to indicate the end of data. Set up your data file to contain the following values to test program. The first number is the height, second is radius and last is calculation type. You will switch on character based on the calculation type requested. Calculation type K and D are invalid types so those cases print input values and appropriate message and continue processing. Use 2 decimal places of precision. Print out Height radius and requested calculation values. Also print our number of data sets successfully processed.Explanation / Answer
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
int main()
{
double height,radius;
char ch;
ifstream infile("input.txt");
if(!infile)
{
cout<<"Unable to open input file, so exiting from program." << endl;
return 0;
}
int correct_data_sets = 0;
infile >> height >> radius >> ch;
while(!infile.eof())
{
switch(ch)
{
case 'C':
{
cout << fixed << setprecision(2) <<"Height of Cylinder is "<< height << " Radius of Cylinder is "<< radius << endl;
cout << fixed << setprecision(2) <<"one circle area is "<< (3.14159*radius*radius) << endl;
correct_data_sets++;
}
break;
case 'V':
{
cout << fixed << setprecision(2) <<"Height of Cylinder is "<< height << " Radius of Cylinder is "<< radius << endl;
cout << fixed << setprecision(2) <<"Cylinder Volume is "<< (3.14159*radius*radius*height) << endl;
correct_data_sets++;
}
break;
case 'S':
{
cout << fixed << setprecision(2) <<"Height of Cylinder is "<< height << " Radius of Cylinder is "<< radius << endl;
cout << fixed << setprecision(2) <<"Cylinder Surface area is "<< (2*3.14159*radius*(radius+height)) << endl;
correct_data_sets++;
}
break;
case 'K': cout << fixed << setprecision(2) <<"Height of Cylinder is "<< height << " Radius of Cylinder is "<< radius << endl; break;
case 'D': cout << fixed << setprecision(2) <<"Height of Cylinder is "<< height << " Radius of Cylinder is "<< radius << endl; break;
default:break;
} // end switch
infile >> height >> radius >> ch;
}//end while
cout << "Total Number of data sets correctly processed is " << correct_data_sets << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.