-Solved- Thanks to JLBorges

This is an assignment to input student grades.
However, apparently my professor want the program to recognize when the user
presses "enter" for the name to conclude that it's finished.
So basically, instead of requiring to have only 5 students grade, how do I change my code to so that it will take however many grades up till "enter" key?
Also is there any other way to make the output look like a table instead of my easy way lol?
Thanks,

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
#include <iostream>
#include <string>
using namespace std;
void getData(int sizeArray, string name[], float score[]);


int main()
{
	const int SIZE = 5;
	string name[SIZE];
	float score[SIZE];

	getData(SIZE, name, score);

	cout << "Name             Grade" << endl;
	cout << "---------        --------" << endl;

	for (int i = 0; i < SIZE; i++)
	{
		cout << name[i] << "                 " << score[i] << endl;
	}
	cout << "\n";


	system("pause");
	return 0;
}

void getData(int sizeArray, string name[], float score[])
{
	for (int i = 0; i < sizeArray; i++)
	{
		int count = 0;
		cout << "Enter Name: ";
		cin >> name[i];
		cout << "Enter Grade: ";
		cin >> score[i];
		count++;
	}
}

Last edited on
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
#include <iostream>
#include <string>
#include <iomanip>

// return the number of names and scores that were entered
int getData( int max_size, std::string name[], double score[] );

int main()
{
    const int MAX_SIZE = 100 ;
    std::string name[MAX_SIZE];
    double score[MAX_SIZE];

    const int actual_size = getData( MAX_SIZE, name, score );

    if( actual_size > 0 )
    {
        std::cout << "\n              Name                 Grade\n"
                  << "     -------------------------  --------\n"
                  << std::fixed << std::setprecision(2) ; // two digits fter the decimal point

        for( int i = 0 ; i < actual_size ; ++i )
        {
            std::cout << std::setw(3) << i+1 << ". " // sl no in a field of width 3
                      << std::setw(25) << name[i] // name in a field of width 25
                      << std::setw(10) << score[i] << '\n' ;
        }
    }
}

// return the number of names and scores that were entered
int getData( int max_size, std::string name[], double score[] )
{
    for( int num_read = 0 ; num_read < max_size ; ++num_read )
    {
        std::cout << "name (enter an empty string to end input): " ;
        std::getline( std::cin, name[num_read] ) ; // read in a complete line as name

        if( name[num_read].empty() ) return num_read ; // empty name, nothing more to be read, return

        std::cout << "score: " ;
        std::cin >> score[num_read] ;

        if( !std::cin ) return num_read ; // input of score failed, give up, return

        else std::cin.ignore( 1000, '\n' ) ; // extract and discard the new line remaining in the input buffer
                                             // (we do not want the next getline to see this new line)
    }

    return max_size ; // if we reach here, max_size items have been read
}
Thanks!
However, after I tried to implement your method into my code, somehow my method of finding average and lowest score went wrong. How can I fix it?
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
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int getData(int max_size, string name[], float score[]);
void highestLowestAverageByScore(float tScoreArray[], int tSize, float & tHighestGradeScore, float & tLowestGradeScore, float & tAverageGradeScore);
int main()
{
	const int SIZE = 100;
	string name[SIZE];
	float score[SIZE];

	const int actual_size = getData(SIZE, name, score);

	if (actual_size > 0)
	{
		cout << "\n      Name                 Grade\n"
			<< "     ---------------      --------\n";

		for (int i = 0; i < actual_size; i++)
		{
				cout << setw(18) << name[i] 
				<< setw(14) << score[i] << '\n';
		}
	}
	float highestGradeScore = 0;
	float lowestGradeScore = 0;
	float averageGradeScore = 0;

	highestLowestAverageByScore(score, SIZE, highestGradeScore, lowestGradeScore, averageGradeScore);
	cout << "The class average: " << averageGradeScore << endl;
	cout << "Highest grade: " << highestGradeScore << endl;
	cout << "Lowest grade: " << lowestGradeScore << endl;
	system("pause");
	return 0;
}


int getData(int max_size, string name[], float score[])
{
	for (int i = 0; i < max_size; i++)
	{
		int count = 0;
		cout << "Enter name or enter key to stop: ";
		getline(cin, name[i]); 

		if (name[i].empty()) return i; 

		cout << "Enter grade: ";
		cin >> score[i];

		if (!cin) return i; 

		else cin.ignore(1000, '\n'); 
	}

	return max_size; 
}

void highestLowestAverageByScore(float tScoreArray[], int tSize, float & tHighestGradeScore, float & tLowestGradeScore, float & tAverageGradeScore)
{
	tHighestGradeScore = tScoreArray[0];
	tLowestGradeScore = tScoreArray[0];
	for (int i = 1; i < tSize; i++)
	{
		if (tScoreArray[i] > tHighestGradeScore)
		{
			tHighestGradeScore = tScoreArray[i];
		}
		if (tScoreArray[i] < tLowestGradeScore)
		{
			tLowestGradeScore = tScoreArray[i];
		}
	}
	tAverageGradeScore = (tHighestGradeScore + tLowestGradeScore) / 2;
}
> somehow my method of finding average and lowest score went wrong. How can I fix it?

You are passing a wrong size to the function; pass the actual size:
1
2
3
// line 30:
// highestLowestAverageByScore(score, SIZE, highestGradeScore, lowestGradeScore, averageGradeScore);
highestLowestAverageByScore(score, actual_size, highestGradeScore, lowestGradeScore, averageGradeScore);


You should verify that actual_size is greater than zero before trying to compute and print the results.
Strongly favour double over float as the default floating point type.
Topic archived. No new replies allowed.