简体   繁体   中英

How to pass array of input from an external file into main program?

I am new to C++ and i'm having trouble with my program using classes and inputfile to display my input in the output. How should I display the country, population and area? I'm getting error messages like:

Line 82 [Error] invalid use of 'Country::Country'

Line 89 [Error] invalid types 'long int[int]' for array subscript

Line 93 [Error] invalid types 'double[int]' for array subscript

Here is what I have so far:

#include <iostream> 
#include <string>
#include <fstream>

using namespace std;

class Country
{

    private:
        string name;
        long int population;
        double area;

    public:
        Country();
        Country(string, long, double);
        void setName(string);
        void setPopulation(long);
        void setArea(double);
        string getName();
        long getPopulation();
        double getArea();

};

Country::Country(){
  name="?";
  population=0;
  area=0;
}

Country::Country(string name1, long population1, double area1){
  name=name1;
  population=population1;
  area=area1;
}

void Country::setName(string name1){
  name=name1;
}

void Country::setPopulation(long population1){
  if(population1>=0.0)
    population=population1;
  else{
    population1=0.0;
    cout<< "Invalid number. Setting population to 0."<<endl;
  }
}

void Country::setArea(double area1)
{
  if(area1>=0.0)
    area=area1;
  else{
    area1=0.0;
    cout<< "Invalid number. Setting area to 0."<<endl;
  }
}

string Country::getName(){
  return name;
}

long Country::getPopulation(){
  return population;
}

double Country::getArea(){
  return area;
}

int main(){

  Country home;
  const int H=5;
  string homename="";
  long homepopulation=0;
  double homearea=0;
  ifstream infile("mycountrydata.txt");

  home.setName(homename);
  home.setPopulation(homepopulation);
  home.setArea(homearea);
  home.Country(homename, homepopulation, homearea);
  for(int i=0; i<H; i++){

    cout<<"Enter the country's name: ";
    infile>>homename[i];
    cout<<endl;
    cout<<"Enter the country's population: ";
    infile>>homepopulation[i];
    cout<<endl;
    cout<<"Enter the country's area: ";
    cout<<endl;
    infile>>homearea[i];

  }
  infile.close();
  return 0;
}

A constructor is a special member function which cannot be directly invoked this way:

home.Country(homename, homepopulation, homearea);

long 's don't have the [] operator defined and hence you can't do:

infile>>homepopulation[i];

since earlier you declare long homepopulation . The same explanation holds for the error in

infile>>homearea[i];

These are answers addressing the exact errors in your code, but it isn't a substitute for a good teaching resource. See this answer for some useful material.

country is a constructor and it can be invoked by giving the below statement in the beginning of the main() replacing country home;

country home(homename, homepopulation, homearea); 

I guess you want to use homepopulation and homearea as arrays but you declared them as normal variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM