Help? Must return value? "error C4716

Started by
13 comments, last by moff 14 years, 11 months ago
wow okay i just fixed it randomly wow im so happy.... but really bad news... after i open it it shows intro screen then asks the player there name, i type in a name, and then instead of switching over and going on through the game.... the intro screen pops right back up asking there name again.... please help?!? will someone thats real smart at C++ codes please look at this and point out and Explainnnnnn whats wrong. be aware im very new to this.

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;


int Diceroll();
void Introscreen();
void Tryagain();
void Win();
		int Totalbears;
		int Dicenumber;
		int Useranswer;
		int Totalcorrect;
		string PLAYAGAIN;
		string NAME;
		bool PLAY = true;

void LOSE (string NAME)

{
		//the losing screen
		cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<< endl;
		cout<<"!                                      !"<<endl;
		cout<<"!        Sorry,                        !"<<endl;
		cout<<"!                 You Lose!            !"<<endl;
		cout<<"!                                      !"<<endl;
		cout<<"!                                      !"<<endl;
		cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl<<endl;
		cout<<endl;

		cout<<"SORRY "<<NAME<< " YOU LOST"<<endl<<endl;
		cout<<"BETTER LUCK NEXT TIME"<<endl;
}
		//the intoduction screen
void Introscreen()
{
		cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl;
		cout<<"!                                      !"<<endl;
		cout<<"!                                      !"<<endl;
		cout<<"!             Ice and Dice             !"<<endl;
		cout<<"!                                      !"<<endl;
		cout<<"!                                      !"<<endl;
		cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl<<endl;
		cout<<endl;

		cout<<"Game details:"<<endl;
		cout<<"The name is in the game, and the game is in the name."<<endl;
		cout<<"and the name of the game is Polar Bears Around An Ice Hole."<<endl;
		cout<<"Some people call this Petals Around A Rose."<<endl<<endl;
		cout<<endl;
		cout<<"Wish to play? Please enter your name here:"<<endl;
		cin>>NAME;
}

int Diceroll()
{
		Totalbears = 0;

		for (int numRoll = 1; numRoll<4; numRoll++)
		{
				Dicenumber=1+rand()%6;

				switch(Dicenumber)
				{
						case 1:
						{
								cout<<"*******"<<endl;
								cout<<"*     *"<<endl;
								cout<<"*  0  *"<<endl;
								cout<<"*     *"<<endl;
								cout<<"*******"<<endl;
								Totalbears = Totalbears+0;
								break;
						}

						case 2:
						{
								cout<<"********"<<endl;
								cout<<"*    0 *"<<endl;
								cout<<"*      *"<<endl;
								cout<<"* 0    *"<<endl;
								cout<<"********"<<endl;
								Totalbears = Totalbears+0;
								break;
						}

						case 3:
						{
								cout<<"*******"<<endl;
								cout<<"*  0  *"<<endl;
								cout<<"*  0  *"<<endl;
								cout<<"*  O  *"<<endl;
								cout<<"*******"<<endl;
								Totalbears = Totalbears+2;
								break;
						}

						case 4:
						{
								cout<<"*********"<<endl;
								cout<<"*  0 0  *"<<endl;
								cout<<"*       *"<<endl;
								cout<<"*  0 0  *"<<endl;
								cout<<"*********"<<endl;
								Totalbears = Totalbears+0;
								break;
						}

						case 5:
						{
								cout<<"*********"<<endl;
								cout<<"* O   O *"<<endl;
								cout<<"*   O   *"<<endl;
								cout<<"* O   O *"<<endl;
								cout<<"*********"<<endl;
								Totalbears = Totalbears+4;
								break;
						}

						case 6:
						{
								cout<<"********"<<endl;
								cout<<"* O  O *"<<endl;
								cout<<"* O  O *"<<endl;
								cout<<"* O  O *"<<endl;
								cout<<"********"<<endl;
								Totalbears = Totalbears+0;
								break;
						}

						default:
						break;
				}//end of switch
		}
		return 0;
}
void Win ()
{
		cout<<"Congratulations, "<<NAME<<", You have won! =D"<<endl;
}
void Tryagain()
{
		cout<<"Want to play again? (yes or no)"<<endl;
		cin>>PLAYAGAIN;

		if (PLAYAGAIN == "no")
		{
		PLAY = false;
		cout<<"Bye"<<endl;
		}
		if (PLAYAGAIN == "yes")
		{
		PLAY = true;
		cout<<"Okay!"<<endl;
		}
}
int main()
{
		srand((unsigned)time(NULL));
		
		string PLAYAGAIN = "yes";

		while (PLAYAGAIN == "yes")
		
		Introscreen();

		while(PLAY)
		{

				Totalcorrect = 0;

				for (int iCOUNT=1; iCOUNT<11; iCOUNT++)
				{

						if (Useranswer==3)
						{
								Win();
						}

						Diceroll();

						cout<<"How many polar bears do you see around the ice holes?"<<endl;
						cin>>Useranswer;

						if (Useranswer == Totalbears)
						{ 
								cout<<"Correct! =D"<<endl;
								Totalcorrect++;
						}
						else
						{
								cout<<"Incorrect! The right answer was: "<<Totalbears<<endl;
						}
						system("pause");

						Tryagain();
				}
		}		
return 0;
		}

