简体   繁体   中英

trying to create a dice game using function c++

i am trying to have a function determine the result

a. If the numbers add up to 5, 7, or 12, then the player wins, and the function should return an indication of this (use some integer to represent a win). b. If the numbers add up to 2, 4, or 11, then the player loses, and the function should return an indication of this (once again, use an integer). c. If the numbers add up to anything else, then the game is a draw and the function should say this (by way of an integer).

question, do i need a different func for winner, loser, and draw?

and what how can i return a integer to main to let main know that if we have a winner a loser a draw.

just learning to program any help would be greatly appreciated

 //function
 int outcome(int, int)
    {
    int die1;
    int die2;
    int winner;
    int loser;
    int draw;

      if (die1&&die2==5||7||12)

      return 99;

      if (die1&&die2==2||4||11)

      return loser;

      else
      return draw;
      }



    // func to get a random number
    int rollDice()
    {
    int roll;

    roll = (rand()%6)+1;

    return roll;
    }

the main func

#include <iostream> 
#include <cstdlib> 
#include <fstream> 

using namespace std; 

int main() 
{ 
double die1=0; 
double die2=0;
int winner=0; //counter for winners   
int loser=0; //counter for losers
int draw=0; //counter for draw 

//func to determine the outcome 
int outcome(int, int); 

//func for random numbers 
int rollDice(); 

int outcome(int, int) 
if (return==99) 
    cout <<"winner";

Your code has several syntax errors. If I want to, say, make a function to add two integer numbers, I'd do something like this:

int add(int a, int b) //VARIABLES MUST CARRY A NAME!
{
    return a+b;
}

If you want to work with conditions, do this:

if(a==5 && b==6 || a==6 && b==7) //Just as an example

Your fixed condition would be this:

if (die1+die2==5 || die1+die2==7 || die1+die2==12)

Also, study variable scope. Let's say I have the following:

int main()
{
    int myVar = 1;
}

int anotherFunction()
{
    println("%d", myVar); //This will cause an error, because myVar doesn't exist here, it only exists in main()
}

These are the most notable errors I can see in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM