****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;
            }
    }
}

Recommended Answers

All 6 Replies

As you show the number of birds, add the number to a running total, like totlaBirds += numberOfBirds Be sure to zero the total before starting.

As you show the number of birds, add the number to a running total, like totlaBirds += numberOfBirds Be sure to zero the total before starting.

I did what you asked but it keeps printing out the wrong number of birds. What do you mean by adding it to yor running total? Would you put the sum in your for loop?

Here's where I tell you to turn your brain back on and think. If you want to add values to a running total, where would you put that add?

Let's get away from the computer. Sit down with pencil and paper and run through the task. Write down the steps you take -- in order -- from beginning to end. Then look at the Then look at your code. Does it follow what you wrote down? If not, fix it.

Here's where I tell you to turn your brain back on and think. If you want to add values to a running total, where would you put that add?

Let's get away from the computer. Sit down with pencil and paper and run through the task. Write down the steps you take -- in order -- from beginning to end. Then look at the Then look at your code. Does it follow what you wrote down? If not, fix it.

I know that you add it to the for loop. I did sum = sum + birds[ARRAY_SIZE] and it gave me a sum but not what I was looking for. I am in my first ever programming class and my teacher does a lousy job of explaining things.

... my teacher does a lousy job of explaining things.

So do you.

C'mon, man! You changed code, right? Do we know what it looks like now? No!

You have a problem, right? Do we know what it is? No! All you said was "not what I was looking for". Response - Then make it give you what you're looking for. You are in control of the computer.

If that's not good enough, what might be some good things to tell us? What would your instructor ask if you said that? Well, assume we will ask the same questions and just give us the info. Don't make us ask -- it'll take days to fix a problem you should be able to fix in 10 minutes.

So do you.

C'mon, man! You changed code, right? Do we know what it looks like now? No!

You have a problem, right? Do we know what it is? No! All you said was "not what I was looking for". Response - Then make it give you what you're looking for. You are in control of the computer.

If that's not good enough, what might be some good things to tell us? What would your instructor ask if you said that? Well, assume we will ask the same questions and just give us the info. Don't make us ask -- it'll take days to fix a problem you should be able to fix in 10 minutes.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

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 species1);
void bubbleSort(birds list[],  int length);
int findHighest();

/////////////////////////////////////////////////////////

int main()
{
    int scores[ARRAY_SIZE];
    int number;          
    int anArray,size;
    birds s;
    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;
    system("pause");
    
    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;
    system("pause");
    
    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;
    system("pause");
    
    if ( !inFile)
    {
         cout << "invalid file name \n";
         system ("pause");
         return 1;
    }     
    }
//////////////////////////////////////////////////// 
 
    if(number > 3)
    {
    
         cout << "You have entered a number out of range."
              << "\nNext time you run this program please ent,er a lower number,"
              << "\npreferably numbers 1-3." << endl << endl;
              
         system ("pause");
         return 0;
    }
////////////////////////////////////////////////////       
     
    birds species1;
    
    //MAKE AN ARRAY OF OBJECTS
    birds total[ARRAY_SIZE];
    
    //INITIALIZE THE ARRAY OF OBJECTS TO KNOWN VALUES.
    initialize(total, ARRAY_SIZE);
    
    //PRINT OUT THE ARRAY OF OBJECTS
    print(total, ARRAY_SIZE);
    
   
    system("PAUSE");
    system("cls");
    int sum = 0;
             
    for(int species = 0; species < ARRAY_SIZE && !inFile.eof(); species++)
    {
        species1 = getBirds(inFile);
        print(species1);
        total[species]=species1;
        sum = sum + species1.number;     
    }
    cout << "\nThere are " << sum << " many birds in this list" << endl;
    
    system("PAUSE");
    system("cls");
    bubbleSort(total, ARRAY_SIZE); 
    print(total, ARRAY_SIZE);
    
    /*findHighest();  */ 
  
    cout <<"\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}
/////////////////////////////////////////////////
// take an array of players, initialize each object to some
//predefined state,.... 

void initialize (birds species[], int number )
{
     for(int count =0; count < number; count++)
       {
           species[count].species = "Type";          
           species[count].number = 0;
       }
}
//////////////////////////////////////////
void print(const birds species[], int number)
{
     int count = 0;
         for(int count =0; count < number; count++)
       {
           cout <<"Birds: "<<count;      
           cout <<"\tSpecies: "<< species[count].species;  
           cout << "\t\tNumber: "<< species[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;    
          inFile >> temp.number;
       
       return temp; 
}
//
//////////////////////////////////////////
void print(birds species1)
{  
           char cityName[ARRAY_SIZE];
           int count = 0;
           cout <<"Species: "<< species1.species;
           
           cout << setw(5) <<"Number: " << species1.number << endl;
}
//////////////////////////////////////////

void bubbleSort(birds list[], int length)
{
    birds temp;
    int iteration;
    int index;
    int count = 0;
    fstream inFile;

    for (iteration = 1; iteration < length; iteration++)
    {
        for (index = 0; index < length - iteration && !inFile.eof(); index++)
            if (list[index].number > list[index + 1].number) 
            {
                temp = list[index];
                list[index] = list[index + 1];
                list[index + 1] = temp;
            }
    }
}

Here is my code, but where I have the sum it gives me the wrong number. Would you suggest something different?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.