wrong output

this source code does what its suppose to do but some of the calculations are off and i dont know why

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;

void open_file (int speeds [], ofstream& out_stream, ifstream& in_stream); //this function opens the file and inputs it into an array
int speeders (int speeds [], ofstream& out_stream); //this function calculates how many speeders there were in the month
double speeder_percent (int speeds [], ofstream& out_stream); // this function calculates the percentage or speeders in the month
double average_speed_of_speeder (int speeds [], ofstream& out_stream); // this function calculates the average speed of a speeder
double percent_extreme (int speeds [], ofstream& out_stream); // this function calculates the percent of extreme speeder (over 55 mph)
int highest_speed (int speeds [], ofstream& out_stream); // this function finds the highest speed someone was going in the month

int main () {
int speeds [4500]; //this is my array that stores the speeds from the file
ifstream in_stream;
ofstream out_stream;
open_file (speeds, out_stream, in_stream);
speeders (speeds, out_stream);
speeder_percent (speeds, out_stream);
average_speed_of_speeder (speeds, out_stream);
percent_extreme (speeds, out_stream);
highest_speed (speeds, out_stream);
}
void open_file (int speeds [], ofstream& out_stream, ifstream& in_stream) {
string filename;
string file_output;
cout << "Enter the data file you want processed." << endl;
cin >> filename;
in_stream.open (filename.c_str());
file_output = filename += ".dat";
out_stream.open (file_output.c_str());
for (int i = 0; i < 4500; ++i) {
in_stream >> speeds [i];
}
out_stream << "Report for " << filename << endl;
}
int speeders (int speeds [], ofstream& out_stream) {
int count = 0;
for (int i = 0; i < 4500; ++i) {
if (speeds[i] > 35 ) {
++count;
}
}
out_stream << count << " cars were speeding" << endl;
}
double speeder_percent (int speeds [], ofstream& out_stream) {
double count1 = 0;
double all = 0;
double percentage = 0;
for (int i = 0; i < 4500; ++i) {
if (speeds[i] > 35) {
++count1;
}
if (speeds[i] > 0) {
++all;
}
}
percentage = (count1) / (all);
percentage = percentage * 100;
out_stream << "This is " << percentage << "% of all the cars driving on Main Street" << endl;
}
double average_speed_of_speeder (int speeds [], ofstream& out_stream) {
double average = 0;
double sum = 0;
double count2 = 0;
for (int i = 0; i < 4500; ++i) {
sum += speeds [i];
if (speeds[i] > 35) {
++count2;
}
}
average = sum / count2;
out_stream << "The average speed of these was " << average << " mph" << endl;
}
double percent_extreme (int speeds [], ofstream& out_stream) {
double count3 = 0;
double all_speeders = 0;
double percent_extreme = 0;
for (int i = 0; i < 4500; ++i) {
if (speeds[i] > 55) {
++count3;
}
if (speeds[i] > 35) {
++all_speeders;
}
}
percent_extreme = (count3) / (all_speeders);
percent_extreme = percent_extreme * 100;
out_stream << count3 << " or " << percent_extreme << "% of the speeders were in excess of 55 mph" << endl;
}
int highest_speed (int speeds [], ofstream& out_stream) {
int max = 0;
for (int i = 0; i < 4500; ++i) {
if (speeds[i] > max) {
max = speeds[i];
}
}
out_stream << "The highest speed recorded was " << max << " mph" << endl;
}

this code outputs this to a file after i input a file



Report for December16.dat
1175 cars were speeding
This is 26.7288% of all the cars driving on Main Street
The average speed of these was 2.27032e+07 mph
391 or 33.2766% of the speeders were in excess of 55 mph
The highest speed recorded was 1835627636 mph

the average speed and the highest speed seem off to me
There are a lot of functions which are declared as returning a value, but don't in fact return anything. These should be declared as type void.

Incorrect results - probably a file error. There are no checks on whether or not the input file was opened, nor whether any records, and how many, were read from that file.

As a minimum, check input file is open. Recommended, count the number of values which were actually read from the file, and pass that to all of the other functions.

This is a cut-down version to show the idea:
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
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;

int open_file (int speeds [], ofstream& out_stream, ifstream& in_stream);   // opens the file and inputs it into an array
void speeders (int speeds [], ofstream& out_stream, int n);                 // calculates how many speeders there were in the month

int main () 
{
    int speeds [4500]; //this is my array that stores the speeds from the file
    ifstream in_stream;
    ofstream out_stream;
    int n = open_file (speeds, out_stream, in_stream);
    cout << "Number of records read = " << n << '\n';
    speeders (speeds, out_stream, n);
}

int open_file (int speeds [], ofstream& out_stream, ifstream& in_stream) 
{
    int count = 0;
    
    string filename;
    string file_output;
    cout << "Enter the data file you want processed." << endl;
    cin >> filename;
    in_stream.open (filename.c_str());
    if (!in_stream)
    {
        cout << "unable to open input file: " << filename << '\n';
        return 0;
    }
    
    file_output = filename += ".dat";
    out_stream.open (file_output.c_str());
    if (!out_stream)
    {
        cout << "unable to open output file: " << file_output << '\n';
        return 0;
    }    
    
    for (count = 0; count < 4500; ++count) 
    {
        in_stream >> speeds [count];
        
        if (!in_stream)
            break;
    }
    out_stream << "Report for " << filename << endl;
    return count;
}

void speeders (int speeds [], ofstream& out_stream, int n) 
{
    int count = 0;
    for (int i = 0; i < n; ++i) 
    {
        if (speeds[i] > 35 )
        {
            ++count;
        }
    }
    out_stream << count << " cars were speeding" << endl;
}



Also, though I didn't put it in the above code, rather than coding the 'magic number' 4500 in two or more separate places, it would be better to define a constant instead.

 
const int arraysize = 4500; // global 


inside main()
 
int speeds [arraysize];


inside function open_file()
 
    for (count = 0; count < arraysize; ++count) 



Note: all of the other functions will be using n, the number of values actually read from the file, so you won't need either 4500 or arraysize in those other functions.
Last edited on
thank you for all that but not to be mean but that didnt fix my problem you kinda just gave me tips on how to make a better program which i will take into account but the real problem is that im getting the wrong things output to my file
you kinda just gave me tips on how to make a better program

More than that. I helped you to correct the errors you described in the output.

the real problem is that im getting the wrong things output to my file

You gave this example of those wrong values:
Report for December16.dat
1175 cars were speeding
This is 26.7288% of all the cars driving on Main Street
The average speed of these was 2.27032e+07 mph
391 or 33.2766% of the speeders were in excess of 55 mph
The highest speed recorded was 1835627636 mph


Those look to me exactly like the result of garbage values stored in the array, specifically because your program doesn't do any checking of what has been read from the file.

Unless your data file contains 4500 very, very strange values, the information in my previous post tells you exactly how to fix the problem with incorrect output.

You know the saying - garbage in, garbage out. Fix the input and the output will be fixed too.

To be fair, there may be other errors in addition to the fundamental problems I highlighted. For example the average speed of the speeders, function average_speed_of_speeder()

contains this code:
1
2
3
4
    sum += speeds [i];
    if (speeds[i] > 35) {
        ++count2;
    }

which probably should read like this:
1
2
3
4
    if (speeds[i] > 35) {
        sum += speeds [i];
        ++count2;
    }


Last edited on
shit thanks dawg sometimes i need someone to be harsh for me to learn this shit so thanks
Ok, glad you made some progress. Nothing personal was intended, but I guess I was a bit blunt in the earlier post. Sorry if it seemed harsh.
Topic archived. No new replies allowed.