C++ Problem Specification: Your friend asks you to help him/her in studying the
ID: 3776606 • Letter: C
Question
C++ Problem Specification:
Your friend asks you to help him/her in studying the height of a population. He/she wants to store data in a file called “population.dat” of the following format: a sequence of code and height separated by one space. The code can be:
- either 1, in that case the following height corresponds to a man,
- or 2, in that case the following height corresponds to a woman (See Figure 1 as a sample).
Then, he/she wants to be able to get the average height of woman, or the average height of men, or the average height of the whole population (i.e. men and women). Sometimes, he/she needs to know two or all of them. Therefore, your friend expects to have a menu as follows:
1: Women heights average
2: Men's heights average
3: Population average
4: Other: exit
Your choice:
That continues to be displayed till he/she decides to exit.
Furthermore, she needs to store all computed data on a file.
Write a C++ program that:
-Asks the user to enter a code, then a height and store the data in a file called "population.dat" (See Figure 1 for sample and format). The program must allow the user keep entering data until he/she enters -1 as code (See Figure 2). The code must be either 1 or 2 as explained above; any other code (except -1) must be rejected i.e.: the user is re-asked to enter the code (See Figure 3).
-Opens the file "population.dat" to read the data and prepare for computing the averages.
-Writes computed data to a file named "height.out" (See Figure 5 for a sample output file).
-Displays the above menu and asks the user to enter his/her choice. Once the choice is entered the corresponding average is displayed along with the number of heights read. The program must display the menu and allow the user to enter his/her choice until he/she decides to exit (See Figure 2).
Note that:
-Your program should make appropriate use of switch statements, if statements, looping control structures, and input and output file operations.
-Your program must read the data from the file using a loop.
-Your program should handle any exceptional situation such as the first code is -1 or all heights entered are men ones, error while creating the input file, etc. (See Figure 3 and Figure 4) - Be sure to use proper formatting and appropriate comments in your source code.
-The averages should be displayed using two decimal numbers.
-Output file must be formatted as shown in Figure 5.
-Comment your code.
Explanation / Answer
#include<iostream>
#include<iomanip>
#include<fstream
#include<string>
using namespace std;
int main()
{
int choice;
int code,mcount,wcount,pcount;
string Message,Message2,Message3,Message4,Message5, Message6;
char data[26];
int mh[6] , wh[7],ph[13] , i ;
int nOfmh , nOfwh ,nOfph;
float mavg , wavg ,pavg ;
ifstream inputfile;
inputfile.open("population.dat");
for (int i=0;i<26;i++)
{
inputfile>>data[i];
}
if (!inputfile)
{
cout<<"Error, Can not find input file"<<endl;
return 1;
}
cout<<"Please enter the height population data"<<endl;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
cout<<"Enter the height : "<<endl;
cin>>height;
cout<<"Enter the code: 1 : Man, 2: Woman, -1: End : ";
cin>>code;
while ((choice<=1) || (choice>=3));
switch(choice)
{
case 1:
while((code!=0)||(code!=-1))
{
if(code==1)
{
Message="Number of women heights entered is ";
nOfwh += wh[wcount];
wcount++;
}
else
Message="The number of women’s heights is ";
nOfwh=0;
}
case 2:
while((code!=0)||(code!=-1))
{
if(code==2)
{
Message3="Number of men heights entered is ";
nOfmh += mh[mcount];
mcount++;
}
else
Message3="The number of men’s heights is ";
nOfmh=0;
}
case 3:
while ((code!=0)||(code!=-1))
{
if((code==1)&(code==2))
{
Message5="Number heights entered is ";
nOfph += ph[pcount];
pcount++;
}
else
Message5="No data entered ";
nOfmh=0;
}
case 4:
break;
}
{
if (wcount>0)
{
Message2="Women height average is ";
wavg=(nOfwh/wcount);
}
else
Message2="no average to compute";
}
{
if (mcount>0)
{
Message4="Men height average is ";
mavg=(nOfmh/mcount);
}
else
Message4="no average to compute";
}
if (pcount>0)
{
Message6="Population height average is ";
pavg=(nOfph/pcount);
}
else
Message6="no average to compute";
}
cout<<endl<<endl<<endl;
cout<<" ************************************************"<<endl;
cout<<" 1: Women height average"<<endl;
cout<<" 2: Men height average"<<endl;
cout<<" 3: Population height average"<<endl;
cout<<" other: Exit"<<endl;
cout<<" ************************************************"<<endl;
cout<<" Your choice: ";
cin>>choice;
cout<<endl;
cout<<Message<<nOfwh<<". "<<Message2<< wavg<<endl;
cout<<" ************************************************"<<endl;
cout<<" 1: Women height average"<<endl;
cout<<" 2: Men height average"<<endl;
cout<<" 3: Population height average"<<endl;
cout<<" other: Exit"<<endl;
cout<<" ************************************************"<<endl;
cout<<" Your choice: ";
cin>>choice;
cout<<endl;
cout<<Message5<<nOfph<<". "<<Message6<< pavg<<endl;
cout<<" ************************************************"<<endl;
cout<<" 1: Women height average"<<endl;
cout<<" 2: Men height average"<<endl;
cout<<" 3: Population height average"<<endl;
cout<<" other: Exit"<<endl;
cout<<" ************************************************"<<endl;
cout<<" Your choice: ";
cin>>choice;
cout<<endl;
cout<<Message3<<nOfmh<<". "<<Message4<< mavg<<endl;
cout<<" ************************************************"<<endl;
cout<<" 1: Women height average"<<endl;
cout<<" 2: Men height average"<<endl;
cout<<" 3: Population height average"<<endl;
cout<<" other: Exit"<<endl;
cout<<" ************************************************"<<endl;
cout<<" Your choice: ";
cin>>choice;
cout<<endl;
inputfile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.