473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Inputting data on screen from File

namcintosh
First of all, here is my program:


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio>
  3. #include <fstream>              //Needed to use files
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int main()
  12. {
  13.        ifstream inFile;
  14.        int people;
  15.  
  16.  
  17.        inFile.open("people.dat");
  18.  
  19.        inFile >> people;
  20.        cout << people <<endl;
  21.  
  22.        inFile >> people;
  23.        cout <<people <<endl;
  24.  
  25.        inFile >>people;
  26.        cout <<people <<endl;
  27.  
  28.        inFile >>people;
  29.        cout <<people <<endl;
  30.  
  31.        inFile >>people;
  32.        cout << people <<endl;
  33.  
  34.        inFile >>people;
  35.        cout <<people <<endl; 
  36.  
  37.        inFile.close();
  38.  
  39.        getch();
  40.        return 0;
  41.  
  42. }
Here is my problem:
This program should produce a bar chart showing the population growth for a small town at 20 year intervals during the past 100 years (the years are from 1900-2000). This program should read in the population figures from a file (which is people.dat). For each year, it shoudl display the date and a bar consisting of one asterdisk for each 1000 people. For example:

1900 **
1920 ****
1940 *****

Here is the data from the file.
2000
4000
5000
9000
14000
18000

I am having trouble figuring how to read the file to get it to print out a star for each 1000 years.

Does anyone have any idea how to do that?
Apr 17 '07 #1
6 3245
ilikepython
844 Expert 512MB
First of all, here is my program:


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio>
  3. #include <fstream>              //Needed to use files
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int main()
  12. {
  13.        ifstream inFile;
  14.        int people;
  15.  
  16.  
  17.        inFile.open("people.dat");
  18.  
  19.        inFile >> people;
  20.        cout << people <<endl;
  21.  
  22.        inFile >> people;
  23.        cout <<people <<endl;
  24.  
  25.        inFile >>people;
  26.        cout <<people <<endl;
  27.  
  28.        inFile >>people;
  29.        cout <<people <<endl;
  30.  
  31.        inFile >>people;
  32.        cout << people <<endl;
  33.  
  34.        inFile >>people;
  35.        cout <<people <<endl; 
  36.  
  37.        inFile.close();
  38.  
  39.        getch();
  40.        return 0;
  41.  
  42. }
Here is my problem:
This program should produce a bar chart showing the population growth for a small town at 20 year intervals during the past 100 years (the years are from 1900-2000). This program should read in the population figures from a file (which is people.dat). For each year, it shoudl display the date and a bar consisting of one asterdisk for each 1000 people. For example:

1900 **
1920 ****
1940 *****

Here is the data from the file.
2000
4000
5000
9000
14000
18000

I am having trouble figuring how to read the file to get it to print out a star for each 1000 years.

Does anyone have any idea how to do that?
You can divide the number in the file by 1000 and then print that many stars like this:
Expand|Select|Wrap|Line Numbers
  1. get number from file;
  2. int num_of_stars = people / 1000;
  3. for (the value of num_of_stars){
  4.     print an asterik}
  5.  
Hope that helps.
Apr 17 '07 #2
You can divide the number in the file by 1000 and then print that many stars like this:
Expand|Select|Wrap|Line Numbers
  1. get number from file;
  2. int num_of_stars = people / 1000;
  3. for (the value of num_of_stars){
  4.     print an asterik}
  5.  
Hope that helps.

So, where should that code go in the program?
Apr 19 '07 #3
ilikepython
844 Expert 512MB
So, where should that code go in the program?
Before you print the number and after you read it in from the file. Then you can print out the number of stars depending on the number divided by 1000.
Apr 19 '07 #4
Before you print the number and after you read it in from the file. Then you can print out the number of stars depending on the number divided by 1000.
Can you point that out to me in my program please???
Apr 19 '07 #5
ilikepython
844 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio>
  3. #include <fstream>              //Needed to use files
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int main()
  12. {
  13.        ifstream inFile;
  14.        int people;
  15.  
  16.  
  17.        inFile.open("people.dat");
  18.  
  19.        inFile >> people;
  20.        people /= 1000;
  21.        for (int x = 0; x < people; x++){
  22.              cout << "*";}
  23.        cout << endl;
  24.  
  25.        inFile >> people;
  26.        cout <<people <<endl;
  27.  
  28.        inFile >>people;
  29.        cout <<people <<endl;
  30.  
  31.        inFile >>people;
  32.        cout <<people <<endl;
  33.  
  34.        inFile >>people;
  35.        cout << people <<endl;
  36.  
  37.        inFile >>people;
  38.        cout <<people <<endl; 
  39.  
  40.        inFile.close();
  41.  
  42.        getch();
  43.        return 0;
  44.  
  45. }
See the bolded statements, that prints x number of stars where x is equal to the number from the file divided by 1000(one star for every 1000 in the number). Add that to the other statments after that to do that for each one. If you have more questions just ask.
Apr 20 '07 #6
See the bolded statements, that prints x number of stars where x is equal to the number from the file divided by 1000(one star for every 1000 in the number). Add that to the other statments after that to do that for each one. If you have more questions just ask.

Thanks so much, I got it!!! I also figured out how to put the year in beside the stars.
Apr 20 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Angus Lepper | last post by:
I'm writing a stock ticker for a stock market simulation, and can load the data into the xmlreader in the first place, but can't figure out how to refresh/update the data in it. Any ideas? Code:...
4
by: snow.carriers | last post by:
http://www.rafb.net/paste/results/fTQgRW16.html Here's my program so far. This is what I'm trying to do: http://contest-cemc.uwaterloo.ca/ccc/2005/senior/phone.pdf So far it works fine. The only...
2
by: saltedcoffee | last post by:
hey, I am beginer to C++ programing. I am working on a project called the "maze Problem" First of all I am required to read a text file in( which is the maze) and store it into a 2 dimentional...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
1
by: Ramper | last post by:
Have a .txt document as: String int int int int int int int
3
by: james121285 | last post by:
I have been trying this program for ages and am not getting very far. I am trying to input data from a seperate file and use it to work out the max and min values of the data. I have done the second...
2
by: UofFprogrammer | last post by:
Hello, Several Weeks ago I asked a question about testing for the end of an input file. I have been using this method pretty well for inputting information from an external file. I am using C++. ...
5
by: tim123 | last post by:
Hey. I'm having some issues reading in data from a tab delimited text file. I only want to input numbers, and ideally I'd like it to be able to cope with a line or 2 of text at the top in case...
1
by: shadowofanubis66 | last post by:
Basically, the project I'm working on this week is to make a school lunch menu with a GUI, when the person types in a day, the menu for that day shows up. We have two files, ones a day off text file...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.