Cant figure out how to let the user modify the counts.

After the data is in the array, prompt the user to modify the array by inputting a species name and a count. If the species is not in the array, print a message indicating this and add the species to the end of the array. If the species is in the array, change the count in the array to the new count. Allow the user to input any number of changes.

This is what I have so far:

//Description of program:Manages a
//list of bird species and counts
//stored in an array of structs.


#include <fstream>

#include <algorithm>
#include <stdio.h>
#include <string>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <cstdlib>

using namespace std;

#define ARRAY_SIZE 200

typedef struct { char species [200];
int count ;
} BIRDS;

BIRDS birds[ARRAY_SIZE];


void selectionSort(BIRDS birds[], int n)
{
int p = 0 ; // pass number
int small = 0; // smallest
BIRDS tempBird;
int i;

for (p=0; p<n; p++) {
small = p;

for (i=p+1; i<n; i++) {
if (strcmp(birds[i].species, birds[small].species) < 0) {
small = i;
}
}
tempBird = birds[p];
birds[p] = birds[small];
birds[small] = tempBird;
}
}

int main () {
///////Declaring Variables/////////
char File_name [200];
char city_name [200];
int i;
int Total_numBirds = 0;
int j;
int min;
int max;
int maxIndex;
string state;
FILE * fptr;




//Ask the user for a file name.////////
cout<<"Enter the name of a text file: ";
cin>>File_name;
cout<<endl;

cout<<"The name of the file is: " << File_name;
cout<<endl<<endl;


/////////////////////////////////////////////
////////LIST THE CITY///////////////////////
///////////////////////////////////////////



fptr= fopen(File_name, "r");

if (fptr == NULL) {
cout<<"The file "<<File_name<<" was not found."<< endl;
return 1;
}


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

fgets (city_name, sizeof(city_name), fptr);
i=0;
while (!feof(fptr)) {
fscanf (fptr, "%s %d\n", birds[i].species, &birds[i].count);
cout<<i<<"species: "<< birds[i].species<< " count: "<< birds[i].count << endl;
i++;
}
fclose (fptr);

//Prompt user to make changes to a species and count/////////
int New_count;

cout<<"###############################################"<<endl;
cout<<"\nEnter a species: ";
cin>> birds[i].species;
cout<<endl;
cout<<"Enter a new count: ";
cin>> New_count;

cout<<endl<<endl;

//////////////////////////
fptr= fopen(File_name, "r");

if (fptr == NULL) {
cout<<"The file "<<File_name<<" was not found."<< endl;
return 1;
}


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

fgets (city_name, sizeof(city_name), fptr);
i=0;
while (!feof(fptr)) {
fscanf (fptr, "%s %d\n", birds[i].species, &birds[i].count);
cout<<i<<"species: "<< birds[i].species<< " count: "<< birds[i].count << endl;
i++;
}
fclose (fptr);




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




//////////////MAKE FUNCTION TO ADD THE NEW COUNT TO THE TEXT HERE//////////////////////
/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////




//sorts the species of birds alphabetically//////
selectionSort(birds, i);


//compute total number of birds///////
Total_numBirds = 0;

for (j=0; j<i; j++) {
Total_numBirds += birds[j].count;
}

cout<<"###############################################"<<endl;
cout<<endl;
cout<<"The total number of birds for this city is: "<<Total_numBirds;
cout<<endl<<endl;



//find the species with the most sightings///////

maxIndex = 0;
max = 0;

for (j=0; j<i; j++) {
if (birds[j].count > max) {
max = birds[j].count;
maxIndex = j;
}
}
cout << "The species with the most amount of birds is: " << birds[maxIndex].species;
cout<<endl<<endl;







//displays the birds within a range of sigtings//////////

cout << "Enter a minimum value for a range: ";
cin >> min;
cout<<endl;
cout << "Enter a maximum value for a range: ";
cin >> max;
cout<<endl;

for (j=0; j<i; j++) {
if ((birds[j].count <= max) && (birds[j].count >= min)) {
cout << "species: " << birds[j].species << " count: " << birds[j].count << endl;
}
}




//print out the birds to an output file///////////
fptr = fopen("output.txt", "w");

for (j=0; j<i; j++) {
fprintf (fptr, "%-40s \t %d\n", birds[j].species, birds[j].count);
}

fclose(fptr);
}

Ok. So a lot here you need to look at.

Do you really have you use static sized arrays for this?
Are you allowed to use std::string?
Do you have to use C style file IO vs C++ IO streams?

Because you're making the solution to this problem WAY more complicated than is should be. (e.g a std::map would auto-sort, auto-insert and allow easy modification of object counts.
I don't have to use anything specific for the project I just spent a lot of time on these forums and threw this together and yes I believe I am allowed to use std::string
Ok Then.
I'd look at easier ways to write them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <map>
#include <string>

using namespace std:

int main() {
// Automatically store as many as you want, and they are alphabetical
// Any new addition defaults to 0 count
map<string, unsigned> species;

// To add 1 to the species count for frog new ones
species["frog"]++;
// To add 2
species["frog"] += 2;

species["fish"] = 6;
species["zerba"] = 10;
species["a something starting with a"]+= 5;

// To print the map
map<string, unsigned>::iterator iter;
for (iter = species.begin(); iter != species.end(); ++iter)
 cout << "Species: " << iter->first << " = " << iter->second << endl;

 return 0;
}

Last edited on
Topic archived. No new replies allowed.