Ok...the program is supposed to read data from the file and then output one star for every 1000 people. At the bottom of the program, it is supposed to round the numbers up or down (2500 = 3 stars, 2499 = 2 stars) but I can't get that part to work. Please help! Thanks in advance.

int main()
{
    int people;
    ifstream inputFile;
    
    
    inputFile.open("people.dat");
    cout << "Reading information from the file.";
    cout << endl;
        
    
    if (!inputFile)
     cout << "Error opening file." << endl;
    else 
    {
         for (int year = 1900; year < 2001; year += 20)
         { 
             inputFile >> people;
             cout << year << "  " ; 
                      
                    for (int star = 0; star < people / 1000; star++)                             
                        cout << '*';           
                        cout << endl;
                        
                    
         }
         
         while (people % 1000 < 500)
         {
               int star = 1;
               cout << '*';                                             
         }                  
         inputFile.close();
    }    
    cout << endl;
    
    
    system ("pause");
    return 0;
}
while (people % 1000 < 500)
         {
               int star = 1;
               cout << '*';                                             
         }

Think about what this bit is really doing. First, you want to print a final star if the population number was > x500. Is that what your code does?

Why a loop? You're just printing one star here, if any. What's the purpose of the star variable? Do you need to store anything?

Lastly, shouldn't this be inside the main for loop, coming after the loop that prints the stars for each full 1000?

See if you can fix this, post your revision.

Try replacing the whole while loop stuff with this.

std::cout<<std::string((people+500)/1000,'*')<<std::endl;

If you don't want to use the string constructor then try...

for(int i=(people+500)/1000;i;i--)
   std::cout<<'*';
std::cout<<std::endl;
while (people % 1000 < 500)
         {
               int star = 1;
               cout << '*';                                             
         }

Think about what this bit is really doing. First, you want to print a final star if the population number was > x500. Is that what your code does?

Why a loop? You're just printing one star here, if any. What's the purpose of the star variable? Do you need to store anything?

Lastly, shouldn't this be inside the main for loop, coming after the loop that prints the stars for each full 1000?

See if you can fix this, post your revision.

Well...at first I had it as an if/else statement in main, and it would kick out an extra line of stars after displaying the years and stars. So I tried to move it down and it was still doing the same thing so I changed it to a while loop and moved it back up into main where it finally stopped printing out an extra line of stars (after I switched the greater than sign), but not the correct number of stars. I will see if I can fix this though. Thanks.

Ok...I got it now. Thanks to both of you. I appreciate it. I was going by the model that my teacher had displayed on the board (which obviously wasn't a very good model to go by). Here is the revision:

int main()
{
    int people;
    ifstream inputFile;
    
    
    inputFile.open("people.dat");
    cout << "Reading information from the file.";
    cout << endl;
        
    
    if (!inputFile)
     cout << "Error opening file." << endl;
    else 
    {
         
         for (int year = 1900; year < 2001; year += 20)
         { 
             inputFile >> people;
             cout << year << "  ";
                   for (int star = (people + 500) / 1000; star; star--)
                      cout << '*';                                         
                      cout << endl;  
                         
         }
         
            
             
         inputFile.close();
    }    
    
   
    
    system ("pause");
    return 0;
}

It also might help if I tell you guys the whole program (*smh*...lol):
Ok...the bottom part of the program is supposed to read the file and then display the rounded number in a table as the charted population. I tried to redo the same piece as StuXYZ suggested but it didn't work, so I just left it. Here is what I have so far (sorry for the mix-up):

int main()
{
    int people;
    ifstream inputFile;
    
    cout << "This program displays population by year.";
    cout << endl;
    cout << endl;

    inputFile.open("people.dat");      
    
    if (!inputFile)
     cout << "Error opening file." << endl;
    else 
    {
         
         for (int year = 1900; year < 2001; year += 20)
         { 
             inputFile >> people;
             cout << year << "  ";
             for (int star = 0; star < people / 1000; star++)
             cout << '*';
             cout << endl;
             cout << endl;                         
         }
    }
         inputFile.close();
         
         inputFile.open("people.dat");
                  
         cout << "Year" << setw(20) << "Actual Population";
         cout << setw(22) << "Charted Population" << endl;
         cout << "----------------------------------------------";
         cout << endl;
         for (int year = 1900; year < 2001; year += 20)
         { 
             inputFile >> people;             
             cout << year;             
             cout << setw(20) << people;
             cout << setw(22) << people;
             cout << endl;     
         }       
    
    system ("pause");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.