Jump to content
  • Advertisement
Sign in to follow this  
N0M4D

Need help with programming homework

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm a student at brown college for video game design. I have an assignment due tomorrow and have a problem. I missed the day they discussed functions in class. Our assignment is to create a rock paper scissors game in C++ windows console app using visual studio.net 2003. I'm totally brain farting. Please help...an example of functions would help tons!

Share this post


Link to post
Share on other sites
Advertisement
It is the policies of the forum not to help on homework questions, however, since you did not ask specifically ask for a solution to your problem but rather for theory, I'll bite.

A function is a "piece of reusable code that returns a value.


#include <iostream>

//This is a function that computes the square of some number.
int computeSquare(int number) {
int tempNumber;
tempNumber = number * number;
return tempNumber;
}

//We call the function in main()
int main() {
//Here we ask the user to input a number.
int someNumber;
int itsSquare;
std::cout << "Input a number: ";
std::cin >> someNumber;

//Here we call the function and compute the square of someNumber and put it in itsSquare.
itsSquare = computeSquare(someNumber);

//Here we write the result to the console.
std::cout << "The square of " << someNumber << "is " << itsSquare << ".";
return 0;
}




In this particular example, we have a function, computeSquare. Let's look at it's definition.

int computeSquare(int number)

This declare a function, called computeSquare, that takes an int as a parameter (called number) that returns another int. if we wanted it to return a double, we'd use double computeSquare(int number).

The return statement in the function is assigned to the l-value where it is called. Note that you can call a function any number of time in a program, and that you can call functions within other functions.

Share this post


Link to post
Share on other sites
ok sorry, didnt know the rules...ok so if main is a funtion then why is the int in front of main() also, im supposed to use voids? sorry im really lost

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can do both, int means that it must return an
integer value. From what i know the operating system
wants to have a 0 back when everything went allright.
so use return 0; to end the function.

Share this post


Link to post
Share on other sites
Quote:
Original post by N0M4D
ok sorry, didnt know the rules...ok so if main is a funtion then why is the int in front of main()

Typically, you'll close your program with return 0; somewhere in the body of the main() function. This tells the system that your program has finished without an error.

You could use return 1; or return 505; or whatever to finish the program and tell the system there was an error.

Quote:
also, im supposed to use voids? sorry im really lost

void is just a way of telling the compiler that a function takes no arguments and/or returns no value, for example:
void NoReturnedValue(int a_number) {
// ... do something ...
return; // leave function without returning a value
}

int RandomNumber(void) {
int a_number;
// ... random number generator goes here ...
return a_number;
}

int main(int argc, char* argv[]) {
int my_number = 505;
NoReturnedValue(my_number); // call the NoReturnedValue() function
my_number = RandomNumber(); // call the RandomNumber() function

RandomNumber(); // legal, but a bit useless

// illegal, because the RandomNumber() function doesn't take arguments
int my_other_number = RandomNumber(my_number);

return 0; // return from the main() function
}


Share this post


Link to post
Share on other sites
If you still need to learn about functions, you could try the excellent tutorial here (the whole series can be found here).

Functions have what is called a return type. This is some data that the function "returns" (or gives back if you want to think of it that way) after it is run. If you don't need to return anything, you can use void as the return type.

It's valid in C++ to use void main, but is bad practice and isn't a habit you should get into (even though you probably won't be returning anything until you're more advanced).


As for the rule regarding homework, it's to prevent us doing all the work for you, or giving you the answers, which wouldn't really help you learn at all. You can ask general questions and we'll help you out, but we won't do your homework for you is all. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Kazgoroth
It's valid in C++ to use void main, but is bad practice and isn't a habit you should get into (even though you probably won't be returning anything until you're more advanced).

Many compilers implement this as a language extension, but this is not standard C++, nor has it ever been. A fully compliant and portable program should have:

int main() or
int main( int argc, char **argv );

:stylin: "Make games, not war."

EDIT: It is valid in ISO C (ISO/IEC 9899:1999), though.

Share this post


Link to post
Share on other sites
I don't want my homework done for me, I wouldn't learn anything lol. I'm really amazed about the responses! I love you guys. One last question, how could I randomize words. Or say 1 = rock, 2 = paper and 3 = scissors? So I can have a cout << "The Computer chose " << Randmword << endl;

Share this post


Link to post
Share on other sites
You're going to need to produce a random number, then choose what to print based off of that number. Look at: "Random Function" and "Selection Statements"

Once you get that going, and play around with it for a while, it may be a good exercise to try and put your strings in an array. That way, you can refer to the string you want as an index into the array, and avoid using selection statements.

Start looking into <strings> and <vectors>; they are located in the standard c++ library in the STL. There are many places on GameDev to look, and plenty of tutorials on the net.

:stylin: "Make games, not war."

Share this post


Link to post
Share on other sites
Quote:
Original post by N0M4D
I don't want my homework done for me, I wouldn't learn anything lol. I'm really amazed about the responses! I love you guys. One last question, how could I randomize words. Or say 1 = rock, 2 = paper and 3 = scissors? So I can have a cout << "The Computer chose " << Randmword << endl;


You could use an enum for your random words.

enum Enum
{
item1;
item2;
item3;
};

Check this website for more info on enums.

Otherwise, you could create an array of strings, and initialize each item in the array to paper, rock, scissor.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!