943,548 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 792
  • C++ RSS
Dec 13th, 2008
0

Help with read file program to round numbers pt. 2

Expand Post »
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):


C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
meistrizy is offline Offline
33 posts
since Dec 2008
Dec 14th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by meistrizy ...
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.
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006
Dec 14th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
meistrizy is offline Offline
33 posts
since Dec 2008
Dec 14th, 2008
0

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

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:

c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Dec 14th, 2008
0

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

Thanks!!
Reputation Points: 10
Solved Threads: 0
Light Poster
meistrizy is offline Offline
33 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: capital letter
Next Thread in C++ Forum Timeline: functions...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC