Question

Using C++ Write a program that gets 5 or more sets of information from user in...

Using C++
Write a program that gets 5 or more sets of information from user in this format

First name last name Sex(Male –M, Female F) Age   

Mellissa Henderson F 12

Marcus Cruz M 51

Anderson Mellissa F 19

Kenneth Lisa F 34

William Nancy F 24

Richard Eric M 69

Ronald William M 13

Christopher Elizabeth F 8

David Rodriguez M 11

Ryan Peter M 31

Your program should generate a 9 digit unique social security number for each person.

It should add Mr. for male Ms. for female when writing it to the screen.

Your program should calculate how many kids, adults, teenagers and seniors based on this rule. It should calculate the percentage of females and males in that list.

Kids 0-12 Teenagers 13 -19 Adults 21 -59 Seniors 60 and up.

The program should output all the values in the following format (sample)


Ms. Mellissa Henderson Kid 313-20-4343

Mr.Marcus Cruz Adult 603-49-7821

Ms. Anderson Mellissa Teenager 213-90-4341

Ms. Kenneth Lisa Adult 313-20-4344

Ms. William Nancy Adult 313-20-4345

Mr. Richard Eric Senior 313-20-4346

Mr.Ronald William Teenager 313-20-4349

Ms. Christopher Elizabeth Kid 613-69-7881

Mr.David Rodriguez Kid 603-89-7823

Mr.. Ramirez Bianca Adult 603-49-0821

.

Kids 3

Teenagers 2

Adults 4

Seniors 1

Male = 40%

Female =60%

Your program should use

One and/or two dimensional arrays,

Loop(s) to read/write

At least one function

If else statement

Switch statement
0 0
Add a comment Improve this question Transcribed image text
Answer #1

///////////////////////// read data from file

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>

using namespace std;

// function for random unique ssn number

void randomCode(int code[][3],int count){
   bool b=false;
   long ssn=0;
  
   while(true){
      
       // get each digit for ssn
       // first 3 digits
       int fR=rand()%900+100;
      
       // 2nd 2 digits
       int mR=rand()%90+10;
      
       // 3rd 4 digits
       int lR=rand()%9000+1000;
      
       ssn=fR*1000000+mR*10000+lR;
       b=false;
      
       for(int i=0;i<count;i++){
           long number=code[i][0]*1000000+code[i][1]*10000+code[i][2];
          
           // if ssn number exist ,
           if(number==ssn){
               b=true;
               break;
           }
       }  
      
       // if unique ssn number
       if(b==false){
           break;
       }
   }
  
   // split ssn number and insert into array
   code[count][0]=(int)(ssn/1000000);
   ssn=ssn%1000000;
   code[count][1]=(int)(ssn/10000);
   code[count][2]=(ssn%10000);
  
}


int main(){
   // input file name
  
   cout<<"Enter input file name : ";
   string fName;
   cin>>fName;
  
   ifstream in;
   in.open(fName.c_str());
  
   // local variables
  
   string firstName[50];
   string lastName[50];
   char sex[50];
   int age[50],i=0;
   int ssn[50][3];
   int tm=0,tf=0;
  
   if(!in.bad()){
      
       // read all data from file
       while(!in.eof()){
           string fname,lname;
           char ch;
           int age_;
          
           in>>fname;
           in>>lname;
           in>>ch;
           in>>age_;
          
           // by switch 'M' for male and for 'F' for female
           switch(ch){
               case 'M':{
                   tm++;
                   break;
               }
               case 'F':
               {
                   tf++;
                   break;
               }
           }
          
           // store in corresponding array
           firstName[i]=fname;
           lastName[i]=lname;
           sex[i]=ch;
           age[i]=age_;
           randomCode(ssn,i);
          
           // to store in next block
           i++;  
       }
      
   }
   else{
       cout<<"\nError in File.";
   }
  
   cout<<"File data loded.\n";
  
   ///// output file
  
   cout<<"Enter output file name : ";
   string ofName;
   cin>>ofName;
  
   ofstream out;
   out.open(ofName.c_str());
  
   int k_=0,t_=0,a_=0,s_=0;
  
   if(!out.bad()){
       // print and write in file both
      
       for(int a=0;a<i;a++){
           if(sex[a]=='M')   {out<<"Mr. ";cout<<"Mr. ";}
           if(sex[a]=='F')   {out<<"Ms. ";cout<<"Ms. ";}
          
           out<<" ";
           out<<firstName[a];
           out<<" ";
           out<<lastName[a];
           out<<" ";
          
           cout<<" ";
           cout<<firstName[a];
           cout<<" ";
           cout<<lastName[a];
           cout<<" ";
          
           // age selection
           if(age[a]>=0 && age[a]<=12){out<<"kid";cout<<"kid";k_++;}
           if(age[a]>=13 && age[a]<=20){out<<"Teenager";cout<<"Teenager";t_++;}
           if(age[a]>=21 && age[a]<=59){out<<"Adult";cout<<"Adult";a_++;}
           if(age[a]>=60){out<<"Senior";cout<<"Senior";s_++;}
          
           out<<" ";
           out<<ssn[a][0];
           out<<"-";
           out<<ssn[a][1];
           out<<"-";
           out<<ssn[a][2];
           out<<endl;
          
          
           cout<<" ";
           cout<<ssn[a][0];
           cout<<"-";
           cout<<ssn[a][1];
           cout<<"-";
           cout<<ssn[a][2];
           cout<<endl;
          
       }
      
       out<<"Kids "<<k_<<endl;
       out<<"Teenagers "<<t_<<endl;
       out<<"Adults "<<a_<<endl;
       out<<"Seniors "<<s_<<endl;
       out<<"Male= "<<((tm*100)/(tm+tf))<<"%"<<endl;
       out<<"Female= "<<((tf*100)/(tm+tf))<<"%";
      
       cout<<"\nKids "<<k_;
       cout<<"\nTeenagers "<<t_;
       cout<<"\nAdults "<<a_;
       cout<<"\nSeniors "<<s_;
       cout<<"\nMale= "<<((tm*100)/(tm+tf))<<"%";
       cout<<"\nFemale= "<<((tf*100)/(tm+tf))<<"%";
      
       out.close();
   }
   else{
       cout<<"\nError in File.";
   }
  
  
  
   return(0);
}

Add a comment
Know the answer?
Add Answer to:
Using C++ Write a program that gets 5 or more sets of information from user in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CREATE TWO C++ PROGRAMS : Program 1 Write a program that reads in babynames.txt (provided) and...

    CREATE TWO C++ PROGRAMS : Program 1 Write a program that reads in babynames.txt (provided) and outputs the top 20 names, regardless of gender. The file has the following syntax: RANK# BoyName Boy#OfBirths Boy% GirlName Girl#OfBirths Girl% You should ignore the rank and percentages. Compare the number of births to rerank. Output should go to standard out. Program 2 Write a program that reads a text file containing floating-point numbers. Allow the user to specify the source file name on...

  • C++ Create an application that searches a file of male and female first names. A link...

    C++ Create an application that searches a file of male and female first names. A link to the file is provided on the class webpage. "FirstNames2015.txt" is a list of the most popular baby names in the United States and was provided by the Social Security Administration. Each line in the file contains a boy's name and a girl's name. The file is space-delimited, meaning that the space character is used to separate the boy name from the girl name....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT