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;
}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;
}

Recommended Answers

All 4 Replies

It also might help if I tell you guys the whole program (*smh*...lol):

It also might help to tell us what the problem is and what you're trying to accomplish.

If this is the same problem as another thread, you should have just continued there.

Sorry, I'm new to this. I had already marked the thread solved before I realized that I didn't include all of the information needed to complete the program. I tried to go back into and add on the other thread but it didn't seem like it was accepting any new posts(the post count never changed). I believe I posted this same information in my other thread.

The problem is that I need to create a program that reads the information from a file, displays that information and then rounds the numbers either up or down based on the number itself. If the number ends in 500+ it rounds up, less than 500 rounds down.

I have everything except for the rounding part.

So, you actually want to round up when (the number) mod 1000 is equal or greater than 500, and down if (the number) mod 1000 is less than 500.
An example to how you could write it:

#include<iostream>
using namespace std;

int main(void)
{
int x;

cout<<"Enter a number to round: ";cin>>x;
cout<<"The rounded number is ";
if(x%1000<500) x-=x%1000;
else x+=1000-x%1000;
cout<<x<<"\n";
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.