How to input 50 numbers from data file? Arrays

Hi im new programming im having problems with a program i am doing for school here is the question
a group of 50 people have gone apple picking in Pennsylvania. each person has collected a number of apples, in there basket. At the end of the day prizes will be given to some of these apple pickers

write a program including the following:
a. Declare an array to the store 50 numbers of apples
b. input the 50 numbers from data file c:\ apple_trip.txt *** we were giving a bunch of random 50 numbers for this one of my problems is how do i read in all those numbers??
c. count how many of the people picked less than 25 apples?
d. which person collected the least number of apples?
which person collected the most numbers of apples?
e. which person collected the most number of apples?


so far this is all i got

#include <iostream>
#inclue <fstream>
using namespace std;
void main ()
{
int myApples[50];

ifstream read("file.txt");
Last edited on
For problem b, Below is an example code on how to read the numbers on the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//This program reads data from a file.
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
     ifstream inputFile;
     int number{0};

     // Open the file.
     inputFile.open("Whatever.txt");

     // Read the numbers from the file and display them.
     while (inputFile >> number)
      {
           cout << number << endl;
      }

     // Close the file.
     inputFile.close();

     return 0;


For problem c, while the program is reading the file, you could use an if statement to identify the people who picked less than 25 apples and count them (hint: use a counter).

This should give you a head start, before moving to problems d and e.
thank you i got it to work didn't realize how simple it was lol
Last edited on
Good deal!
Topic archived. No new replies allowed.