4 Replies - 622 Views - Last Post: 03 April 2013 - 07:12 PM Rate Topic: -----

#1 rhsjr_25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-April 13

star search C++ program, How to not use Global Variables

Posted 03 April 2013 - 05:19 PM

I did the assignment and the code compiles, but she sent it back and said "do not use global variables". I've been trying to figure it out but every time I make a change, the code doesn't compile anymore. Please help if you can.

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

void getJudgeData(double&, double&, double&, double&, double&);
void calcScore(double&);
int findLowest(double&);
int findHighest(double&);
double score1, score2, score3, score4, score5, LowestScore, HighestScore;
double TotalScore;

int main()
{
	getJudgeData(score1, score2, score3, score4, score5);
	calcScore(TotalScore);
	return 0;
}

void getJudgeData(double &score1, double &score2, double &score3, double &score4, double &score5)
{
	cout << "----Data entry for Judge 1-----------\n Enter score between 0 and 10: ";
	cin  >> score1;
		while (score1 <0 || score1 > 10)
		{
			cout <<"Score must be in the range 0-10, please re-enter score: ";
			cin  >> score1;
		}
	cout << "----Data entry for Judge 2-----------\n Enter score between 0 and 10: ";
	cin  >> score2;
		while (score2 <0 || score2 > 10)
		{
			cout <<"Score must be in the range 0-10, please re-enter score: ";
			cin  >> score2;
		}
	cout << "----Data entry for Judge 3-----------\n Enter score between 0 and 10: ";
	cin  >> score3;
		while (score3 <0 || score3 > 10)
		{
			cout <<"Score must be in the range 0-10, please re-enter score: ";
			cin  >> score3;
		}
	cout << "----Data entry for Judge 4-----------\n Enter score between 0 and 10: ";
	cin  >> score4;
		while (score4 <0 || score4 > 10)
		{
			cout <<"Score must be in the range 0-10, please re-enter score: ";
			cin  >> score4;
		}
	cout << "----Data entry for Judge 5-----------\n Enter score between 0 and 10: ";
	cin  >> score5;
		while (score5 <0 || score5 > 10)
		{
			cout <<"Score must be in the range 0-10, please re-enter score: ";
			cin  >> score5;
		}
}

int findLowest(double &LowestScore)
{
	
	if (score1<=score2 && score1<=score3 && score1<=score4 && score1<=score5)
	LowestScore=score1;
	else if (score2<=score1 && score2<=score3 && score2<=score4 && score2<=score5)
	LowestScore=score2;
	else if (score3<=score1 && score3<=score2 && score3<=score4 && score3<=score5)
	LowestScore=score3;
	else if (score4<=score1 && score4<=score2 && score4<=score3 && score4<=score5)
	LowestScore=score4;
	else if (score5<=score1 && score5<=score2 && score5<=score3 && score5<=score4)
	LowestScore=score5;
	
	return 0;
}

int findHighest(double &HighestScore)
{
	if (score1>=score2 && score1>=score3 && score1>=score4 && score1>=score5)
	HighestScore=score1;
	else if (score2>=score1 && score2>=score3 && score2>=score4 && score2>=score5)
	HighestScore=score2;
	else if (score3>=score1 && score3>=score2 && score3>=score4 && score3>=score5)
	HighestScore=score3;
	else if (score4>=score1 && score4>=score2 && score4>=score3 && score4>=score5)
	HighestScore=score4;
	else if (score5>=score1 && score5>=score2 && score5>=score3 && score5>=score4)
	HighestScore=score5;
	
	return 0;
}

void calcScore(double &TotalScore)
{
	findLowest(LowestScore);
	findHighest(HighestScore);
	TotalScore = (((score1+score2+score3+score4+score5)-(LowestScore+HighestScore))/3);
	cout << endl;
	cout << setw(5) << "Score 1" << "\t\t" << "Score 2" << "\t\t" << "Score 3" << "\t\t" << "Score 4" << "\t\t" << "Score 5" << endl;
	cout <<"---------------------------------------------------------------------\n";
	cout << setw(5) << score1 << "\t\t" << score2 << "\t\t" << score3 << "\t\t" << score4 << "\t\t" <<score5<< "\t\t" << endl;

	cout <<"Average= " << fixed << setprecision(2) << TotalScore  <<  endl; 

}



Is This A Good Question/Topic? 0
  • +

Replies To: star search C++ program, How to not use Global Variables

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: star search C++ program, How to not use Global Variables

Posted 03 April 2013 - 05:34 PM

Just put the variables inside of main... And modify your calcScore function to accept the needed parameters... Or use an array for the scores.

This post has been edited by IngeniousHax: 03 April 2013 - 05:36 PM

Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: star search C++ program, How to not use Global Variables

Posted 03 April 2013 - 05:37 PM

Wow, you are really lost. You truly need to sit down with your instructor and tell her just how lost you are before trying to go any further. Otherwise you're quickly going to get so far behind that you'll never catch up, or catch on, and just waste your tuition.

There isn't a lot of help we can give with this mess short of re-writing your homework for you. I know that's not what you want to hear, but its simply that bad.

These make no sense they way you've set them up.
void calcScore(double &TotalScore)
int findHighest(double &HighestScore)
int findLowest(double &LowestScore)

Its obviously you put the LowestScore variable with the findLowest method because you somehow think that they might in some way, maybe, go tother, somehow, but you don't really know how.
Instead, you should be passing in all the numbers, and returning the lowest - not void.

If you made an int array instead of the 5 individual scores you could just pass the array as an argument. You could then loop through the array instead of this terrible repetitive contruct you have in getJudgeData(). Why is that even named with 'get' anyway? It really should just be JudgeData() or ValidateData(). But its not getting anything.

:offtopic:
Spoiler


"I don't know where to start" - This usually means you should go back to your instructor and admit you are this lost. Don't bluff your way through this course thinking that by chapter 10 it will all suddenly snap into place and become clear. It won't. Unlike history class where chapter 1 might be 17th century England and chapter 2 might be World War II, giving you a fresh start - Coding builds upon the lessons of the previous chapter. You have to use lesson 1 material to succeed in lesson 2. Chapter 10 builds upon and uses material from chapter 9. If you let your pride get in the way you will be too lost to recover and have wasted thousands of dollars in tuition.

This post has been edited by tlhIn`toq: 03 April 2013 - 05:40 PM

Was This Post Helpful? 1
  • +
  • -

#4 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: star search C++ program, How to not use Global Variables

Posted 03 April 2013 - 05:50 PM

This is in no way correct syntax (consider it pseudo code), but maybe it can get you moving a little forward:
main()
{
  double scores[7]
  getJudgeData(scores)
 
}

getJudgeData(double *scores)
{
 n = scores size
 for i < n
   print "Enter score for judge " + (i+1) + " between 0 and 10."
   cin >> score[i]
     while(score[i] < 0 || score[i] > 10)
        cin >> score[i]

}



The rest could follow this same type of design.

This post has been edited by IngeniousHax: 03 April 2013 - 05:51 PM

Was This Post Helpful? 1
  • +
  • -

#5 rhsjr_25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-April 13

Re: star search C++ program, How to not use Global Variables

Posted 03 April 2013 - 07:12 PM

Thanks everyone for your help. I just spoke with my teacher and she said the function prototypes we have to use are:

void getJudgeData(string j, double &);
double calcScore(double, double, double, double, double);
double findLowest(double, double, double, double, double);
double findHighest(double, double, double, double, double);


I'm basically going to start from scratch and try to put this thing together.

Thanks, again.

This post has been edited by JackOfAllTrades: 04 April 2013 - 03:06 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1