Compilers acting differently.

We had an assignment in class:
Write a modular program that prompts the user for the total number of students in a class, and creates an array to hold an integer test score for each student.
The program must accept the test scores from the user and store it in the array.
The program must also display the total number of perfect scores (i.e. scores of 100) entered by the user.
*********************************************************************************
Here's my solution:


#include <iostream>
#include <string>

using namespace std;

int main()
{
int num_students;
int count;
int perfect_score;

cout << "How many students do you have in you class? ";
cin >> num_students;
cout << endl << endl;
cout << "You have " << num_students << " students in your class. Please enter their";
cout << " scores, hitting the return key after each score has been input.\n\n";

int SCORES[num_students];

for (count = 0; count < num_students; count++)
{
cin >> SCORES[count];
cout << endl;
if (SCORES[count] == 100)
{
perfect_score++;
cout << "So far you have " << perfect_score << " 100's. \n\n";
}
}

cout << "The total number of perfect scores is: " << perfect_score;
cout << endl;


return 0;
}

********************************************************************************

I use XCode for writing program and it compiles fine on XCode, however, the professor couldn't compile this code on his computer. Why would that happen? Here's the solution:

/* Jonathan White
This program is designed to display the number of perfect scores in a class. */
#include <iostream>
using namespace std;

int main()
{


int perfect = 0;
int students;
int score = 0;


cout << "Input the total number of students in the class: ";
cin >> students;


int values[students];

for (int num = 0; num < students; num++)
{
cout << "Student " << (num + 1) << "; ";
cin >> values[num];
}

for (int num = 0; num < students; num++)
{
if (values[num] == 100)
perfect += score + 1;
}

cout << "The number of perfect scores is " << perfect << endl;

return 0;
I'm confused -- why are there two sets of code?

Anyway:


1) In the first set of code, you never initialize 'perfect_score'. Remember that variables don't automatically start at zero, you need to initialize them to have them start at zero.

2) In both sets of code you are creating an array (SCORES / values) with a non-const variable size, which is not legal in standard C++:

 
int SCORES[num_students];  // this only works if 'num_students' is const, which it isn't 


3) This isn't an error... but what's the point of 'score' in the second set of code? It's always zero... you never change it. And you only use it here: perfect += score + 1; ... which is the same as incrementing 'perfect' by 1 since 'score' is always zero.


#1 might throw a warning, and #2 should throw an error, but a lot of compilers will let you get away with #2 without complaining even though it technically isn't legal. I'm guessing your compiler doesn't complain, and your teacher's compiler does, which is why it won't compile for him.

Note you don't even need an array for this. Just combine those two for loops into one for loop. Like... just get the score, then see if the score is 100 right away. No need to record all the scores and check them all later.
Last edited on
Disch wrote:
2) In both sets of code you are creating an array (SCORES / values) with a non-const variable size, which is not legal in standard C++

Are you sure about that? I thought the size just needed to be known at compile time, but not necessarily a const. Wouldn't this be legal?

1
2
int a = 5;
int arr[a];
IIRC, that's legal in C99, but is not legal in C++
It needs to be a constant known at compile time.
g++ allows this as an extension, but it won't necessarily work with other compilers (such as VC++).
More info on variable length arrays -> http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC75
Topic archived. No new replies allowed.