Array and inFile problem

I have been tasked with fixing this code below. You can already see the changes I made with my notes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream> //added fstream header

using namespace std;

int main()
{
    int cars[10];
    int sum = 0;
    int max;
    int salesPerson;

    fstream inFile;  //declared input file
    inFile.open("cars.txt");

    for (int j = 0; j < 10; j++)
    {
        inFile >> cars[j];
    }

    cout << "The total number of cars sold = " << sum << endl;

    max = cars[0];
    salesPerson = 1;

    for (int j = 1; j < 10; j++)
        if (max > cars[j])
        {
           max = cars[j];
           salesPerson = j;
        }


    cout << "The salesperson selling the maximum number of cars is salesperson "
         << salesPerson << endl;

    cout << "Salesperson " << salesPerson << " sold "
         << max << " cars last month." << endl;


    cout << endl;

    inFile.close();

    return 0;
}


The program is supposed to take the number of car sales from 10 salesmen from an input file and return the total cars sold, who sold the most, and how many they sold. See below.

cars.txt:
5 6 11 8 3 4 1 7 2 5

Sample Run:
The total number of cars sold = 52
The salesperson selling the maximum number of cars is salesperson 3
Salesperson 3 sold 11 cars last month.

Given I am a beginner and there are several problems with this script, I suppose I should take it one problem at a time. My first problem is how do you input the 10 integer values from the file individually? I appreciate any and all input.
Hello WakelessFoil,

First question is how do you know the file is open?

The for loop assumes that there are 10 people with sales. Not a good idea to assume this.

1
2
for (int idx = 0; idx < 10 && inFile >> cars[idx]; idx++)
    sum += cars[idx];


I would start with checking that the file is open before you try to read it and then work on the for loop to read and store what is in the file.

Until this part is working there is no point of the rest of the progam until you have something to work with.

The rest of the program look like it should work, but I will know more when I test it.

Andy
Hello WakelessFoil,

I made 1 change in the 2nd for loop from
if (max > cars[j])
to
if (cars[j] > max) // <--- Sometimes order makes a big difference.

Andy

Edit: typo.
Last edited on
You don't need the array or 2 loops. The sum and finding the max values can be done within one loop without knowing how many items there are.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	fstream inFile("cars.txt");

	if (!inFile.is_open()) {
		cout << "Cannot open the file\n";
		return 1;
	}

	int sum = 0;
	int max = 0;
	int salesPerson = 0;

	for (int s = 1, n = 0; inFile >> n; ++s) {
		sum += n;

		if (n > max) {
			max = n;
			salesPerson = s;
		}
	}

	cout << "The total number of cars sold = " << sum << endl;

	cout << "The salesperson selling the maximum number of cars is salesperson "
		<< salesPerson << endl;

	cout << "Salesperson " << salesPerson << " sold "
		<< max << " cars last month." << endl;

	inFile.close();
}



The total number of cars sold = 52
The salesperson selling the maximum number of cars is salesperson 3
Salesperson 3 sold 11 cars last month.


Topic archived. No new replies allowed.