| | |
Help with read file program to round numbers pt. 2
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 26
Reputation:
Solved Threads: 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):
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)
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; }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Dec 2008
Posts: 26
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Sep 2008
Posts: 90
Reputation:
Solved Threads: 12
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:
An example to how you could write it:
c++ Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
- need help with assembler program (Assembly)
- Uncompress (C)
- Finding length (Java)
Other Threads in the C++ Forum
- Previous Thread: capital letter
- Next Thread: functions...
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






