0

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;
}
Martin G
  • 17,357
  • 9
  • 82
  • 98
RocketKatz
  • 39
  • 1
  • 9
  • What does your mycountrydata.txt file look like? It looks like you are trying to scan for user input manually instead of reading a file. Can you fix indentation on your code? – ryanlutgen Feb 10 '15 at 04:21
  • This is my file: China, 1,357,380,000, 9,596,961 France, 66,616,416, 640,679 United States, 320,206,000, 9,857,306 South Korea, 51,302,044. 100,210 Japan. 126,434,964, 377,944 – RocketKatz Feb 10 '15 at 04:26
  • Yes, I'm trying to read from a file, but I don't know what to use to get the input from the external file. Would it be something like infile>> or home.getName, etc. Also, my understanding of using classes into the main program is lacking. – RocketKatz Feb 10 '15 at 04:31

2 Answers2

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.

Community
  • 1
  • 1
Pradhan
  • 16,391
  • 3
  • 44
  • 59
0

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.