why is it running when the file is empty.

it should be saying the file is empty but it just runs the program with zeros and if I don't set the zero, it runs the program with twos.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//libraries
#include <fstream> //file input and output
#include <iostream> //console input and output
using namespace std;

//Programmer defined data types
//NONE

//Special compiler dependent definitions
//NONE

//global constants/variables
const int SIZE = 8; //size of the array

//Programmer defined functions
void introduction(string obj); //program introduction
int readScores(int* s, int count); //input scores from file
double calcAverage(int* s); //calculate average score
int countScoresGreater(int* s, double avgScore);  //calculate the number of scores greater than average score

//main program
int main()
{
  //Data
  string objective = "input scores from file 8Scores.txt and calculate the average score and the number of scores > average score."; //program objective
  int i; //loop index
  int count; //check if file is empty
  int scores[SIZE]; //all the scores input from the file
  int nGreater = 0; //number of scores greater than the average score
  double average; //average score

  //user introduction
  introduction(objective);  

  //input scores from input file
  readScores(scores, count);

  //if file was empty
  if (count == 0) cout << "Sorry the file is empty. ";
  if (count > 0) //if file was not empty
  {
    //calculate and output average score 
    average = calcAverage(scores);
    cout << endl << "The average of " << SIZE << " numbers is " << average << endl; 
  
    //calculate the number of scores greater than average score
    cout << "The number of scores greater than the average score is " << countScoresGreater(scores, average) << endl;
  }
}//main

// introduction
void introduction(string obj)
{
  //data
  //obj is the program objective

  //user introduction
  cout << "Objective: This program will "; 
  cout << obj << endl;
  cout << "Programmer: Teacher\n"; 
  cout << "Editor(s) used: Notepad\n"; 
  cout << "Compiler(s) used: TDM MinGW\n"; 
  cout << "File: " << __FILE__ << endl; 
  cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl; 
}//introduction

//input scores from file
int readScores(int* s, int count)
{
  //data
  //s is an array of scores (passed from main)
  int i; // loop index 
  int aScore = 0; //each score from input file
  ifstream fin; //represent input file
  string userFile; //user input file name

  //
  count = 0;

  // get user file name
  cout << "What is the input file name? ";
  getline(cin, userFile);
  cout <<endl;

  //input scores from input file
  fin.open(userFile.c_str()); //open specifed file from user
  if (!fin.good()) throw "I/O error"; //if file isnt open output error to console
  if (fin.good())
  { 
    for (i = 0; i < SIZE; i++) 
    { 
      fin >> aScore;
      fin.ignore(1000, 10); 
      s[i] = aScore;
      count++;
      cout << aScore << " ";
    } // for
  }
  fin.close(); 
  return count;
}//readScores

//calculate average score
double calcAverage(int* s)
{
  //data
  double result; //average score
  //s array of scores from main
  int i; //loop index
  int scoreTotal = 0; //sum of all scores

  //sum all the scores
  for(i = 0; i < SIZE; i=i+1)
  {
   scoreTotal = scoreTotal + s[i];
  }//for i

  //calculate average score
  result = scoreTotal / double(SIZE);
  return result; 
}//calcAverage

//calculate the number of scores greater than average score
int countScoresGreater(int* s, double avgScore)  
{
  //data
  int result; //number of scores greater than the average score
  //s is array of scores from main
  //avgScore is the average score
  int i; //loop index

  //count the scores greater than the average score
  for (i = 0; i < SIZE; i++)
  {
    if (s[i] > avgScore) result = result + 1;
  }//for i
  return result;
}//countScoresGreater 
1
2
3
4
5
6
7
8
9
int readScores( int* s, int byvalue );

//main program
int main()
{
  int count; // uninitialized
  readScores( scores, count );
  if (count == 0) cout << "Undefined behaviour";
}

The 'count' is uninitialized. We do not know what its value is.

The function takes both parameters by value. Therefore, it does not modify 'count'.

The function returns a value that you don't use.

Thus,
1
2
  int count = 0; // initialized with known value
  count = readScores( scores, count );
thank you!
Topic archived. No new replies allowed.