help with printing user inputted arrays
Oct 30, 2017 at 9:50am UTC
I am writing a program where the user chooses a class size from one to twenty and user inputs the scores of that amount of test scores, then it prints out every inputted test score and then at the end it prints out every test score and the average. I can't get it to print every test score at the end it just prints out a random string of letters and numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <iostream>
using namespace std;
int main()
{
double clas, score [20], sum = 0;
string og;
cout << "Enter Class Size (1-20)" << endl;
cin >> clas;
int result = 0;
if (clas >= 1 && clas <= 20)
{
for (double count = 1; count <= clas; count++)
{
int n = 1;
cout << "Enter Score: " << endl;
cin >> score [n];
sum = sum + score[n];
n = n + 1;
}
double avg;
avg = sum / clas;
cout << endl;
cout << "Original Scores: " << score << endl;
cout << "Average: " << avg << endl;
}
else
{
cout << "Class is not within required range. The required range is 1-20." << endl;
}
return 0;
}
everything else works fine but i can't get the printing of the array at the end to work
Oct 30, 2017 at 10:01am UTC
cout << "Original Scores: " << score << endl;
score is effectively a pointer when used like this. You have to iterate over the array.
1 2 3 4
for (double count = 0 ; count < clas; count++)
{
cout << score[n] << '\n' ;
}
Note that you should be using
score[0] to
score[clas-1] . NOT score[1] to score[clas]. The first element of the array is element zero; not element one.
Last edited on Oct 30, 2017 at 10:01am UTC
Topic archived. No new replies allowed.