Array and Functions

Using 2 arrays, each declared to size 20. Array 1 will be an array of strings to hold the contestants’ names. Array 2 will be an array of double to hold the average score. As you read each name, store in Array 1. As you Calculate_Score, store the result in Array 2. Calculate ALL scores before you do any printing. You will read until the end of the file and will need to keep a counter of how many contestants you read data for. Once the data has all been read and the array are filled, print the output headers and use a for loop (0 to counter or counter-1) to print the results. The output for this program will be simply the name and score, aligned in columns. Use the same data & functions as the first program.
A new singing competition is being organized in Texas. Five judges score the performers to determine a winner. Each judge sores the performer between 0 and 10, fraction scores with 1 decimal place such as 5.7 are allowed. A performer’s final score is found by eliminating the highest judge’s score and the lowest judge’s score then averaging the remaining 3 scores.

My problem is I keep getting a compiler error but I can't figure out why.

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

double Calculate_Score(double Scores[]);
double Find_Lowest(double Scores[]);
double Find_Highest(double Scores[]);

int main ()
double lg, small, points;
string judges[20];
double Scores[20];
ifstream infile;"input.txt");
ofstream outfile;"output.txt");
outfile<<left<<setw(13)<<"Name"<<setw(15)<<"Avg Score"<<endl;
while (!infile.eof())

return 0;

double Calculate_Score(double Scores[])
double points,small,lg;
int i;
points -= (small + lg)/3;
return points;

double Find_Lowest(double Scores[])
double small = Scores[0];
double points = 0;
int i;
for(i = 0; i < 10; i++)
if (Scores[i] < small)
small = Scores[i];
return small;

double Find_Highest(double Scores[])
double lg = Scores[0];
double points = 0;
int i;

for(i = 0; i < 10; i++)
points += Scores[i];
if (Scores[i] > lg)
lg = Scores[i];


return lg;

This code compiles for me. What error are you seeing?
What is the error and what line does it point to?
Unhandled exception at 0x00a163e6 in TexasIdol2.exe: 0xC0000005: Access violation writing location 0xcccccccc.

It seems to run up until I want to outfile the the average of the scores and the judges names
Okay. Well, the way that ifstream works if whenever you grab data from the file it always starts at the beginning. The first time you grab data from the file in your program you are saving the first two lines of data into the 20th slot of your arrays. Then later on in your while loop you are again saving more data into those same slots.

It is important to note that the 20th slot does not exist.
When you create an array of the size 20, the indices of the array are from 0-19, not from 1-20.
In that case what change would you suggest I make to the code?
I've tried a couple of things but nothing seems to give me the output that I am looking for.
What is the output you are getting right now? Is it just the column headers? Cause that's what it looks like.

If so, you need to actually retrieve all of your data. How is your data stored in the input file? Is it stored in two columns? Or is it in one column?
Smithson 7.0 8.0 9.4 3.0 9.0
Marshall 9.0 2.0 4.0 1.5 8.0
Johnson 5.2 5.2 5.2 5.2 5.2
Jones 6.1 10.0 10.0 5.3 9.7
Keyes 2.0 3.5 2.0 3.5 3.0
Simmons 7.2 10 10 7 5
Williams 9.3 5 8.3 9 9.3
Hunt 5.1 2.4 5.5 9.0 7
Rules 1 9 8 7 6
Anand 5.5 7 7 7 7
Hartley 5.0 8.8 9.8 10 7.5
Hoover 6.1 7.2 8.3 7.2 6.1
Casey 9 7 5 3 9
Hudson 4.4 5.5 6.6 7.7 8.8
Conti 8.9 9.8 3.9 10.0 3
Peterson 8.8 4.2 6.0 2 7

That's what my data looks like and I am supposed to simply print out the name and the average of the scores, which seems simple enough. But I just seem to be missing something
Here is the tutorial from this website that offers the information you need:

Look a little less than half-way down the page at a section called "Text Files". You'll notice in their example that they have a while loop that will read from their file one line at a time.

To do this, they are using a function called getline(). The way this function works is that it will read an entire line from the file stream (passed as the first argument: infile) and save that entire line as a string the string variable that is passed as the second argument.

So at this point, you have the entire first line of your input file saved in a string (name and scores alike). Your next job is to separate it all. So you need to parse the line by using string member functions which are available here in this site's documentation ( You can use the string::find() commands to figure out where to split the line using the string::substr() command.

For example:
Suppose the first line needs to be split into name and scores...
// The line variable contains the first line data of the file.
string line = Smithson 7.0 8.0 9.4 3.0 9.0;

// The find command with a space passed as the argument
// will search for the first space inside of the string and return the position.
int space_position = line.find(' ');

// Now that we know where the space is located, we know
// where to split the string to isolate the name.
// The first argument to substr() is the starting position of the new sub string.
// We want ours to start at the beginning because that is where the name is in
// the line, so we pass 0. The second argument is the amount of characters to
// take after the starting character. Since we are starting at the beginning,
// the position of the space that we found is equal to the length of the name.
string name = line.substr(0, space_position)

So, that tells you the process that we read lines from files and how we split them up into different forms of data. So to finish that line you need to then change your primary string to not include the name, so you can search for the next space without repeating what you just did.

The way to do that is to do like so:
// If you only pass the first argument it will assume you want
// everything following that position.
line = line.substr(space_position + 1);

I hope this helps. :)

EDIT: Added string member functions link.
Last edited on
Topic archived. No new replies allowed.