c++ assignment This is the whole assignment, but i already have the code for que
ID: 656178 • Letter: C
Question
c++ assignment
This is the whole assignment, but i already have the code for question 1 [i will post it], all i need help with is question 2.
here is code for question 1:
#include <iostream>
#include<fstream>
using namespace std;
//constant declarations
const int SIZE=10;
const int NUM_DIV=4;
//struct declaration..already you given
struct Division
{
char divName[SIZE];
double sales;
};
int main()
{
//initial variables declarations
Division div[NUM_DIV];
double sale;
char divNames[NUM_DIV][6]={"North","South","East","West"};
//storing divnames into struct array
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
div[i].divName[j]=divNames[i][j];
}
}
//reading sales from user and storing into structs
for(int i=0;i<4;i++){
cout << " Enter sales for division " << divNames[i] << ":";
cin>>sale;
div[i].sales=sale;
}
//opening files and writing output
ofstream outputFile;
outputFile.open("output.dat");
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
outputFile << div[i].divName[j];
}
outputFile << div[i].sales;
}
//closing the file
outputFile.close();
return 0;
}
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
struct persondetail
{
char name[Size];
};
int main()
{
int age;
int n;
cout<<"enter the data ,1:"<<endl;
cout<<"fetch the data , 2:"<<endl;
cin>>n;
switch(n)
{
case 1:
{
persondetail x;
cout<<"enter the name of person ";
cin>>x.name;
cout<<"Enter the age of person ";
cin>>Z.age;
ofstream outFile;
outFile.open("ios.dat",ios::out);
outFile.write((char*)&a,sizeof(x));
outFile.close();
break;
}
case 2:
{
persondetail Z;
int age;
cout<<"enter the age of person ";
cin>>age;
ifstream inFile;
InFile.open("info.dat",ios::in);
int nm=0,check=-1;
while(!inFile.eof())
{
InFile.read((char*)&z,sizeof(Z));
if(age == Z.age)
{
check = 1;
}
nm++;
}
if(check == 1)
{
cout<<"find of "<<nm<<endl;
cout<<"name is "<<x.name<<endl;
cout<<"age is "<<Z.age;
}
inFile.close();
break;
}
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.