Help with read file program to round numbers pt. 2

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Help with read file program to round numbers pt. 2

 
0
  #1
Dec 13th, 2008
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):


  1. int main()
  2. {
  3. int people;
  4. ifstream inputFile;
  5.  
  6. cout << "This program displays population by year.";
  7. cout << endl;
  8. cout << endl;
  9.  
  10. inputFile.open("people.dat");
  11.  
  12. if (!inputFile)
  13. cout << "Error opening file." << endl;
  14. else
  15. {
  16.  
  17. for (int year = 1900; year < 2001; year += 20)
  18. {
  19. inputFile >> people;
  20. cout << year << " ";
  21. for (int star = 0; star < people / 1000; star++)
  22. cout << '*';
  23. cout << endl;
  24. cout << endl;
  25. }
  26. }
  27. inputFile.close();
  28.  
  29. inputFile.open("people.dat");
  30.  
  31. cout << "Year" << setw(20) << "Actual Population";
  32. cout << setw(22) << "Charted Population" << endl;
  33. cout << "----------------------------------------------";
  34. cout << endl;
  35. for (int year = 1900; year < 2001; year += 20)
  36. {
  37. inputFile >> people;
  38. cout << year;
  39. cout << setw(20) << people;
  40. cout << setw(22) << people;
  41. cout << endl;
  42. }
  43.  
  44. system ("pause");
  45. return 0;
  46. }int main()
  47. {
  48. int people;
  49. ifstream inputFile;
  50.  
  51. cout << "This program displays population by year.";
  52. cout << endl;
  53. cout << endl;
  54.  
  55. inputFile.open("people.dat");
  56.  
  57. if (!inputFile)
  58. cout << "Error opening file." << endl;
  59. else
  60. {
  61.  
  62. for (int year = 1900; year < 2001; year += 20)
  63. {
  64. inputFile >> people;
  65. cout << year << " ";
  66. for (int star = 0; star < people / 1000; star++)
  67. cout << '*';
  68. cout << endl;
  69. cout << endl;
  70. }
  71. }
  72. inputFile.close();
  73.  
  74. inputFile.open("people.dat");
  75.  
  76. cout << "Year" << setw(20) << "Actual Population";
  77. cout << setw(22) << "Charted Population" << endl;
  78. cout << "----------------------------------------------";
  79. cout << endl;
  80. for (int year = 1900; year < 2001; year += 20)
  81. {
  82. inputFile >> people;
  83. cout << year;
  84. cout << setw(20) << people;
  85. cout << setw(22) << people;
  86. cout << endl;
  87. }
  88.  
  89. system ("pause");
  90. return 0;
  91. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Help with read file program to round numbers pt. 2

 
0
  #2
Dec 14th, 2008
Originally Posted by meistrizy View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Re: Help with read file program to round numbers pt. 2

 
0
  #3
Dec 14th, 2008
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.
Last edited by meistrizy; Dec 14th, 2008 at 12:52 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: Help with read file program to round numbers pt. 2

 
0
  #4
Dec 14th, 2008
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:

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main(void)
  5. {
  6. int x;
  7.  
  8. cout<<"Enter a number to round: ";cin>>x;
  9. cout<<"The rounded number is ";
  10. if(x%1000<500) x-=x%1000;
  11. else x+=1000-x%1000;
  12. cout<<x<<"\n";
  13. system("pause");
  14. return 0;
  15. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 26
Reputation: meistrizy is an unknown quantity at this point 
Solved Threads: 0
meistrizy meistrizy is offline Offline
Light Poster

Re: Help with read file program to round numbers pt. 2

 
0
  #5
Dec 14th, 2008
Thanks!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 636 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC