I have a question in c++ , can you help me ,and tell me where is the proplem in my solution ?

The question is:

Assume that the maximum number of students in a class is 50. Write a program that reads students' names followed by their test score from a file and outputs the following:

a. class average
b. Names of all the students whose test scores are below the class average, with an appropriate message
c. Highest test score and the names of all the students having the highest score


You can use this file : in.txt
Ahmed 60.8
Mona 87.3
Ali 77.1
Mahmood 97.9
Isa 63.1
Zainab 100

MY SOLUTION :

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main()
{
    int count = 0;
	float avrg;
	int maxIndex=0;
    float sum=0;
	float scores[50];
	string names[50];

	ifstream infile;
	infile.open("in.txt");

	while (!infile.eof() && (count <=50))
	{
		infile>>names[count]>>scores[count];
		count++;
		sum+=scores[count];
		
	}
    avrg= sum/count;
	cout<<avrg;
		
	if (scores[50]<avrg)
		cout<<names[count];
	cout<<endl;

	maxIndex=scores[0];

	if (scores[count]>maxIndex)
		maxIndex =count;
	cout <<names[count];
	infile.close();
	return 0;
}

Dani AI

Generated

A reliable, minimal plan: read all student records into a container, compute the class average in one pass, then do two simple scans—one to print students below average and one to find/collect the highest score and its names. Several concrete defects in the posted program prevent any useful output; fixing those and using a small, robust pattern makes the rest trivial.

Key problems observed in the original code (for ):

  • The input loop and indexing are fragile. An off-by-one or EOF test that allows index 50 for a 50-element array will run past the array bounds.
  • The code increments count and then does sum += scores[count], so the sum adds an uninitialized/out-of-range element instead of the value just read.
  • Later references like scores[50] and printing names[count] use wrong indices (out of range or past the last read).
  • The max-tracking uses the wrong type (an int assigned a float) and the wrong index when comparing; that mix of types/indexes corrupts the logic.
  • No check that the input file actually opened, and no guard against an empty file (divide-by-zero when computing average).

A compact, modern pattern (illustrative) is to parse lines into a vector of small Student structs, use std::accumulate for the sum and std::max_element for the highest score, then simple loops to print names below average and those equal to the maximum. Example approach:

#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iomanip>

struct Student { std::string name; double score; };
// read lines into students vector using getline + istringstream,
// check file open, compute sum with std::accumulate, find max with std::max_element,
// print average, below-average names, and all names with score == max.

Final tips: always check ifstream after open, protect the 50-student limit if required, prefer double for scores, guard against empty input before dividing, and when comparing floating values consider an epsilon if exact equality is not expected. As and suggested, focus first on a correct, well-indexed input loop and a clean pass to compute average; the rest follows.

Recommended Answers

All 9 Replies

Asking where the problem is not a question in C++.
You have to explain what the problem is, and then we will tell you where it is.

One problem in your code is this loop.

while (!infile.eof() && (count <=50))
{
         infile>>names[count]>>scores[count];
         count++;
         sum+=scores[count];
}

For some reason that is usually in every C++ FAQ, do not use the .eof() function for end of file testing.
Change it to

while ((infile>>names[count]>>scores[count])&& (count <50))
{
         sum+=scores[count];
         count++;
}

That should correct the problem of incorrect calculation of the average.

For 2 and 3, you should use loops to iterate through all the values in the array, instead of just using scores[50].

Something like

for ( int i = 0 ; i < count; i++ )
{
             if (scores[i ]<avrg)
                     cout<<names[i] << endl;
}

ok ,please tell how can I get the Highest test score and the names of all the students having the highest score ?

Google for finding maximum in array C++

The most common way to find the biggest number, is creating a variable, which is called max , for storing the first element of the array. Run throught the entirely array and check whether there are any number that are bigger than our max variable. Whenever we found any number that is bigger than max , we immediately assign that value to max . By the end of the loop, max will contain the largest number.

Where is the mistakes in my solution , there is no output ,I don't know why.

Of course there's output. You have cout statements in your code. Unless you're not telling us the whole story. Explain exactly what is happening, and why it's wrong. Examples are easier for us to understand.

And about "I WANT IT FOR TOMORROW" -- we don't care. You should have posted last week if you needed that bad. See this, it's in The Rules you read when you registered.

Thank you inviusal ...

thank you very much invisal*

what is invisal?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.