[Edited by - Lorraine_B on May 1, 2009 11:03:00 PM]
Advertisement
P.S. sorry its so big and spaced like that, when i copyd and pasted it.. it got all crazy spaced O_O

so dont worry on my screen in the program its not crazy like that lol
Tell us, what do you think the difference is between these two hypothetical functions:
int Function();void Function();
im not 100% sure now that you bring that up
Quote:Original post by Lorraine_B
int function will return some information to whoever calls it

Exactly. In light of this, does it make any sense to make Introscreen() return an integer when you could simply mark it as not returning anything (using "void")? Especially when this value that is always zero and is ignored inside main().


Quote:
after i open it it shows intro screen then asks the player there name, i type in a name, and then instead of switching over and going on through the game.... the intro screen pops right back up asking there name again....

The answer to this is in the miscellaneous list of issues I made in my first response to your earlier thread.
Functions need to return values, that is a rule in C++. The void datatype is for functions that do not have to return a value. While you still have a return statement, it returns (to put it simply) nothing (like an empty set in mathematics).
okay do i put it like this:

void Introscreen()

?
if so... i get errors


cpp(104) : error C2562: 'Introscreen' : 'void' function returning a value


and a few others
Remember that you have included a declaration "void function()" and a definition "void function() { ... }" for each function. If you change the return type in one, then you must change it in the other.

As an alternative to this, you could delete the declarations and move the definitions above main, as each definition can double as a declaration:
int Diceroll(){    // code for Diceroll}int Introscreen(){    // code for Introscreen}void Tryagain(){    // code for Tryagain}void Win(){    // code for Win}// variablesint main(){    // main code}
wait what are the declarations?

well i moved the int main one to be last like you had it.

it works no errors....
but its stil doing the same thing, it wont get past the intro page, after it asks the persons name it just pops up again asking there name again

=(
Quote:Original post by Lorraine_B
wait what are the declarations?


A declaration is where you make a promise to the compiler. For example, your "Win" function. Its declaration is this:
void Win () ;

This means that Win is a function which takes no parameters and returns no values. You must declare a function before C++ will allow you to use it. Declarations most frequently used when breaking a larger program into multiple files, but occasionally must be used in the same file to resolve circular function calling (function A calls function B, and B also calls A).

The definition or implementation of a function is the actual body, like this:
void Win (){	cout<<"Congratulations, "<<NAME<<", You have won! =D"<<endl;}

As I said before, a definition counts as a declaration aswell. So if there is no circular calling, then you can re-order the functions to avoid needing to declare them. This is usually a good thing, because declarations duplicate information in multiple locations, forcing you to make changes in all locations when you update your code.

This caused the error message you had earlier, you changed one of the definitions but forgot to update the declaration.

This topic is closed to new replies.

Advertisement