3 Replies - 516 Views - Last Post: 25 November 2009 - 08:08 PM Rate Topic: -----

#1 astroshark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 25-November 09

Problem with calculating scores!

Post icon  Posted 25 November 2009 - 03:44 PM

Hi I manage to create some code to where it will read in a set of exam scores and display their category as well as if they enter an invalid number it repeats the code until the data is valid or until they enter the SENTINEL value but when I do it crashes after reading in 1 number and it does not repeat the loop if they enter in a invalid number.

// C05PP04.cpp

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

const int SENTINEL = -999;

int getData(int score, string category);
string getCategory (int score, int amount, int & numOutstanding, int & numSatisfactory, int & numUnsatisfactory);

int main()
{
	int score = 0;
	int score2;
	int amount = 0;
	int numOutstanding = 0, numSatisfactory = 0, numUnsatisfactory = 0;
	string category;
	category = getCategory (score, amount, numOutstanding, numSatisfactory, numUnsatisfactory);
	score2 = getData(score, category);
	cout << score2 << endl;
// Display final summary results
	return 0;
}
//This function asks the user for a score (or sentinel value) and loops until the
//user types in the correct date - data validation loop
int getData(int score, string category)
{
	do
	{
		cout << "Enter in exam scores from 0-100, -999 stops the program!" << endl;
		cin >> score;
		cout << "Your score is " << score << "Category: " << category << endl;
	}
	while(score < 0 || score > 100 && score < -999 || score > -999);
	return 0;
}
//This function is passed the score and returns the corresponding category.  It also
//increments the appropriate counter variable
string getCategory (int score, int amount, int & numOutstanding, int & numSatisfactory, int & numUnsatisfactory)
{
	if(score >= 90 && score <= 100)
	{
		cout << "Outstanding!" << endl;
		numOutstanding++;
	}
	else if(score >= 60 && score <= 89)
	{
		cout << "Satisfactory!" << endl;
		numSatisfactory++;
	}
	else if(score >= 0 && score <= 59)
	{
		cout << "Unsatisfactory!" << endl;
		numUnsatisfactory++;
	}
	amount++;
	cout << "The number of Outstanding scores is: " << numOutstanding << "." << endl;
	cout << "The number of Statisfactory scores is: " << numSatisfactory << "." << endl;
	cout << "The number of Unsatisfactory scores is: " << numUnsatisfactory << "." << endl;
	cout << "The total amount of scores entered is: " << amount << "." << endl;
	return 0;
}


This post has been edited by astroshark: 25 November 2009 - 03:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with calculating scores!

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Problem with calculating scores!

Posted 25 November 2009 - 05:39 PM

I think its because you tried to return an integer in this function

string getCategory (int score, int amount, int  numOutstanding, int  numSatisfactory, int  numUnsatisfactory)
{
	if(score >= 90 && score <= 100)
	{
		cout << "Outstanding!" << endl;
		numOutstanding++;
	}
	else if(score >= 60 && score <= 89)
	{
		cout << "Satisfactory!" << endl;
		numSatisfactory++;
	}
	else if(score >= 0 && score <= 59)
	{
		cout << "Unsatisfactory!" << endl;
		numUnsatisfactory++;
	}
	amount++;
	cout << "The number of Outstanding scores is: " << numOutstanding << "." << endl;
	cout << "The number of Statisfactory scores is: " << numSatisfactory << "." << endl;
	cout << "The number of Unsatisfactory scores is: " << numUnsatisfactory << "." << endl;
	cout << "The total amount of scores entered is: " << amount << "." << endl;
}


When you should return a string

This post has been edited by Aphex19: 25 November 2009 - 05:39 PM

Was This Post Helpful? 1
  • +
  • -

#3 astroshark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 25-November 09

Re: Problem with calculating scores!

Posted 25 November 2009 - 06:37 PM

Ok I got it working but now i'm trying to calculate the average but i'm having difficulty with it as its also calculating in the SENTINEL value how do I make it to where it won't calculate the SENTINEL value in the average?

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

const int SENTINEL = -999;

void getData(int score_data);

int main()
{
int outstanding_score = 0;
int satisfactory_score = 0;
int unsatisfactory_score = 0;
int score_data = 0;
int average;
int sum = 0;

while(score_data != SENTINEL)
{
	cout << "Enter first score (0-100, -999 stops): " << endl;
	cin >> score_data;
if(score_data >= 90 && score_data <= 100) 
{ 
	cout << "Outstanding!" << endl;
	outstanding_score++; 
}
else if(score_data >= 60 && score_data <= 89)
{ 
	cout << "Satisfactory!" << endl;
	satisfactory_score++;
}
else if(score_data >= 0 && score_data <= 59)
{
	cout << "Unsatisfactory!" << endl;
	unsatisfactory_score++;
}
}
sum = sum + score_data;
average = outstanding_score + satisfactory_score + unsatisfactory_score;
average = sum / average;
cout << "The number of outstanding scores is " << outstanding_score << "." << endl;
cout << "The number of satisfactory scores is " << satisfactory_score << "." << endl;
cout << "The number of unsatisfactory score is " << unsatisfactory_score << "." << endl;
cout << "The average score is " << average << "." << endl;

return 0;
}

void getData(int score_data)
{
	do
	{
			cout << "Enter exam score (1-100, -999 stops): " << endl; 
			cin >> score_data;
	}
		while(score_data < 0 && score_data > SENTINEL || score_data < SENTINEL || score_data > 100);
}


Was This Post Helpful? 0
  • +
  • -

#4 astroshark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 25-November 09

Re: Problem with calculating scores!

Posted 25 November 2009 - 08:08 PM

alright I was able to fix it thx Aphex19 for trying to help me out
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1