问题
For the record I am completely green in C++ and any other programming language for that matter, so if it's possible I'd be happy if you can answer in a way that I can understand :)
I have been working on a simple rock, paper, scissors game recently where I have 3 basic functions, one for the user, one for the bot, and one that choose which one wins the game.
I know using system("cls")
isn't the best way to go, but I'm not planning on using this outside Windows.
The final function results()
need to use the variables x
and brand
from the two previous functions, however I can't seem to find a way to do this. And where people explain it anywhere else, they explain it too advanced for me. Remeber that I don't need to change any of them, I simply have to compare them to determine the winner.
I'll show you my code here so you can see what you are dealing with. Please give me comments on other things I can improve here.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
using namespace std;
int bgame(), ugame(), results();
int main()
{
srand(time(NULL));
cout<<"Welcome to RPS!\n" <<"You know what to do.\n" <<"\n[ENTER]";
cin.ignore();
system("cls");
ugame();
return 0;
}
int ugame()
{
int x;
cout<<"Type a number from 1-3: ";
cin>> x;
cin.ignore();
if ( x == 1 )
{
cout<<"\nYou chose rock!\n" <<"\n[ENTER]";
cin.ignore();
bgame();
}
else if ( x == 2)
{
cout<<"\nYou chose paper!\n" <<"\n[ENTER]";
cin.ignore();
bgame();
}
else if ( x == 3 )
{
cout<<"\nYou chose scissors!\n" <<"\n[ENTER]";
cin.ignore();
bgame();
}
else
{
cout<<"\nTry again.\n" <<"\n[ENTER]";
cin.ignore();
ugame();
system("cls");
}
return 0;
}
int bgame()
{
int brand = rand()>>4;
system("cls");
cout<<"The bot will now choose an item.\n" <<"\n" <<"[ENTER]\n";
cin.ignore();
brand = rand() % 3 + 1;
if ( brand == 1)
{
cout<<"\nBot chose rock!";
cin.ignore();
results();
}
else if ( brand == 2 )
{
cout<<"\nBot chose paper!";
cin.ignore();
results();
}
else if ( brand == 3 )
{
cout<<"\nBot chose scissors!";
cin.ignore();
results();
}
else
{
cout<<"\nError.";
cin.ignore();
bgame();
system("cls");
}
return 0;
}
int results()
{
}
回答1:
you can send "parameters" to a function. That's create a copy of your variable and you can use it in another function.
You need to specifies it with your function name (that's named prototype) like this :
int results(int x, int brand)
You put a type name and a variable name. For example this function will take 2 int as parameters.
int results(int x, int brand)
{
// do something with your variables
}
And when you call the function you type:
results(x, brand);
If you want, this link explains it with images and examples and more details: http://www.cplusplus.com/doc/tutorial/functions/
回答2:
First, learn how to use functions with parameters.
After that, you should be able to do what you want.
As suggested, you only have to do something like this :
declaration :
int bgame(int x){
... what bgame do ...
}
and in ugame :
int ugame(){
int x;
...
cin >> x;
...
bgame(x);
}
Hope this helps.
回答3:
Use parameters, pass by value. Have parameters in the function header, so you can pass the value into the funcion.
using namespace std;
int bgame(int x_p), ugame(), results(int x_p, int brand_p);
int main()
{
srand(time(NULL));
cout<<"Welcome to RPS!\n" <<"You know what to do.\n" <<"\n[ENTER]";
cin.ignore();
system("cls");
ugame();
return 0;
}
int ugame()
{
int x;
cout<<"Type a number from 1-3: ";
cin>> x;
cin.ignore();
if ( x == 1 )
{
cout<<"\nYou chose rock!\n" <<"\n[ENTER]";
cin.ignore();
bgame(x);
}
else if ( x == 2)
{
cout<<"\nYou chose paper!\n" <<"\n[ENTER]";
cin.ignore();
bgame(x);
}
else if ( x == 3 )
{
cout<<"\nYou chose scissors!\n" <<"\n[ENTER]";
cin.ignore();
bgame(x);
}
else
{
cout<<"\nTry again.\n" <<"\n[ENTER]";
cin.ignore();
ugame();
system("cls");
}
return 0;
}
int bgame(int x_p)
{
int brand = rand()>>4;
system("cls");
cout<<"The bot will now choose an item.\n" <<"\n" <<"[ENTER]\n";
cin.ignore();
brand = rand() % 3 + 1;
if ( brand == 1)
{
cout<<"\nBot chose rock!";
cin.ignore();
results(x_p, brand);
}
else if ( brand == 2 )
{
cout<<"\nBot chose paper!";
cin.ignore();
results(x_p, brand);
}
else if ( brand == 3 )
{
cout<<"\nBot chose scissors!";
cin.ignore();
results();
}
else
{
cout<<"\nError.";
cin.ignore();
bgame(x_p);
system("cls");
}
return 0;
}
int results(int x_p, int brand_p)
{
// Do whatever you want to do with x (x_p) and brand (brand_p).
}
回答4:
For you, you only need to return 1 value for each function, so return would work. Instead of return 0;, use return x; and return brand;. In the comparing function, define two new variables, such as y; or crand;, then assign them like this: y=int ugame(); and crand=int bgame();. Then y=x and crand=brand and you can work from there. So then you don't need any parameters or anything. Also, don't try to define brand straight out, just declare int brand; and let the randomizing function (which must be seeded with srand by the way) assign a value to brand. You also don't have to define the functions as int; they will define themselves.
来源:https://stackoverflow.com/questions/17718601/c-how-do-i-use-variables-from-one-function-to-another