Study this:
Code:
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
int main (void)
{
int i;
const int numberFish = 3;
//declare arrays
int Length[numberFish];
int Weight[numberFish];
string Species[numberFish];
//get input
for (i = 0; i < numberFish; i++)
{
cout<<"Enter species:"<<endl;
getline(cin, Species[i]); // Read a line into a string
cin.ignore(); // Quikie clean up
cout<<"Enter weight in ounces:"<<endl;
cin>>Weight[i];
cin.ignore(); // Quikie clean up
cout<<"Enter length in inches:"<<endl;
cin>>Length[i];
cin.ignore(); // Quikie clean up
}
for (i = 0; i < numberFish; i++)
{
cout<<"Species: "<< Species[i] <<endl;
cout<<"Weight: "<< Weight[i] <<endl;
cout<<"Length: "<< Length[i] <<endl;
}
}
Alternatively, you could group everything together in a struct and then dynamically allocate an array of objects. This would make things considerably easier and you could define numberFish without bending over backward:
Code:
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
struct Fish
{
string Name;
int Length;
int Weight;
};
int main (void)
{
int i;
int numberFish;
cout<<"Enter the number of fish: ";
cin>> numberFish;
cin.ignore();
Fish *Species = new Fish[numberFish];
//get input
for (i = 0; i < numberFish; i++)
{
cout<<"Enter species:"<<endl;
getline(cin, Species[i].Name); // Read a line into a string
cin.ignore(); // Quikie clean up
cout<<"Enter weight in ounces:"<<endl;
cin>> Species[i].Weight;
cin.ignore(); // Quikie clean up
cout<<"Enter length in inches:"<<endl;
cin>> Species[i].Length;
cin.ignore(); // Quikie clean up
}
for (i = 0; i < numberFish; i++)
{
cout<<"Species: "<< Species[i].Name <<endl;
cout<<"Weight: "<< Species[i].Weight <<endl;
cout<<"Length: "<< Species[i].Length <<endl;
}
}
-Prelude