Function Prototypes

must return a value ?

Page 1 of 1

8 Replies - 553 Views - Last Post: 25 April 2009 - 06:40 PM Rate Topic: -----

#1 rachelw1023  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-April 09

Function Prototypes

Posted 25 April 2009 - 03:11 PM

//GuessMyNumber3

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

//function prototypes
int getRandomNumber();
int getUserGuess();
void tooHigh();
void tooLow();
void congratulations(int);

int main()
{
	 int theNumber;
	 int guess;
	 int tries = 0;
	srand(time(0));

	theNumber = getRandomNumber();

	do
	{
		guess = getUserGuess();
		tries++;

		if(guess > theNumber)
			tooHigh();

		if(guess < theNumber)
			tooLow();
	
	}while(guess != theNumber);
   
	
	congratulations(tries);

	system("Pause");
	return 0;
}
	

	int guess, tries = 0, theNumber;

int getRandomNumber()
{
	srand(time(0));
	int theNumber = rand() % 100 + 1;
	
}

int getUserGuess()
{
		cout << "Enter a guess: ";
		cin >> guess;
		++tries;
	
}

void tooHigh()
{
	if (guess > theNumber)
		cout << "Too High!\n\n";
}

void tooLow()
{
	if (guess < theNumber)
		cout << "Too Low!\n\n";
}

void congratulations(int x)
{
	cout << "\nThat's it! You got it in " << tries << "guesses!\n";
	
}	

This post has been edited by rachelw1023: 25 April 2009 - 04:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Function Prototypes

#2 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Function Prototypes

Posted 25 April 2009 - 03:48 PM

Those are correct ways to write function prototypes, if that's what you're asking.
Was This Post Helpful? 0
  • +
  • -

#3 rachelw1023  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-April 09

Re: Function Prototypes

Posted 25 April 2009 - 04:09 PM

View PostBlakeJustBlake, on 25 Apr, 2009 - 02:48 PM, said:

Those are correct ways to write function prototypes, if that's what you're asking.

Actually this is a home work assignment. I am not really understanding C++ but I have to take it. This assignment asks
Complete the following program by writing the code for each function. You have been supplied with the function prototypes, function calls, and function headers. You need to write the code to make each function work.

Hint: No need to touch any code in the main function.

But when I wrote in the code the compiler tells me

Error 3 error C4716: 'getRandomNumber' : must return a value

Error 4 error C4716: 'getUserGuess' : must return a value

I have been reading my book and looking online trying to figure it out I dont expect anyone to give the right answer just point me in the right direction
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Function Prototypes

Posted 25 April 2009 - 04:18 PM

1 - Please edit your posting (see the edit button in lower right).
Delete all your code.
Get a fresh copy of the code with formatting in place from your editor / IDE.
Paste the formatted code between code tags like this :code:


2 - Have a look at this function:
void tooHigh()
{
if (guess > theNumber)
cout << "Too High!\n\n";
}


You passed nothing into this function so where do you think it will get the values for "guess" and "theNumber" from?
I think you may want to include something inside the parens ( "(" and ")" ).
What do you think?




OK look at this function:
int getRandomNumber()
{
srand(time(0));
int theNumber = rand() % 100 + 1;

}



What does the "int" at the beginning mean?
Was This Post Helpful? 0
  • +
  • -

#5 rachelw1023  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-April 09

Re: Function Prototypes

Posted 25 April 2009 - 04:26 PM

to be honest with you I dont even know like I said I am stuck in this class I have no idea what I am doing. I have been trying to get in touch with the tutor for the class but it has been like playing tag. I have to take this class to get to the core of networking for some reason. As far as the parenthesis I will try to mess around with them most of time when I get something to compile its an accident just by messing around with things.
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Function Prototypes

Posted 25 April 2009 - 04:31 PM

OK it's no worries.

I don't think you need to talk the tutor as much as you need to read the textbook.

I am 100% certain everything you need is covered in there.

But just to make sure you have the information. Have a read through this page and the next one (see the link at the bottom) and see if my questions make more sense then.
http://www.cplusplus...rial/functions/

EDIT
Just to be clear.
Feel free to come back and ask more questions.
This isn't a 'brush off'.
Just want to ensure we are all talking the same language.

This post has been edited by janotte: 25 April 2009 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Function Prototypes

Posted 25 April 2009 - 05:03 PM

To elaborate on what Janotte is saying: We don't want to give you the answer, we're here to help you learn, so we'll point you to where you might want to look, but we're not going to post the correct code for you without any guidance. Of course, this is not saying that's what you're trying to do. But try and take what we tell you as guidance toward understanding your problems better instead of just a solution that we figured out for you.
Was This Post Helpful? 0
  • +
  • -

#8 rachelw1023  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-April 09

Re: Function Prototypes

Posted 25 April 2009 - 05:54 PM

I am not trying to get you to give me the answer I stated that in my first post. I am trying to get a better understanding of these codes and what they do and why they do what they do... I am just frustrated because this is the only class that I dont understand. My book is crap and doesn't explain things very well. Or maybe I just dont understand the book either way I am feeling really frustrated thank you both for trying to help I didn't know what to expect when I came on here. I only have 3 weeks left of this class and will never have to look back as long as I pass.
Was This Post Helpful? 0
  • +
  • -

#9 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Function Prototypes

Posted 25 April 2009 - 06:40 PM

I'm sorry, I wasn't trying to make it sound like you were just trying to get an answer from us.

Could you try and ask some more questions that we could help answer, because I have a feeling programming would be more enjoyable for you if it was portrayed clearer to you.

Did janotte's link help you out? cplusplus.com is a great web site for c++ reference material. If things are still unclear, then ask questions and we'll answer them or direct you to somewhere with the answer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1