****Output
There were 305 birds in this list.
Species: Snow_Goose Number: 305
There were 567 birds in this list.
Species: Canada_Goose Number: 567
There were 4 birds in this list.
Species: Tundra_Swan Number: 4
There were 31 birds in this list.
Species: Wood_Duck Number: 31
There were 60 birds in this list.
Species: Gadwall Number: 60
There were 143 birds in this list.
Species: American_Wigeon Number: 143
Press any key to continue . . .
***How would I make a loop so that it would add up all the numbers of birds and species from the text file. Right now it is just taking each type of specie and showing its number, but I want to add all up like for example: 305 + 567 + 4 + 31 + 60 + 143 = 1110
Here is my code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct birds
{
string species;
int number;
};
const int ARRAY_SIZE = 200;
// take an array of players, initialize each object to some
//predefined state,....
void initialize (birds species[], int number);
//PRINT FUNCTION.... PRINT THE TEAM ARRAY
void print(const birds species[], int number);
birds getBirds(fstream& inFile);
void print(birds s);
void bubbleSort(birds list[], int length);
/////////////////////////////////////////////////////////
int main()
{
int number;
char cityName[ARRAY_SIZE];
char choice = 'Y';
fstream inFile;
cout << "\t\tWelcome to this program!\n"
<< "\nChoose a number with a city to see its number of species"
<< "\n[1] Big Stone Gap,VA"
<< "\n[2] Virginia Beach, VA"
<< "\n[3] Melfa, VA\n"
<< "Enter a number: ";
cin >> number;
cout << endl;
////////////////////////////////////////////////////
if(number == 1)
{
inFile.open("one.txt");
inFile.getline(cityName, ARRAY_SIZE);
cout << cityName << endl;
if ( !inFile)
{
cout << "invalid file name \n";
system ("pause");
return 1;
}
}
////////////////////////////////////////////////////
if(number == 2)
{
inFile.open("two.txt");
inFile.getline(cityName, ARRAY_SIZE);
cout << cityName << endl;
if ( !inFile)
{
cout << "invalid file name \n";
system ("pause");
return 1;
}
}
////////////////////////////////////////////////////
if(number == 3)
{
inFile.open("three.txt");
inFile.getline(cityName, ARRAY_SIZE);
cout << cityName << endl;
if ( !inFile)
{
cout << "invalid file name \n";
system ("pause");
return 1;
}
}
////////////////////////////////////////////////////
if(number > 3)
{
cout << "The number you have entered was invalid!\n"
<< "Next time, please enter a lower number preferably numbers 1-3!" << endl << endl;
system("pause");
return 0;
}
////////////////////////////////////////////////////
birds odu1;
birds odu2;
birds odu3;
odu1.species = 9;
odu1.number = 212;
//---------------------------
odu2.species = 9;
odu2.number = 212;
//---------------------------
odu3.species = 9;
odu3.number = 212;
//---------------------------
//MAKE AN ARRAY OF OBJECTS
birds team[ARRAY_SIZE];
//INITIALIZE THE ARRAY OF OBJECTS TO KNOWN VALUES.
initialize(team, ARRAY_SIZE);
//PRINT OUT THE ARRAY OF OBJECTS
system("pause");
system("cls");
print(team, ARRAY_SIZE);
system("PAUSE");
system("cls");
odu3 = getBirds(inFile);
print(odu3);
team[0]=odu3;
odu2 = getBirds(inFile);
print(odu2);
team[1]=odu2;
odu1 = getBirds(inFile);
print(odu1);
team[2]=odu1;
odu3 = getBirds(inFile);
print(odu3);
team[3]=odu3;
odu2 = getBirds(inFile);
print(odu2);
team[4]=odu2;
odu1 = getBirds(inFile);
print(odu1);
team[5]=odu1;
system("PAUSE");
system("cls");
bubbleSort(team, ARRAY_SIZE);
print(team, ARRAY_SIZE);
system("PAUSE");
return EXIT_SUCCESS;
}
/////////////////////////////////////////////////
// take an array of players, initialize each object to some
//predefined state,....
void initialize (birds team[], int number )
{
for(int count =0; count < number; count++)
{
team[count].species = "WoW";
team[count].number = 0;
}
}
//////////////////////////////////////////
void print(const birds team[], int number)
{
for(int count =0; count < number; count++)
{
cout <<"Birds: "<<count;
cout <<"\tSpecies: "<< team[count].species;
cout <<"\t\tNumber: "<< team[count].number<<"\n";
}
}
////////////////////////////////////////////
// use the file stream variable to access a single line of data]
// and return an object of type player
birds getBirds(fstream& inFile)
{
birds temp;
inFile >> temp.species >> temp.number;
/////////////////////////////////////////////////////////
cout << "\nThere were " << temp.number << " birds in this list." << endl;
//cout << "\nThe species with the most amount of birds is " << temp.species << ".\n" << endl;
////////////////////////////////////////////////////////
return temp;
}
//
//////////////////////////////////////////
void print(birds s)
{
cout <<"Species: "<< s.species << "\t";
cout <<"Number: "<< s.number<< "\n";
}
//////////////////////////////////////////
void bubbleSort(birds list[], int length)
{
birds temp;
int iteration;
int index;
for (iteration = 1; iteration < length; iteration++)
{
for (index = 0; index < length - iteration; index++)
if (list[index].number > list[index + 1].number)
{
temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
}
}
}