counting true's and false's from bools

This is a discussion on counting true's and false's from bools within the C++ Programming forums, part of the General Programming Boards category; I have written a program that reads in data from a file about people like so : first name, last ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    counting true's and false's from bools

    I have written a program that reads in data from a file about people like so :

    first name, last name, age, is hispanic, if hispanic what type of hispanic, race, disability

    I have a bool that works to determine if a person is hispanic or not and reports back 1 if they are and 0 if they are not. I juat can't figure out how I can count the # of 1's (trues) and report how many people on the list are hispanic.

    This is my code so far:

    Code:
    #pragma once
    #include <string>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    class peopleClass
    {
    private:
        string fname, lname, race, citizen, disability,hispanictype;
    	int age;
    	bool hispanic;
    public:
        peopleClass(){;}
        string getFname()const{return fname;}
        string getLname()const{return lname;}
        string getRace()const{return race;}
        string getCitizen()const{return citizen;}
        string getDisability()const{return disability;}
    	string getHispanictype () const {return hispanictype;}
    	int getAge()const{return age;}
    	char getHispanic()const {return hispanic;}
        void setFname(string newName){fname=newName;}
        void setLname(string newName){lname=newName;}
        void setRace(string newRace){race=newRace;}
        void setCitizen(string newCitizen){citizen=newCitizen;}
        void setDisability(string newDisability){disability=newDisability;}
    	void setHispanictype (string newHispanictype) {hispanictype=newHispanictype;}
        void setAge(int newAge){age=newAge;}
        void setHispanic(bool value){hispanic=value;}
    	//void setHispanic(char newHispanic){hispanic=newHispanic;}
    	
    	bool isHispanic (const peopleClass&) const;
    
    
        friend bool operator < (const peopleClass &left, const peopleClass &right);
        friend bool operator <= (const peopleClass &left, const peopleClass &right);
        friend bool operator > (const peopleClass &left, const peopleClass &right);
        friend bool operator >= (const peopleClass &left, const peopleClass &right);
        friend bool operator == (const peopleClass &left, const peopleClass &right);
        friend bool operator != (const peopleClass &left, const peopleClass &right);
    
        friend ostream & operator << (ostream &out, const peopleClass &right);
        friend istream & operator >> (istream &in, peopleClass &right);
    
    
    };
    
    bool operator < (const peopleClass &left, const peopleClass &right)
    {
        return left.lname < right.lname;
    }
    bool operator <= (const peopleClass &left, const peopleClass &right)
    {
        return left.lname <= right.lname;
    }
    bool operator > (const peopleClass &left, const peopleClass &right)
    {
        return left.lname > right.lname;
    }
    bool operator >= (const peopleClass &left, const peopleClass &right)
    {
        return left.lname >= right.lname;
    }
    bool operator == (const peopleClass &left, const peopleClass &right)
    {
        return left.lname == right.lname;
    }
    bool operator != (const peopleClass &left, const peopleClass &right)
    {
        return left.lname != right.lname;
    }
    
    bool peopleClass::isHispanic (const peopleClass& getHis) const
    {
    //	if peopleClass.getHispanic = 'H';
    //	return true;
    	return hispanic;
    }
    
    ostream & operator << (ostream &out, const peopleClass &right)
    {
        out << setw(15) << right.lname 
            << setw(15) << right.fname
            << setw(8) << right.age
            << setw(8) << right.hispanic;
    		
    
    	//if (peopleClass.isHispanic (hispanic))
    	out << setw(8) << right.hispanictype;
           
    	out << setw(8) << right.race
            <<setw(8)<< right.citizen
            <<setw(8)<< right.disability;
    
        return out;
    }
    istream & operator >> (istream &in, peopleClass &right)
    {
     
    	char tmp;
    
        in >> right.fname >> right.lname >> right.age >> tmp;
        if(tmp=='H')
        {
            in >>right.hispanictype ;
            right.hispanic=true;
    
        } 
    
    	else
            right.hispanic=false;
        in>>right.race >> right.citizen >> right.disability;
        return in;
    }
    
    void sort(peopleClass people[], int numPeople);
    
    int main()
    {
        peopleClass Group[500];
        ifstream fin("program2.txt");
        ofstream fout("output2.txt");
    
        int i, numPeople;
        i=0;
        while(i<500 && fin >> Group[i])
            i++;
        numPeople=i;
        
    	sort(Group, numPeople);
    
        for(i=0;i<numPeople;i++)
            fout << Group[i] << endl;
    
        return 0;
    }
    
    void sort(peopleClass Group[], int numPeople)
    {
        int i,j,min;
        for(i=0;i<numPeople-1;i++)
        {
            min=i;
            for(j=i+1;j<numPeople;j++)
                if(Group[j] < Group[min])
                    min=j;
            swap(Group[i], Group[min]);
        }
    }
    Does anyone have an idea of how I could count how many people are hisapnic?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    You've got an array, so I would suppose a for loop, and a counter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    I get that, but where would I put it?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21