-
Program problem
I have a read program and a write program. The first is just the write and I'm pretty sure it works. It compiles. My real problem comes with the next program. How am I supposed to make the program either display all of the file, or just all occurances of a certain name, or all of a certain name with only a certain level (i.e. am or noon or pm) Any help would be appreciated.
Here is the write program:
Code:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <fstream>
using namespace std;
/******************************************************
Structure: person
******************************************************/
struct person
{
char personName[30];
int amLevel;
int noonLevel;
int pmLevel;
};
int main()
{
ofstream outFile;
person patient;
outFile.open("a:\\Z089933sugar.dat",ios::binary);
if(outFile.fail())
{
cout<< "Error! Make sure file exists.";
exit(1);
}
//1
strcpy(patient.personName,"Benjamin_Franklin");
patient.amLevel = 180;
patient.noonLevel = 200;
patient.pmLevel = 220;
outFile.write((char*)&patient,sizeof(person));
//2
strcpy(patient.personName,"Benjamin_Franklin");
patient.amLevel = 170;
patient.noonLevel = 190;
patient.pmLevel = 200;
outFile.write((char*)&patient,sizeof(person));
//3
strcpy(patient.personName,"Benjamin_Franklin");
patient.amLevel = 190;
patient.noonLevel = 200;
patient.pmLevel = 150;
outFile.write((char*)&patient,sizeof(person));
//4
strcpy(patient.personName,"Alex_Doe");
patient.amLevel = 140;
patient.noonLevel = 170;
patient.pmLevel = 150;
outFile.write((char*)&patient,sizeof(person));
//5
strcpy(patient.personName,"Mark_Doe");
patient.amLevel = 150;
patient.noonLevel = 175;
patient.pmLevel = 135;
outFile.write((char*)&patient,sizeof(person));
//6
strcpy(patient.personName,"John_Doe");
patient.amLevel = 100;
patient.noonLevel = 120;
patient.pmLevel = 110;
outFile.write((char*)&patient,sizeof(person));
//7
strcpy(patient.personName,"Alex_DoeII");
patient.amLevel = 130;
patient.noonLevel = 125;
patient.pmLevel = 145;
outFile.write((char*)&patient,sizeof(person));
//8
strcpy(patient.personName,"Chris_Doe");
patient.amLevel = 120;
patient.noonLevel = 150;
patient.pmLevel = 135;
outFile.write((char*)&patient,sizeof(person));
//9
strcpy(patient.personName,"Jane_Doe");
patient.amLevel = 175;
patient.noonLevel = 190;
patient.pmLevel = 215;
outFile.write((char*)&patient,sizeof(person));
//10
strcpy(patient.personName,"Craven_Moorehead");
patient.amLevel = 200;
patient.noonLevel = 175;
patient.pmLevel = 220;
outFile.close();
return 0;
}
So there is all the info that the next program is supposed to read, and here is the read program:
Code:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <fstream>
using namespace std;
void showAll();
void showName();
void showNameLevel();
/***************************************************************
Struct: person
***************************************************************/
struct person
{
char personName[30];
int amLevel;
int noonLevel;
int pmLevel;
};
int main(int argc, char *argv[])
{
ifstream inFile;
person patient[25];
int i = 0;
inFile.open("a:\\Z089933sugar.dat",ios::in|ios::binary);
if(inFile.fail())
{
cout<< "Error! Make sure file exists.";
exit(1);
}
inFile.read((char*)&patient[i],sizeof(person));
while(inFile && i <= 24) // not sure what to do here
{ // might be a for statement?
i++;
}
inFile.close();
switch(argc)
{
case 0:
showAll(); // what should I send
// to these sub-functions?
break;
case 1:
showName();
break;
case 2:
showNameLevel();
break;
default:
cout<< "Too many arguments!";
}
return 0;
}
/*********************************************
Function: showAll
Use:
Arguments:
Returns:
*********************************************/
void showAll()
{
}
/*********************************************
Function: showName
Use:
Arguments:
Returns:
*********************************************/
void showName()
{
}
/*********************************************
Function: showAll
Use:
Arguments:
Returns:
*********************************************/
void showNameLevel()
{
}
I know I should be using the pointer "patient" somehow but I'm not quite sure how to do it, or if I should be building an array or what. Again any help would be appreciated.
Thanks,
Bird
-
Code:
#include <math.h>
#include <ctype.h>
#include <string.h>
Code:
#include <cmath>
#include <cctype>
#include <cstring>
As for finding all occurances of something. Just don't read directly into the structure. Read each line into temp variables, then check to see if it matches the criteria. If it does, add it to the struct. Then output the struct.
-
The only problem with that is I have to read all of the data from the first program. What I really want to know is what to pass my sub functions. For instance
Code:
// I know this
c:\>program11b // This is argc (0)
c:\>program11bJohn_Doe // This is argc (1)
c:\>program11bJohn_Doeam // This is argc (2)
This is how I have to call the program in DOS
So I know that these(0,1,2) are my cases for the switch but what should I be passing the sub-functions so it will either print all of it, or just all of one name, or all of one name with only one level without passing in all of the info line by line? Should I be adding more variables or characters or pointers, etc?
-
Ah, ok. No problem.
For show all, just output everything in your array. That's simple for you, I'm sure. For the next, you have to pass argv[] to the function, then check argv[1] against the values of the names in the array. If it finds the match, print it. Now make sure you do this to the end of the array, not when you find an occurance. The next, you make a switch for argv[2], check to see if it's am, pm, noon... then check the pertaining variable to see if it has a value. You get the idea?
-
That helped me a ton also. Thanks for all the help on both my threads. Saved me some massive time. Really apprciate it.
-
Had a compile error. I'm not sure if my sub functions should be void functions but I'm pretty sure they should be becuase they aren't returning anything to the main function.
Here is a bit of my code in main() and the sub fuction.
Code:
//This is in main()
switch(argc)
{
case 0:
showAll(argv[0]);
break;
//This is in the sub fuction showAll()
void showAll(char *argv[0])
{
cout<< argv[0] <<"<inFile>\n";
}
With this I'm getting an error that reads :"conversion from 'char*' to non-scalar type 'person' requested"
I know I'm a pain but I'm just starting out with this programming stuff and I just want to learn what I'm doing wrong. Guess I'm not looking at it the right way.
-
Code:
void showAll(char *argv[]);
// and the call
showAll(&argv[1]);
Try this.
[EDIT]