| | |
C++ help with student array problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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;
}
#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;
}
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)
#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 : " ; double num_students = 0; cin >> num_students; while (num_students < 0 ) { cout << " Invalid Value! "; cout << " Enter the number of students who took test: "; cin >> num_students; cout << endl; } double* p = new double[num_students]; for (double 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; return 0; }
![]() |
Similar Threads
- Sorting student info Problem (Python)
- Array problem (C++)
- Array problem (C#)
- Is there a simplest way to work this array problem? (C++)
- class array problem! (C++)
- i have problem with array plz help (Java)
- Large Array Problem (PHP)
Other Threads in the C++ Forum
- Previous Thread: Help with Arrays
- Next Thread: help with validating function
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets





