string array input problem

This is a discussion on string array input problem within the C++ Programming forums, part of the General Programming Boards category; Why won't this let me input data from the keyboard for the species string array? Code: int main (void) { ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    Exclamation string array input problem

    Why won't this let me input data from the keyboard for the species string array?

    Code:
    int main (void)
    {
    
    
    //declare arrays
    int Length[numberFish];
    int Weight[numberFish];
    string Species[numberFish];
    
    //declare
    int l=0, w=0, s=0, t=0;
    int numberFish=0;
    
    cout<<"Enter number of fish:"<<endl;
    cin>>numberFish;
    
    //get input
    
    for (s=0;s<numberFish;s++;w=0;w<numberFish;w++;l=0;l<numberFish;l++)
    {
    	cout<<"Enter species:"<<endl;
    	cin.getline(Species,s);
    	cout<<"Enter weight in ounces:"<<endl;
    	cin>>Weight[w];
    	cout<<"Enter length in inches:"<<endl;
    	cin>>Length[l];
    }
    return EXIT_SUCCESS;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >for (s=0;s<numberFish;s++;w=0;w<numberFish;w++;l=0;l<n umberFish;l++)
    I have no idea how you could think this would work. It goes against every description and example code ever written for a for loop.

    >cin.getline(Species,s);
    The first argument to getline should be an istream, not an array of strings.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    huh?

    I have no idea what you mean.

    I am trying to get input from the keyboard and put it into an array Species[s]

    the other strings are working ok, but it does not let me enter info for the Species.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    so I changed it to this:

    Code:
    cout<<"Enter number of fish:"<<endl;
    cin>>numberFish;
    //get input
    for (s=0;s<numberFish;s++)
    
    {
    	cout<<"Enter species:"<<endl;
    	cin>>Species[s];
    }
    for (w=0;w<numberFish;w++)
    {
    	cout<<"Enter weight in ounces:"<<endl;
    	cin>>Weight[w];
    	
    }
    for (l=0;l<numberFish;l++)
    cout<<"Enter length in inches:"<<endl;
    	cin>>Length[l];
    return EXIT_SUCCESS;
    }cout<<"Enter number of fish:"<<endl;
    cin>>numberFish;
    //get input
    for (s=0;s<numberFish;s++)
    
    {
    	cout<<"Enter species:"<<endl;
    	cin>>Species[s];
    }
    for (w=0;w<numberFish;w++)
    {
    	cout<<"Enter weight in ounces:"<<endl;
    	cin>>Weight[w];
    	
    }
    for (l=0;l<numberFish;l++)
    cout<<"Enter length in inches:"<<endl;
    	cin>>Length[l];
    return EXIT_SUCCESS;
    }
    and STILL cannot input species from the keyboard!!

    HELP

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    Heare's what I'm supposed to do:

    read in a number of values representing length, species and wight of fish
    read in all values then deermine average weights, minimum and maximun number of fish sampled for each species
    end of data marked by negative length
    output should be in tabular form
    must use arrays

    HERE'S MY CODE SO FAR:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    //Project 4
    //Amy E Peabody
    //March 23, 2003
    
    int main (void)
    {
    
    //declare
    int l=0, w=0, s=0;
    int numberFish=0;
    int aveweight=0, totalweight=0;
    
    
    //declare arrays
    int Length[10];
    int Weight[10];
    string Species[10];
    
    cout<<"Enter number of fish:"<<endl;
    cin>>numberFish;
    
    //get input
    for (l=0;l<numberFish;l++)
    {
    	cout<<"Enter species of each fish, length in inches and weight in ounces:"<<endl;
    	cin>>Species[s]>>Length[l]>>Weight[w];
    	w++;
    	s++;
    }
    
    //total and average weight
    int t;
    for (t=0;t<10;t++)
    {
    	totalweight+=Weight[t];
    	aveweight=totalweight/t;
    }
    
    //max and min lengths
    int maxLength=Length[0];
    int minLength=Length[0];
    for(l=0;l<10;l++)
    {
    	if (Length[l]<maxLength){
    		maxLength=Length[l];
    	}
    	if (Length[l]<minLength){
    		minLength=Length[l];
    	}
    }
    
    
    return EXIT_SUCCESS;
    }

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    //get input
    for (l=0;l<numberFish;l++)
    {
    	cout<<"Enter species of each fish, length in inches and weight in ounces:"<<endl;
    	cin>>Species[s]>>Length[l]>>Weight[w];
    	w++;
    	s++;
    }
    [
    You don't need s and w here. As long as s, l, and w, start out with the same value; s, l, and w are always going to be the same in this loop, so just use one index, not three.

    Code:
    for (t=0;t<10;t++)
    {
    	totalweight+=Weight[t];
    	aveweight=totalweight/t;
    }
    what if numberfish doesn't equal 10?
    why not use numberfish as the denominator for aveweight?
    why calculate aveweight each time through the loop? Wouldn't doing it once after you add up totalweight be sufficient?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 06:07 AM
  2. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 07:04 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  4. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 10:21 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

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