d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Functions C++ > C++
Add Reply New Topic New Poll
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Feb 20 2013 07:35pm
int main file:

//Main int() file


#include <iostream>
#include <string>
#include "ProtoP4.h"

using namespace std;


int main()
{
char goAgain;

//Writes my header once
WriteHeader();

//Seeds my generator once
SeedRNGen();

int topRange;

do
{
//Asks the user for the high end of the guessing range
topRange = AskRange();

//Runs the guessing game, telling the game the top range value
GuessingGame(); //HW sheet has topRange inside of GuessingGame();


//Asks the user if they would like to play again
cout << "Would you like to go again (y/n) : ";
cin >> goAgain;
cin.ignore();

}while(goAgain = 'y');

cout << "\n\nThanks for playing!\n" << endl;

return 0;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FunctionList file

//.h file for Prototype functions
/

#include <string>


using namespace std;

void WriteHeader();
void SeedRNGen();
int AskRange();
int GuessingGame();
int ObtainMagicNumber();

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Functions file

//.cpp file for Functions



#include <iostream>
#include <string>
#include <cmath>
#include <ctime>


using namespace std;

//Writes out my course header as a function
void WriteHeader()
{
cout << "My name is .\n"
<< "This is C++ Programming.\n"
<< "The title is and number is Program 4.\n"
<< "The objective is to program a guessing game for the user.\n\n";
}

//SeedRNGEN function
void SeedRNGen()
{
srand((unsigned)time(0));
}

//AskRange function
int AskRange()
{
int topRange;
cout << "\n\nPlease enter the max range value: ";
cin >> topRange;
cin.ignore();
return topRange;
}


//Obtain MagicNumber function
int ObtainMagicNumber(int topRange)
{
int MagicNum = rand()%AskRange();
return MagicNum;
}

//GuessingGame function
int GuessingGame(int MagicNum, string Guess)
{

int user_guess;
int guesses = 0;

do
{

cout << "\nEnter your guess: ";
cin >> user_guess;
cin.ignore();
++guesses;

if(user_guess = MagicNum )
{
cout << "\nCorrect!";
}
else if(user_guess > MagicNum )
{
cout << "\nToo High!";
guesses++;
}
else if(user_guess < MagicNum )
{
cout << "\nToo Low!";
guesses++;
}
else
{
cout << "\nWoah, that isn't in the range!";
}

}while(guesses < 5);

return ;
}


Getting these errors and others if I try doing a different way:
error C2561: 'GuessingGame' : function must return a value
see declaration of 'GuessingGame'

I've tried making GuessingGame a string instead of int.
Looked at various examples online and videos yet to no avail.
My instructor teaches us by having us read from the book instead of going over the sections and key components as a class.
I can't understand the concept of functions, calling functions, returns, etc..
If you have a easy understanding of functions please go ahead and explain.

This post was edited by DARK_SPAWN on Feb 20 2013 07:36pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 20 2013 07:39pm
return ;

thats the last line of your GuessingGame function. as you can see, it's not returning anything, but you declared it to return an int
Member
Posts: 4,024
Joined: Apr 1 2009
Gold: 64,464.00
Feb 21 2013 01:00am
Quote (carteblanche @ Feb 20 2013 09:39pm)
return ;

thats the last line of your GuessingGame function. as you can see, it's not returning anything, but you declared it to return an int


This.


After typing 'return' the compiler is saying "return what?!"..

It's your job to say what to return:

Code
return x // where x is some integer -> lets say x is 0.. so return 0 would be sufficient for an entry level program like this.


This post was edited by Twisted454 on Feb 21 2013 01:02am
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Feb 22 2013 01:39pm
Change the return type of GuessingGame to void since you don't need to return anything.

int GuessingGame(int MagicNum, string Guess) becomes void GuessingGame(int MagicNum, string Guess)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll