Trouble with Arrays

closed account (3voN6Up4)
How do I go about putting a users input into an array? I'm trying to hold 10 test scores and use a function to figure out the perfect scores (100). Right now I am confused, I believe I've only seen arrays HOLD numbers, not have numbers get inputted into arrays themselves.

Here is what I got so far:

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
#include <iostream>
using namespace std;
int main()
{
    int testScore1, testScore2, testScore3, testScore4, testScore5,
    testScore6, testScore7, testScore8, testScore9, testScore10;
    int ScoreArray[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    cout << "Enter a test score! (1/10)" << endl;
    cin >> testScore1;
    cout << "Enter a test score! (2/10)" << endl;
    cin >> testScore2;
    cout << "Enter a test score! (3/10)" << endl;
    cin >> testScore3;
    cout << "Enter a test score! (4/10)" << endl;
    cin >> testScore4;
    cout << "Enter a test score! (5/10)" << endl;
    cin >> testScore5;
    cout << "Enter a test score! (6/10)" << endl;
    cin >> testScore6;
    cout << "Enter a test score! (7/10)" << endl;
    cin >> testScore7;
    cout << "Enter a test score! (8/10)" << endl;
    cin >> testScore8;
    cout << "Enter a test score! (9/10)" << endl;
    cin >> testScore9;
    cout << "Enter a test score! (10/10)" << endl;
    cin >> testScore10;
    cout << ScoreArray[10];
}
@Lucsofhazard

To input into the array, just use cin >> testScore[x]; as in
1
2
3
4
5
for(int x=0;x<10;x++)
{
  cout << "Enter a test score! (" << x+1 << "/10)" << endl;
  cin >> testScore[x];
}

and you can then remove all the cin >> testScore1; // Up to testScore10
I'll leave it to you to make the function fifuring out the scores, etc.
closed account (3voN6Up4)
Oh I see now, here is what I have, but I cannot output the array. Loops make things so much easier, don't they? I just like to type a lot I guess!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    int ScoreArray[10];

    cout << "You will be entering 10 test scores, and I will output the perfect scores." << endl;
    system("pause");
    for (int i = 0; i < 10; i++)
    {
        cout << "Enter a test score! (" << i + 1 << "/10)" << endl;
        cin >> ScoreArray[i];
    }
    cout << ScoreArray[i];
}
Last edited on
closed account (3voN6Up4)
It also crashes, just a heads up. It crashes because I am trying to output the array
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
    const int NSCORES = 10 ;

    int ScoreArray[NSCORES];

    std::cout << "You will be entering 10 test scores, and I will output the perfect scores.\n" ;
    for( int i = 0 ; i < NSCORES ; ++i )
    {
        std::cout << "Enter a test score! (" << i + 1 << "/10): " ;
        std::cin >> ScoreArray[i];
    }

    // print out the contents of the array
    std::cout << "\nthe array contains:\n" ;
    for( int i = 0 ; i < NSCORES ; ++i ) std::cout << ScoreArray[i] << ' ' ;
    std::cout << '\n' ;
}


Range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
1
2
3
4
// print out the contents of the array
std::cout << "\nthe array contains:\n" ;
for( int v : ScoreArray ) std::cout << v << ' ' ;
std::cout << '\n' ;
closed account (3voN6Up4)
Thanks @JLBorges and @whitenite1.

I am now aware that the array is functioning and now I am unsure of how I should bring the array to a function that determines which of the 10 scores are "100". I haven't gotten to pointers yet, and making a global array didn't go so well.

On line 13, it says "error: ISO C++ forbids comparison between pointer and integer [-fpermissive]"

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
#include <iostream>
#include <windows.h>
using namespace std;
int ScoreArray[10];
int countPerfect()
{
    for (int i = 100; i == ScoreArray; i++)
    {
        cout << ScoreArray[i] << endl;
    }
}
int main()
{
    int ScoreArray[10];
    cout << "You will be entering 10 test scores, and I will output the perfect scores." << endl;
    system("pause");
    for (int i = 0; i < 10; i++)
    {
        cout << "Enter a test score! (" << i + 1 << "/10)" << endl;
        cin >> ScoreArray[i];
        system("cls");
        if (ScoreArray[i] < 0 || ScoreArray[i] > 100)
        {
            cout << "No numbers great than 100, or less than 0." << endl;
            system("cls");
            cout << "Enter a test score! (" << i + 1 << "/10)" << endl;
            cin >> ScoreArray[i];
        }
    }
    countPerfect();
}
Your loop in the countPerfect funciton will be the same as the one in main. Except this time, instead of putting numbers in the array, you will check if the number in scoreArray[i] is 100. You can do this using an if-statement. If a score is 100, you can increment a counter variable.
Several things wrong with countPerfect:
line 5: You should pass scoreArray in as an argument.
line 7: Your for loop is wrong. i should start at 0 and the termination condition should be <10.
Line 9: You want to count the number of perfect scores, not output them.
Line 11: countPerfect() should return the number of perfect scores.

