How to check if array is empty or not

Aug 19, 2011 at 10:36pm
Hi People!!!

I need your help with a program which gets input from a file which contains list of the students and their 10 numerical grades and calculates the overall score.

Huan Perez 12 34 56 34 12 13 23 16 7 90 Final: 29.7
Neli Cruz 34 56 75 45 67 65 43 12 8 56 Final: 46.1
... etc


However a student can have less than 10 grades, in this case the lacking items in a array must be equal to zero. for example if a student has 8 grades last two remaining elements of array must be equal to zero. But in my case the program assigns them a value of the 8th number.

if somebody has any ideas it would be a great help.

This is the sample of the code:


#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Students{
string name;
string surname;
double score[10];
};

void inputs(Students List[], int p);
void printinf(Students List[], int s);

int main(){

char reply;
do{

Students List[4];

inputs(List, 4);
printinf(List,4);

cout<< endl << "Do you want to run the programm again? y/n" << endl;
cin>> reply;
}while (reply != 'n');

return 0;
}

void inputs(Students List[], int p){
ifstream fin;
fin.open("HW2.in");

for (int i = 0; i < 4; i++){
fin>> List[i].name;
fin>> List[i].surname;
List[i].name = List[i].name + " " + List[i].surname;

for (int a=0; a<10; a++){
fin>> List[i].score[a];
}


//The problem is here. How to check if there are less than ten elements for input and assign remaining elements of the array to zero?
}
fin.close();
}

void printinf(Students List[], int s){

ofstream fout;
fout.open("HW2.out");

fout<< endl << "student name" << '\t' << "Student Score" << '\t' << endl;
for(int i=0; i<4; i++){
fout<< List[i].name << '\t';

double y=0, k;
for(int w = 0; w<10; w++){
fout<< List[i].score[w] << '\t' ;
y = y + List[i].score[w];
}

k = y/10;
fout<< "Final: " << k << endl;
}
fout.close();
}
Last edited on Aug 19, 2011 at 10:38pm
Aug 21, 2011 at 12:13pm
Use good() function after each integer read to check whether the input was int. If it's not int assign 0-s starting from that position.
http://www.cplusplus.com/reference/iostream/ios/good/
Aug 21, 2011 at 12:39pm
How to check if there are less than ten elements for input and assign remaining elements of the array to zero?

It would be better to zero all elements up front, rather than setting them to zero later on.

Use either:

Students List[4] = {0};
or

1
2
Students List[4];
memset(List, 0, sizeof(List));


memset can also be used to reset an array, as well as init it.

As you are using C arrays, it might make sense to keep a count of the used elements, e.g.

1
2
3
4
5
6
struct Students{
string name;
string surname;
double score[10];
int scoreCount; // For example
};


Similarly for the students.

Andy

P.S. If you would like to make your source look prettier, see "How to use tags"
http://www.cplusplus.com/articles/z13hAqkS/
(You can go back and "tag" old messages!)
Last edited on Aug 21, 2011 at 12:50pm
Topic archived. No new replies allowed.