Thread: Program difficulties

  1. #1
    Registered User Birdhaus's Avatar
    Join Date
    Nov 2005
    Posts
    11

    Question 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.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #3
    Registered User Birdhaus's Avatar
    Join Date
    Nov 2005
    Posts
    11
    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

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM