• 0

[c++] help


Question

Well I am trying to make a program in which I have a user input two test scores then the program uses this formula to give me a score:

score = (pointsEarned/pointsPossible) * 100

I am lost in what else to do in my code and wondering if someone could help me with my missing parts.

#include <iostream>
using namespace std;

void getInput(int &pointsEarned, int &pointsPossible);
void printOutput(int score);
const int pointPossible = 200;

void main()			
{
   int pointsEarned, pointsPossible;
   float score;

   getInput(pointsEarned, pointsPossible);
   //enter code to call the function getInput 

   score = (pointsEarned/pointsPossible) * 100;
   printOutput(score);
}

void getInput(int &pointsEarned, int &pointsPossible)
{
	int test1, test2;

	cout  << "Enter first test score:\n";
	cin >> test1;
	cout << "Enter second test score:\n";
	cin >> test2;


	//enter code to request two test scores from the user.  
	//Store these values in pointsEarned and pointsPossible. 
}

void printOutput(int score)
{

	cout << "The student's score is " << score;

}

Link to comment
Share on other sites

14 answers to this question

Recommended Posts

  • 0

For one thing you seem a bit confused about the function of the program. I think that it's supposed to take in "points possible" and "points earned" and return a single score, but you are asking the user to put in two test scores.

Also, why not just do your cout and cin calls in main?

Link to comment
Share on other sites

  • 0

For one thing you seem a bit confused about the function of the program. I think that it's supposed to take in "points possible" and "points earned" and return a single score, but you are asking the user to put in two test scores.

Also, why not just do your cout and cin calls in main?

I have to make a program that has the user input 2 or more (i choose 2) test scores, then define points possible which I choose as 200, then divide those and multiply it by 100 to return the average to the user.

And I didnt do it in main because our teacher is trying to get us used to functions and parameters.. im just a n00b =p

Link to comment
Share on other sites

  • 0

I have to make a program that has the user input 2 or more (i choose 2) test scores, then define points possible which I choose as 200, then divide those and multiply it by 100 to return the average to the user.

And I didnt do it in main because our teacher is trying to get us used to functions and parameters.. im just a n00b =p

So what you want to input are something like pointsEarned1 and pointsEarned2, pointsPossible would just be defined in code as 200.

Then you would calculate score1 and score2 and output those.

Link to comment
Share on other sites

  • 0

Your telling me what I know, but I need help with the code & what im missing.

How about...

cout << "Enter the points earned for the first test.";

cin >> pointsEarned1;

cout << "Enter the points earned for the second test.";

cin >> pointsEarned2;

After changing the arguments for getInput() to be pointsEarned1 and pointsEarned2.

Then calculate both scores and pass them to your printOutput() function.

Also there is no need to declare pointsPossible in main or get any input for it since you have already declared it above as a constant and given it a value.

Link to comment
Share on other sites

  • 0
#include &lt;iostream&gt;
using namespace std;

const int _pointsPossible = 200;

float computeScore(int firstTestScore, int secondTestScore);
void printOutput(float score);

int main()                     
{
	// Create two int variables to handle the input test scores.
	int firstTestScore;
	int secondTestScore;

	// Prompt the user to enter the test scores.
	cout &lt;&lt; "\nPlease enter the first test score: ";
	cin &gt;&gt; firstTestScore;
	cout &lt;&lt; "\nPlease enter the second test score: ";
	cin &gt;&gt; secondTestScore;

	// Create a new float to hold the computed score,
	// and call computeScore() to calculate what the score is.
	float score = computeScore(firstTestScore, secondTestScore);

	printOutput(score);

	return 0;
}

float computeScore(int firstTestScore, int secondTestScore)
{
	// Add the two scores and convert the result to a float.
	// Divide that sum by the points possible and then multiply
	// by 100.
	return (((float)(firstTestScore + secondTestScore) 
		/ (float)_pointsPossible) * 100);
}

void printOutput(float score)
{
	// Output the computed result.
	cout &lt;&lt; "The student's score is " &lt;&lt; score &lt;&lt; " out of " 
		&lt;&lt; _pointsPossible &lt;&lt; "."; 
}

Link to comment
Share on other sites

  • 0

void getInput(int &amp;pointsEarned, int &amp;pointsPossible);

What prompted you to do pass-by-reference with these variables?

void main()			

Please don't do this. Generally you'd want a given application to return its exit code when it returns control to the OS. If nothing else, void main() is going to give you an extra warning message every time you compile. That is, unless you turn off warnings, and you shouldn't do that.

Link to comment
Share on other sites

  • 0

How about...