A couple of problems in main also:
Line 14: This declaration hides the global.
Lines 22-28: This doesn't protect against entering an invalid number multiple times.
Line 31: main() should return 0

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
#include <iostream>
using namespace std;
const int NSCORES = 10;

int countPerfect(int scores[])
{   int count = 0;

    for (int i=0; i<NSCORES; i++)
      if (scores[i] == 100)
        count++;    
    return count;
}

int main()
{   int ScoreArray[NSCORES];
    cout << "You will be entering 10 test scores, and I will count the perfect scores." << endl;
    system("pause");
    for (int i = 0; i < NSCORES; i++)
    {   cout << "Enter a test score! (" << i + 1 << "/10)" << endl;
        cin >> ScoreArray[i];
        system("cls");
        while (ScoreArray[i] < 0 || ScoreArray[i] > 100)
        {   cout << "No numbers great than 100, or less than 0." << endl;
            system("cls");
            cout << "Enter a test score! (" << i + 1 << "/10)" << endl;
            cin >> ScoreArray[i];
        }
    }
    cout << countPerfect() << " perfect scores" << endl;
    return 0;
}

Last edited on
closed account (3voN6Up4)
@AbstrationAnon
I never used const before, what does it do? I was translating your program into my own code and the const made it work, other than a simple int

@Arslan7041
Thanks for your input!

const tells the compiler you promise not to change that value. If you attempt to do so, the compiler will generate an error. It's a good practice to get into.
closed account (3voN6Up4)
@AbstractionAnon

The program doesn't work, it outputs: "10 perfect scores"
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
34
35
36
37
#include <iostream>
#include <windows.h>
using namespace std;
int ScoreArray[10];
int countPerfect()
{
    const int XSCORES = 10;
    int count = 0;
    for (int i = 0; i < XSCORES; i++)
    {
        if (ScoreArray[i] == 100)
        {
            count++;
        }
    }
    cout << XSCORES << " perfect scores" << endl;
}
int main()
{
    int ScoreArray[10];
    cout << "You will be entering 10 test scores, and I will count the perfect scores." << endl;
    system("pause");
    for (int i = 0; i < 10; i++)
    {
        cout << "Enter a test score! (" << i + 1 << "/10)" << endl;
        cin >> ScoreArray[i];
        system("cls");
        while (ScoreArray[i] < 0 || ScoreArray[i] > 100)
        {   cout << "No numbers great than 100, or less than 0." << endl;
            system("pause");
            system("cls");
            cout << "Enter a test score! (" << i + 1 << "/10)" << endl;
            cin >> ScoreArray[i];
        }
    }
    countPerfect();
}
Last edited on
XSCORES is a constant integer. You cannot change a constant integer.
1. Get rid of the XSCORES variable
2. Use 10 instead of XSCORES in your for loop
3. Output count instead of XSCORES
closed account (3voN6Up4)
@PBachmann
Hmm it still says it counted 10 perfect scores.
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
34
35
36
#include <iostream>
#include <windows.h>
using namespace std;
int ScoreArray[10];
int countPerfect()
{
    int count = 0;
    for (int i = 0; i < 10; i++)
    {
        if (ScoreArray[i] = 100)
        {
            count++;
        }
    }
    cout << count;
}
int main()
{
    int ScoreArray[10];
    cout << "You will be entering 10 test scores, and I will count the perfect scores." << endl;
    system("pause");
    for (int i = 0; i < 10; i++)
    {
        cout << "Enter a test score! (" << i + 1 << "/10)" << endl;
        cin >> ScoreArray[i];
        system("cls");
        while (ScoreArray[i] < 0 || ScoreArray[i] > 100)
        {   cout << "No numbers great than 100, or less than 0." << endl;
            system("pause");
            system("cls");
            cout << "Enter a test score! (" << i + 1 << "/10)" << endl;
            cin >> ScoreArray[i];
        }
    }
    countPerfect();
}
PBachman wrote:

