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;
}

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.