Array addressing and testing

I am trying to make a program that takes the name of a restaurant and the ratings for that restaurant (out of 5 stars) and displays how many people gave the restaurant a certain amount of ratings (1, 2, 3, 4, or 5 stars). The data is stored in a text file.

input file:
Dominion Sushi
5 4 3 4 5 5 3 4 5 4 2 4 5 4 5 4 4 3 5 5 5 4 5 4 5 3 4 4 1 5

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//following script is incomplete
#include <iostream>
#include <fstream>


using namespace std;


int main()
{
    string restname;
    char ratings[60];
    int ones, twos, threes, fours, fives;

    ones = 0;
    twos = 0;
    threes = 0;
    fours = 0;
    fives = 0;

    //declare and open input files
    fstream inFile;
    inFile.open("restaurant.txt");

    //check to see if file is open
    if (!inFile.is_open())
    {
        cout << "Unable to open file";

    }//end check

    getline(inFile, restname);

    cout << "*****************************\n" << restname
         << "\n*****************************\n" << endl;

    inFile >> ratings[0] >> ratings[1] >> ratings[2] >> ratings[3] >> ratings[4] >> ratings[5] >>
     ratings[6] >> ratings[7] >> ratings[8] >> ratings[9] >> ratings[10] >> ratings[11] >> ratings[12] >>
      ratings[13] >> ratings[14] >> ratings[15] >> ratings[16] >> ratings[17] >> ratings[18] >> ratings[19] >>
       ratings[20] >> ratings[21] >> ratings[22] >> ratings[23] >> ratings[24] >> ratings[25] >> ratings[26] >>
        ratings[27] >> ratings[28] >> ratings[29];


        if (ratings[28] == '1')
        {
            ones = ones + 1;
        }







    //display answers
    cout << "Number of customers rated 1 = " << ones << endl;
    cout << "Number of customers rated 2 = " << twos << endl;
    cout << "Number of customers rated 3 = " << threes << endl;
    cout << "Number of customers rated 4 = " << fours << endl;
    cout << "Number of customers rated 5 = " << fives << endl;





    return 0;
}


I cannot figure out an efficient way to check each array index for each rating with out writing 145 tests like the one example already in my script. I thought there was a way to address each index in the array individually without actually having to address each index (array[1], array[2], ...)

Any input is greatly appreciated.
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
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::string restname;
    int stars[5];

    std::fstream inFile( "restaurant.txt" );

    if( ! inFile )
        std::cout << "Unable to open file.\n";

    std::getline( inFile, restname );

    while( inFile )
    {
        int tmp;
        inFile >> tmp;
        if( tmp >= 1 && tmp <= 5 )
            ++ stars[tmp-1];
    }

    for( int i = 0; i < 5; ++i )
        std::cout << "Number of customers rated " << i+1 << " = " << stars[i] << '\n';
}
> I thought there was a way to address each index in the array individually
> without actually having to address each index (array[1], array[2], ...)

Write a loop. For example: for( int i = 1 ; i < N ; ++i ) { array[i] ...


For this program, we don't need an array for the ratings
(we can process each rating as it is read without having to store it in an array)

An array to hold the counts (ones, twos, threes etc.) would make the program shorter.

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
47
48
49
50
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    const std::string file_name = "restaurant.txt" ;

    if( std::ifstream file{file_name} ) // if the file was opened for input
    {
        std::string restname ;
        if( std::getline( file, restname ) ) // if the name was read
        {
            constexpr int NUM_STARS = 5 ;

            // array to hold counts of stars ones, twos etc. eg.star_counts[3] == threes
            // (there are no zero stars, so we do not use star_counts[0])
            int star_counts[ NUM_STARS + 1 ] {} ; // initialise to all zeroes

            char rating ; // a rating read from the file
            while( file >> rating ) // for each rating read from the file
            {
                if( rating >= '1' || rating <= '5' ) // if it is a valid rating (one to five) 
                {
                    // update the star count. we exploit the fact that '3' - '0' == 3 etc.
                    const int nstars = rating - '0' ;
                    ++star_counts[nstars] ;
                }

                else 
                { 
                    // invalid characters for ratings like 'x', '9' etc are discarded; we just ignore them right now
                    // we could add code to print an appropriate error message here 
                }
            }

            // display answers
            std::cout << "restaurant: " << restname << '\n' ;
            for( int nstars = 1 ; nstars <= NUM_STARS ; ++nstars )
                std::cout << "Number of customers who rated " << nstars << " == " << star_counts[nstars] << '\n' ;
        }

    }

    else
    {
        std::cerr << "unable to open file " << file_name << '\n' ;
        return 1 ;
    }
}
Last edited on
Topic archived. No new replies allowed.