-
Program difficulties
I've provided the whole program so anyone can tell what I'm trying to accomplish. In my last two sub-functions how am I going to keep the loop going after it finds a name (if there is more than one occurance of that name) I don;t want the loop t stop after finding the name just once. I want it to display all occurances of that name.
Code:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <fstream>
using namespace std;
/***************************************************************
Struct: person
***************************************************************/
struct person
{
char personName[30];
int amLevel;
int noonLevel;
int pmLevel;
};
void showAll(person [], char *[], int);
void showName(person[], char *[], int, int);
void showNameLevel(person[], char *[], int, int);
int main(int argc, char *argv[])
{
ifstream inFile;
person patient[25];
int i;
inFile.open("c:\\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)
{
i++;
inFile.read((char*)&patient[i],sizeof(person));
}
inFile.close();
switch(argc)
{
case 1:
showAll(patient, argv, i);
break;
case 2:
showName(patient, argv, i, argc);
break;
case 3:
showNameLevel(patient, argv, i, argc);
break;
default:
cout<< "Too many arguments!";
}
return 0;
}
/*********************************************
Function: showAll
Use:
Arguments:
Returns:
*********************************************/
void showAll(person patient[], char * argv[], int max)
{
int i;
for (i = 0; i < max; i++)
{
cout << "\nPatient:" << patient[i].personName;
cout << "\n am level:" << patient[i].amLevel;
cout << "\n pm level:" << patient[i].pmLevel;
cout << "\n noon level:" << patient[i].noonLevel;
}
return;
}
/*********************************************
Function: showName
Use:
Arguments:
Returns:
*********************************************/
void showName(person patient[], char * argv[], int max, int argc)
{
int i;
int found;
for(i = 0; i < max; i++)
{
found = strcmp(argv[1], patient[i].personName);
if(found == 0)
{
}
/*********************************************
Function: showNameLevel
Use:
Arguments:
Returns:
*********************************************/
void showNameLevel(person persons[], char * argv[], int max, int argc)
{
}
Any help would be appreciated.
-
You don't have to do anything, the loop will keep going until it hits max or until you break out of it. So just don't break out of the loop and you'll be fine.
-
Sorry, I ment in the function "void showName"
I was told to use the if statement. So how should it work??
Once I know how to do that then I'll know how to do the showNameLevel function as well
-
I was talking about that function as well. You have a for loop. You want the for loop to continue even if you find the name. Unless you specifically break out of the for loop, it will continue automatically. The if statement should be where you output the data you found when the name matched, or whatever showName is supposed to do.
-
Code:
// You can knock out the found variable.
if(strcmp(argv[1], patient[i].personName) == 0) // Meaning you found a match
cout << "Found match: " << patient[i].personName << '.' << endl;
// and that's it. It'll loop until you real the entire array and continue to output the matches.
Also, since you've already determinted that argc is 2 by the time you call the function, there is really no reason to pass it to the function in this case.