I am having trouble displaying my code that is supposed to be read in from "pgm6.txt" - it looks like it is a continuous loop, but I'm not seeing how/why/where. It's probably something oobvious that I'm just overlooking.
Here is a sample of the text in my external file:
Newland
100000
8000
6000
China
1306042971
19882696
9190809
Croatia
4489000
43000
53000
India
1082144298
27947680
9356253
A sample output is:
Country Name:
Country Population: 100000
Number of Births: 8000
Number of Deaths: 6000
It just repeats, over, and over, and over...and doesn't display my name string.
Any help is appreciated. Thanks!
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
class Country
{
private:
string name;
int pop;
int births;
int deaths;
public:
void setName(string na)
{name = na;}
void setPop(int po)
{pop = po;}
void setBirths(int bi)
{births = bi;}
void setDeaths(int de)
{deaths = de;}
string getName()
{return name;}
int getPop()
{return pop;}
int getBirths()
{return births;}
int getDeaths()
{return deaths;}
};
int main ()
{
Country info;
int newPop, infoPop, infoBirths, infoDeaths;
string infoName;
newPop = 0;
infoPop = 0;
infoBirths = 0;
infoDeaths = 0;
ifstream infile;
infile.open("pgm6.txt");
cout << "************************************************" << endl;
getline (infile, infoName);
infile >> infoPop;
infile >> infoBirths;
infile >> infoDeaths;
while (!infile.eof())
{
info.setName(infoName);
info.setPop(infoPop);
info.setBirths(infoBirths);
info.setDeaths(infoDeaths);
cout << left << "Country Name: " << setw(20) << info.getName() << endl;
cout << left << "Population: " << setw(15) << info.getPop() << endl;
cout << left << "Number of Births: " << setw(15) << info.getBirths() << endl;
//cout << left << "Birth Rate: " << Setw(15) << << endl;
cout << left << "Number of Deaths: " << setw(15) << info.getDeaths() << endl;
//cout << left << "Death Rate: " << setw(15) << << endl;
//cout << left << "New Population: " << setw(15) << << endl;
//cout << left << "Percent Growth: " << setw(15) << << endl;
cout << "**************************************************" << endl;
getline (infile, infoName);
infile >> infoPop;
infile >> infoBirths;
infile >> infoDeaths;
}
infile.close();
return 0;
}
Inline Code Example Here