cout << "Enter the points earned for the first test.";

cin >> pointsEarned1;

cout << "Enter the points earned for the second test.";

cin >> pointsEarned2;

After changing the arguments for getInput() to be pointsEarned1 and pointsEarned2.

Then calculate both scores and pass them to your printOutput() function.

Also there is no need to declare pointsPossible in main or get any input for it since you have already declared it above as a constant and given it a value.

I tried your method in many ways and I kept getting errors for my pointsPossible.

#include &lt;iostream&gt;
using namespace std;

const int _pointsPossible = 200;

float computeScore(int firstTestScore, int secondTestScore);
void printOutput(float score);

int main()                     
{
	// Create two int variables to handle the input test scores.
	int firstTestScore;
	int secondTestScore;

	// Prompt the user to enter the test scores.
	cout &lt;&lt; "\nPlease enter the first test score: ";
	cin &gt;&gt; firstTestScore;
	cout &lt;&lt; "\nPlease enter the second test score: ";
	cin &gt;&gt; secondTestScore;

	// Create a new float to hold the computed score,
	// and call computeScore() to calculate what the score is.
	float score = computeScore(firstTestScore, secondTestScore);

	printOutput(score);

	return 0;
}

float computeScore(int firstTestScore, int secondTestScore)
{
	// Add the two scores and convert the result to a float.
	// Divide that sum by the points possible and then multiply
	// by 100.
	return (((float)(firstTestScore + secondTestScore) 
		/ (float)_pointsPossible) * 100);
}

void printOutput(float score)
{
	// Output the computed result.
	cout &lt;&lt; "The student's score is " &lt;&lt; score &lt;&lt; " out of " 
		&lt;&lt; _pointsPossible &lt;&lt; "."; 
}

Hmm seemed to work out until the last stage I entered two scores of 90 and 100 and it came out with wrong calculations; saying the student's score is 95 out of 200, which is wrong. I think it should have been 85 percent.

What prompted you to do pass-by-reference with these variables?

Please don't do this. Generally you'd want a given application to return its exit code when it returns control to the OS. If nothing else, void main() is going to give you an extra warning message every time you compile. That is, unless you turn off warnings, and you shouldn't do that.

Dunno I am taking problem solving concepts with C++ and programming, so its intro.. Our teacher is teaching us the basics and what we can do.

Link to comment
Share on other sites

  • 0

Hmm seemed to work out until the last stage I entered two scores of 90 and 100 and it came out with wrong calculations; saying the student's score is 95 out of 200, which is wrong.

Having it say 'out of 200' is an error on my part. I can't seem to go back and edit it though.

void printOutput(float score)
{
        // Output the computed result.
        cout &lt;&lt; "The student's score is " &lt;&lt; score &lt;&lt; " percent."; 
}

Besides that, though, how do you figure it's in error?

100+90=190

190/200=.95

.95 * 100 = 95

Link to comment
Share on other sites

  • 0

Oh crap your right wrong calculations lol.. I guess in the end I just needed to take out out of 200 and do what you said.

I pretty much understand why you did everything except for:

return (((float)(firstTestScore + secondTestScore) 
                / (float)_pointsPossible) * 100);

I did not know you can do that with the float; maybe im not that far yet.

Link to comment
Share on other sites

  • 0

return (((float)(firstTestScore + secondTestScore) 
                / (float)_pointsPossible) * 100);

I did not know you can do that with the float; maybe im not that far yet.

Putting a type in front like that is called casting. Try removing the floats and watch what happens.

Link to comment
Share on other sites

  • 0

Please don't do this. Generally you'd want a given application to return its exit code when it returns control to the OS. If nothing else, void main() is going to give you an extra warning message every time you compile. That is, unless you turn off warnings, and you shouldn't do that.

Not returning anything from main is a supported scenario in Visual Studio (along with a whole host of Microsoft extensions), so it isn't strictly speaking wrong if that is what he is using, nor will it raise an error (unless extensions are disabled). It is completely harmless, and the runtime will return 0 if you do so. That said, it is not portable, pointless, and is not something that should be taught.

Link to comment
Share on other sites

  • 0

Putting a type in front like that is called casting. Try removing the floats and watch what happens.

Not returning anything from main is a supported scenario in Visual Studio (along with a whole host of Microsoft extensions), so it isn't strictly speaking wrong if that is what he is using, nor will it raise an error (unless extensions are disabled). It is completely harmless, and the runtime will return 0 if you do so. That said, it is not portable, pointless, and is not something that should be taught.

Interesting thanks for the information.. When I progress to my next C++ class I will see if they do not teach that anymore or mention anything.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.