2 Replies - 473 Views - Last Post: 04 December 2014 - 09:03 PM Rate Topic: -----

#1 tonyal  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-September 14

How to make a function prototype that uses variables by reference

Posted 04 December 2014 - 08:48 PM

Hi, I'm working on a project for school for a class where we only do pseudocode but for this we have to use a programming language. I was lucky enough for someone to show me how to do most of the structuring so I could use those as an example for the rest but was unsure of how to how to make a function prototype that uses variables by reference. I'm making a decision based game where two running totals of two variables (ending and morality from decisions made) will decide the game outcome. I only have a few modules put in so far and most of the "story" parts cut down here to save space. Could someone give an example or tell me where one is to do what I need (as simple as possible - I have NO experience with C++). I'm also getting an error saying there is more than one instance of overloaded function for the "whatToDo" module. My partner decided to drop so I'm pulling my hair out - thank you in advance!

// ZombieGame.cpp : Defines the entry point for the console application.
//
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <iostream>
using namespace std;

//These are function prototypes to declare the functions being used
void WakeyWakey();
void TwentyMinsLater(int);
void FightClub(int);
void OmgZombies(int, int);
void WhatToDo(int, int, int);
void GettingTheHellOutOfDodge(int, int);
void TimeToGo(int, int);
void CarryOnMyWaywardSon();
void CantStopHereThisIsZombieCounty(int, int);
void MmmPie(int, int);
void TimesAreHard(int, int, int);
void OhWhatABeautifulLife(int, int, int);


//WhatToDo is fully stated here since I do not know how to make a function prototype that uses variables by reference
void WhatToDo(int Choice, int &Moral, int &End)
{
	int Number;
	/* initialize random seed: */
	srand (time(NULL));
	Number = rand() % 4 + 1; 
	do
	{
		cout <<  "Alright...e.\n";
		cout <<  "Itís time to go to..\n";
		cout <<  "1) Search apartment..\n";
		cout <<  "2) Call family to see if theyíre in town..\n";
		cout <<  "3) Nothing. Let's get to killing undead!\n";
		cin >>  Choice;
		
		if (Choice == 1)
		{
			 
			if (Number == 1)
			{
				cout <<  "You find some canned food. Good thing you went to the market yesterday.\n";
				End = End + 5;
			}
			else if (Number == 2)
			{
				cout <<  "You find a ton of supplies. Good thing....\n";
				End = End + 10;
			}
			else if (Number == 3)
			{
				cout <<  "You find bare shelves and..\n";
			}
			else if (Number == 4)
			{
				cout <<  "You find a book titled 'How...\n";
			}
		}//end If
		else if (Choice == 2)
		{
			cout <<  "You try to call your mom and..\n";
			cout <<  "They might still..\n";
			Moral = Moral + 10;
		}
		else if (Choice == 3)
		{
			cout <<  "You canít seem to think of anything..\n";
	}//End IF
	
	}while (Choice < 1 || Choice > 3);
		
}

int main()
{
	int Choice = 0;
    int Ending = 0;
    int Morality = 0;
	
	WakeyWakey();
	TwentyMinsLater(Choice);
	//other functions here in void format so they don't return a value
	FightClub(Choice);
	OmgZombies(Choice, Ending);
	WhatToDo(Choice, Morality, Ending);
	GettingTheHellOutOfDodge(Choice, Ending);
	TimeToGo(Choice, Ending);
	CarryOnMyWaywardSon();
	CantStopHereThisIsZombieCounty(Choice, Ending);
	MmmPie(Choice, Ending);
	TimesAreHard(Choice, Morality, Ending);
	OhWhatABeautifulLife(Choice, Morality, Ending);

	return 0;
}

void WakeyWakey()
{
	//Display that they woke up
		cout << "Your alarm clock wakes you up....\n \n";
		cout << "Why not just ...\n \n";
		cout << "Huh, that's odd.....\n \n";
		cout << "Right .. \nSomething feels...off.\n \n";
		cout << "There is....\n \n";
}

void TwentyMinsLater (int Choice)
{
	do
	{
		cout << "Twenty minutes later..\n \n";
		cout << "Nothing good is on TV..\n \n";
		cout << "Something bangs on your door...\n \n";
		cout << "1) Go and open the door to see what the person wants.\n";
		cout << "2) Look through the peep-hole.. \n";
		cin >> Choice;
		
		
		if (Choice == 1 || Choice == 2)
		{
			cout << "Before you can do anything....\n";
			cout << "He's trying to bite...\n";
		}
	}while (Choice < 1 || Choice > 2);
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to make a function prototype that uses variables by reference

#2 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 984
  • Joined: 18-April 09

Re: How to make a function prototype that uses variables by reference

Posted 04 December 2014 - 09:01 PM

Quote

//WhatToDo is fully stated here since I do not know how to make a function prototype that uses variables by reference


Why not just declare a function prototype like the rest (from line 9 to line 20) ? like this:


void WhatToDo(int Choice, int &Moral, int &End);


then put the function definition like what you are already doing for the other functions after your main() function. Try looking at thistutorial.
Was This Post Helpful? 0
  • +
  • -

#3 tonyal  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-September 14

Re: How to make a function prototype that uses variables by reference

Posted 04 December 2014 - 09:03 PM

Thank you, I will do that. Having no knowledge of C++ I just went with what he showed me. Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1