1. Get rid of the XSCORES variable
2. Use 10 instead of XSCORES in your for loop

Bad advice. The use of "magic numbers" such as 10 is a poor practice. It is much better to define a constant and use that. That way, if you want to change the number of scores in the program, you need only change one place and don't need to go hunting through the program for all 10s.

lucsofhazard wrote:
it counted 10 perfect scores.

Look at line 10 carefully. You're using the assignment operator (=), not the comparison operator (==), thereby assigning 100 to every occurrence of ScoreArray and causing the if statement to be true on every iteration. Compare to line 9 of the snippet I posted.

Last edited on
closed account (3voN6Up4)
@AbstractionAnon

So looking at the code you gave me

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
#include <iostream>
using namespace std;
const int NSCORES = 10;

int countPerfect(int scores[])
{   int count = 0;

    for (int i=0; i<NSCORES; i++)
      if (scores[i] == 100)
        count++;    
    return count;
}

int main()
{   int ScoreArray[NSCORES];
    cout << "You will be entering 10 test scores, and I will count the perfect scores." << endl;
    system("pause");
    for (int i = 0; i < NSCORES; i++)
    {   cout << "Enter a test score! (" << i + 1 << "/10)" << endl;
        cin >> ScoreArray[i];
        system("cls");
        while (ScoreArray[i] < 0 || ScoreArray[i] > 100)
        {   cout << "No numbers great than 100, or less than 0." << endl;
            system("cls");
            cout << "Enter a test score! (" << i + 1 << "/10)" << endl;
            cin >> ScoreArray[i];
        }
    }
    cout << countPerfect() << " perfect scores" << endl;
    return 0;
}


Instead of using 10 places in an array, you want me to use a constant?

Also. I think you forgot to put a parameter in on line 29. Should be:

cout << countPerfect(scores[]) << " perfect scores" << endl;
right?
Last edited on
closed account (3voN6Up4)
 
cout << countPerfect (scores[]) << " perfect scores" << endl;

is actually invalid, my mistake. My compiler says it has too few arguments to function countPerfect though.
On line 29:
cout << countPerfect() << " perfect scores" << endl;

Should be:
cout << countPerfect(ScoreArray) << " perfect scores" << endl;
closed account (3voN6Up4)
@chicofeo

I'm confused... I mean that works perfectly, but I don't get how it works while the parameters aren't even matched.
The link below may help about "Array as Parameters" (You may have to scroll down a little).

http://www.cplusplus.com/doc/tutorial/arrays/

Below is just a way (not THE way) of the homework exercise :
It shows how do I call the function with array parameters.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

//Function Prototype.
int countPerfectScores(int[], const int);
void inputScoresInArray(int[], const int);


int main()
{
    const int NUMOFTESTSCRS{ 10 };
    int testScores[NUMOFTESTSCRS] = { 0 };

    inputScoresInArray(testScores, NUMOFTESTSCRS); // Call the function with the array parameter and its size.
    std::cout << "There were [" << countPerfectScores(testScores, NUMOFTESTSCRS) // Call the function with the array parameter.
	   << "] perfect scores.\n";

    return 0;
}

int countPerfectScores(int scores[], const int size)
{
    int counter{ 0 };

    for (int count = 0; count < size; count++)
    {
	   if (scores[count] == 100)
		  counter++;
    }

    return counter;
}

void inputScoresInArray(int scores[], const int size)
{
    std::cout << "You will be entering " << size << " test scores, " <<
	   "and I will count the perfect scores.\n";

    for (int count = 0; count < size; count++)
    {
	   std::cout << "Enter a test score! " << "(" << count + 1
		  << " / " << size << ") : ";
	   std::cin >> scores[count];

	   while (scores[count] < 0 || scores[count] > 100)
	   {
		  std::cout << "The number " << scores[count] << " is not between 0 and 100.\n";
		  std::cout << "Enter a test score! " << "(" << count + 1
			 << " / " << size << ") : ";
		  std::cin >> scores[count];
	   }
    }
}
closed account (3voN6Up4)
Gosh C++ is difficult to grasp. Thanks for the link, I will read up on that!
Topic archived. No new replies allowed.