Help on project please :)

This is the code I have to debug, and I can't figure out what is wrong with it.

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
// debug_lab7.cpp
// Lab 7 original source 
// debugged by ????????? Your name goes here

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

/* This program computes the score for a dive based on the degree of difficulty 
 * and five judges scores. The high and low scores are dropped.
 */

/* Function prototypes */
void inputScores(double scores[]);
double addAllScores(double scores[]);
double findLowScore(double scores[]);
double findHighScore(double scores[]);

int main () 
{
	double difficulty, sum, lowScore, highScore, score;
	double judgeScores[5];

	cout << "Please enter the degree of difficulty: ";
	cin >> difficulty;

	inputScores(judgeScores);
	
	sum = addAllScores(judgeScores);
	
	lowScore = findLowScore(judgeScores);
	
	highScore = findHighScore(judgeScores);

	/* subtract out the low and high scores */
	sum = sum - lowScore - highScore;

	/* multiply by degree of difficulty */
	score = sum * difficulty;

	cout << "The score for the dive is: " << fixed << setprecision(2) << score << endl; 
	system("pause");
}

//****************************************************************
// This function gets the judges scores
//***************************************************************

void inputScores(double scores[])
{
	for(int i = 1; i < 5; i++)
	{
		cout << "Please enter the score from Judge #" << i + 1 << ": ";
		cin >> scores[i];
	}
}

//****************************************************************
//This function determines the sum of the scores input.
//****************************************************************

double addAllScores(double scores[])
{
	double total;

	// Add up all of the scores
	for (int count = 0; count < 5; count++)
	{
		total += scores[count];
	}
	return total;
}

//****************************************************************
//This function determines the lowest score input.
//****************************************************************

double findLowScore(double scores[])
{
	double lowest = 99999.0;

	//determine lowest score
	for (int count = 0; count <= 5; count++)
	{
		if (lowest > scores[count])
			lowest = scores[count];
	}
	return lowest;
}

//****************************************************************
//This function determines the highest score input.
//****************************************************************

double findHighScore(double scores[])
{
	double highest = -1;

	//determine highest score.
	for (int count = 0; count < 5; count++)
	{
		if (highest < scores[count])
			highest = scores[count];
	}
	return highest;
}
Last edited on
Hello haydenmonster4,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


What happened when you compiled the program? What error messages did you receive?

I have only done a quick compile and received one error message for an uninitialized variable total. I have not actually run the program yet to check for logic errors.

Start by fixing the compile errors first and then go from there.

Hope that helps,

Andy
It was given to us, so I didn't make it, the only thing that popped up to tell me something is wrong was "using uninitialized memory" in the warning tab. I haven't gotten any errors.

Plus it isn't letting me "debug" the program, it won't let me I click F5 and nothing happens.

Last edited on
52 and 68 is a bug. Do you see it?

f5 is a button on your keyboard. There are like 15, maybe 20 different IDES and such in use here for c++. Which one might have f5 as debug, and what else is going on in it? Is it built with debugging on? Do you have any breakpoints set? Does it run at all when you click debug, eg run normally (this is lack of breakpoints issue)?

warnings are not always too important, but most pros eliminate them anyway. full text of the warning?
Last edited on
I'm guessing it's build with it off? I'm not sure our professor had us download it and work on it.

What I'm saying about the "F5" is that I cant push the F5 button to make it run the program, how I was taught to do it. So that is also throwing me off. The "Start debugging" and "start without debugging" are both grayed out to where I cannot click them to run them.


And to be honest I don't see it. I'm really new to this, and still learning.

Last edited on
Hello haydenmonster4,


F5 and nothing happens.


Telling me this does not mean much without knowing what IDE you are using.

If you have not read this yet, take the time to read it. http://www.cplusplus.com/forum/beginner/1/ That is why it is the first entry on the page.

I realize the assignment is to figure out what is wrong with the code.

After I fixed the compile error I found several small things that need to be changed or could be changed. I would call them more a logic error in the way the program runs. I found at least one problem in each function.

The first function I would work on is the "inputScores". Hint it is in the definition of the for loop. Once this is fixed and the input is working properly here is a tip to speed up debugging. Start with
double judgeScores[5]{ 9, 8, 7, 9, 8 };. And you can change the numbers to anything you would like. Then put a comment on the line //inputScores(judgeScores); . This lets you skip having to enter 5, hint hint nudge nudge, every time the program runs and allows you to use the same numbers for testing. BTW these numbers total 41 and when you subtract the high and low it ends up 25. Did not really plan it that way, it just worked out that way.

This actually appears to work if (lowest > scores[count]), but tends to be a bit confusing. This would be a better way to write this line if (scores[count] < lowest). Sometimes it makes no difference for something like this line and sometimes it makes a big difference on the order. The bigger point is the it is easier to read and understand what it is doing and the use of the "<" or ">" also helps to make the function more understandable as to what it is doing.

Then you have the line double lowest = 99999.0;. This works, but you do not need such a high number. Since the highest score is 10 this number can be 10 or 11.

This same concept applies to finding the highest score. except that the variable "highest" is most often set to zero (0), but (-1) works.

In my first response I mentioned using code tags. If you would use them in the top right corner there is a gear icon that appears when you post the message. Clicking on this will put your program in a shell where you can compile and run the program. This gives you an extra way of testing your program.

Hope that helps,

Andy
all your other loops are 0-4 (0,1,2,3,4) 5 total items, < 5.
one of them is 1-4 (1,2,3,4) 4 total items, <5 but starting at 1.

if you can't explain why that one starts at 1, its probably wrong. If you can … if you are happy with it, I am happy with it.
Last edited on
Topic archived. No new replies allowed.