944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4504
  • C++ RSS
Jul 22nd, 2005
0

C++ help with student array problem

Expand Post »
I must ask user number of students who took test, then create a dynamic array and then get the scores for each student. Then calculate the sum,average of the scores. My problem is that once it calculates it continues to ask user for student scores. I am confused with this while loop. Below is my source code. Any ideas?


#include <iostream>
#include <cmath>
#include <cstdlib>


using namespace std;

int main()
{

double grade= 0;
double sum = 0;
int count = 0;
double avg_score = 0;

cout << " Enter the number of students who took test : " ;
int num_students = 0;
cin >> num_students;
// cout << endl;

// double* p = new double[num_students];
while(true){
if (num_students== 0 || num_students == -1)
{

cout << " Enter a postive value please!" << endl;
cout << " Enter the number of students who took tests: ";
cin >> num_students;
cout << endl;
} // end of if statement


double* p = new double[num_students];




for (int i = 1; i <= num_students; i++)
{
cout << " Enter grade for Student " << i << ":" ;
cin >> grade;


p[i] = grade;
cout << p[i]<< endl;
sum += grade;
++count;
} // end of for loop
delete []p;

avg_score = sum / count;


cout << " The average of the test scores is " << avg_score << endl;
cout << " The sum of the test scores is " << sum << endl;
} // end of while loop
return 0;
}
Similar Threads
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Jul 22nd, 2005
0

Re: C++ help with student array problem

You could just put a break; at the end of your while loop, or the better way would be to change the while loop to only ask for the information while num_students < 1. (Then you just do the for loop and exit). Also, be careful mixing your int's and double's for the same variable.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6. int main()
  7. {
  8. double grade= 0;
  9. double sum = 0;
  10. int count = 0;
  11. double avg_score = 0;
  12. cout << " Enter the number of students who took test : " ;
  13. double num_students = 0;
  14. cin >> num_students;
  15. while (num_students < 0 )
  16. {
  17. cout << " Invalid Value! ";
  18. cout << " Enter the number of students who took test: ";
  19. cin >> num_students;
  20. cout << endl;
  21. }
  22. double* p = new double[num_students];
  23.  
  24. for (double i = 1; i <= num_students; i++)
  25. {
  26. cout << " Enter grade for Student " << i << ":" ;
  27. cin >> grade;
  28.  
  29. p[i] = grade;
  30. cout << p[i]<< endl;
  31. sum += grade;
  32. ++count;
  33. } // end of for loop
  34. delete []p;
  35. avg_score = sum / count;
  36.  
  37. cout << " The average of the test scores is " << avg_score << endl;
  38. cout << " The sum of the test scores is " << sum << endl;
  39. return 0;
  40. }
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with Arrays
Next Thread in C++ Forum Timeline: help with validating function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC