help with output

i'm very new to the programming community and i have an assignment to write a program with a function that gives the score of a bowling game a message.
this is what i have for the function:

void onegamescore(int a, ofstream& outfile)
{
while (a<=50)
outfile << "Horrible Game" << endl;
while (a<=100)
outfile << "Poor Game" << endl;
while (a<=150)
outfile << "Good Game" << endl;
while (a<=200)
outfile << "Very Good Game" << endl;
while (a<=250)
outfile << "Excellent Game" << endl;
while (a<=300)
outfile << "Professional Game" << endl;
return;}

all of the messages print no matter what the score. i've tried them as if statements as well. i only need one rating to print. all i got from my professor is that it's supposed to be a void function but i don't see how that helps.
please help me.
Try this

1
2
3
4
5
6
7
8
9
10
11
12
   if (a <= 50 && a >= 0)
		outfile << "Horrible Game" << endl;
	if (a <= 100 && a > 50)
		outfile << "Poor Game" << endl;
	if (a <= 150 && a > 100)
		outfile << "Good Game" << endl;
	if (a <= 200 && a > 150)
		outfile << "Very Good Game" << endl;
	if(a <= 250 && a > 200)
		outfile << "Excellent Game" << endl;
	if (a <= 300 && a > 250)
		outfile << "Professional Game" << endl;


Also, you dont need return there, remove it. Since its a void function, it doesnt return anything.
thank you so much. this worked but i'm getting another error for another function that says i can't use it as a function. is there something wrong with this function?
using namespace std;
int validgroup (int a, int b, int c, ofstream& outfile)

{ int valid=0;
if (a<=0 && a>=300)
outfile << "This score is Invalid" << endl<< endl <<endl;
valid=0;
if (b<=0 && b>=300)
outfile << "This score is Invalid" << endl<< endl <<endl;
valid=0;
if (c<=0 && c>=300)
outfile << "This score is Invalid" << endl<< endl << endl;
valid=0;
if (valid=1)
outfile << "The Group is Valid" << endl;
else
outfile << "The Group is Invalid"<< endl;
return valid;
}
In the future, please post your code between code tags - http://www.cplusplus.com/articles/jEywvCM9/

Yes, There are lots of things that could be worked on. You start off by doing int valid = 0; which is all good. But why do you keep making it 0 over and over and over again for no reason?

1
2
3
4
5
6
7
8
outfile << "This score is Invalid" << endl << endl << endl;
	valid = 0;
	if (b <= 0 && b >= 300)
		outfile << "This score is Invalid" << endl << endl << endl;
	valid = 0;
	if (c <= 0 && c >= 300)
		outfile << "This score is Invalid" << endl << endl << endl;
	valid = 0;


This - if (valid = 1) // use == not =


1
2
3
valid = 0;
if (valid = 1)
outfile << "The Group is Valid" << endl;


What were you thinking here? Becuase you make valid 0, and then you ask if its equal to 1? Well ofc it isint! :D

After initializing valid to 0, you never change it again.
Last edited on
I don't know honestly. I had help with this part but I think I will need it later to implement a counter to count the invalid and valid groups. But that's not my problem now. This won't work as a function in my main program. I get an error that says validgroup cannot be used as a function.
Well there is nothing wrong with the actual function. Where are these functions? Doesnt look like they are in a class, are they in the main.cpp? In that case, have you prototyped them?
Topic archived. No new replies allowed.