•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 422,804 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,337 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 642 | Replies: 9
![]() |
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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 :
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 :
cpp Syntax (Toggle Plain Text)
#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; }
Last edited by WolfPack : Jan 9th, 2008 at 9:11 am. Reason: Added [CODE=CPP][/CODE] Tags
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 102
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.
For some reason that is usually in every C++ FAQ, do not use the .eof() function for end of file testing.
Change it to
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
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.
CpP Syntax (Toggle Plain Text)
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
CPP Syntax (Toggle Plain Text)
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
CPP Syntax (Toggle Plain Text)
for ( int i = 0 ; i < count; i++ ) { if (scores[i ]<avrg) cout<<names[i] << endl; }
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 102
•
•
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 410
Reputation:
Rep Power: 5
Solved Threads: 36
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. Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
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.
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.
Age is unimportant -- except in cheese
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- I get a "virus Alert" in toolbox (Viruses, Spyware and other Nasties)
- Frequent "Page cannot be..." and slow performance. Please read. (Viruses, Spyware and other Nasties)
- Mac, "Plug In," (OS X)
- "topantispyware" virus (Viruses, Spyware and other Nasties)
- My "HijackThis" (Viruses, Spyware and other Nasties)
- Just another "How do I get rid of xadsjt offeroptimizer?" post (Viruses, Spyware and other Nasties)
- Tried "stuff to do before posting HijackThis Log" HELP! (Viruses, Spyware and other Nasties)
- index.php?p="Whatever" (PHP)
- Hijacked Repeatedly "about:blank" - Please Help (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: save int file to dat
- Next Thread: Windows GUI - problem with dialog box



Linear Mode