Question

How to call a function in another function?C++ Project assistance. In the Project I am attempting...

How to call a function in another function?C++ Project assistance. In the Project I am attempting to pass getOutcome into runGame to attempt to figure out who would win. Upon trying I get an error that getOutcome isn't in scop of runGame. The & symbols were my last attempt at trying to pass it, I do see it is wrong, and any help that can help me with just that is very appreciated.

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>

using namespace std ;

void runGame(int gamemode) // DECLARING WHAT KIND OF GAME WILL BE PLAYED
{
string name;

if(gamemode == 1)
{
cout << "Enter a name for Player 1 : " ;
cin >> name ;

int round , oddRound , choice ; // These variables are on the outside of the do loop
// so the "Do-While" will work.
do
{

cout << "Best of how many rounds? ODD ROUNDS ONLY : " ;
cin >> oddRound ;
round = oddRound % 2 ; // check for remainders to see if ODD or EVEN #

if (round != 0 )
{
cout << "Enjoy \n" ;

for(round = 0 ; round < oddRound ; round++) // Game will run for however many times the user requested
{
cout << "Rock (1) , Paper (2) , Scissors (3)? " ; //prompting user for choice
cin >> choice ;
&getOutcome(&choice , &computer) ; //ATTEMPTING to pass the choice to getOutcome

}
}
else if(round == 0)
{
cout << "My guy read! Try again! \n" ;
}

}while(round == 0);
} // END OF GAME MODE 2

void getOutcome(int choice , int computer)
{
int rock, paper, scissors , wins, compWins, losses, compLosses ;
rock = 1;
paper = 2;
scissors = 3;

if((choice == rock && computer == scissors) || (choice == paper && computer == rock) || (choice == scissors && computer == paper))
{
cout << " wins this round! \n " ; //PUT NAME's HERE
wins++;
compLosses++;
}
else if ((choice == scissors && computer == rock)|| (choice == rock && computer == paper) || (choice == paper && computer == scissors))
{
cout << "loses this round! \n " ; //PUT NAME"S HERE
compWins++ ;
losses++;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In order to call one function inside another function, first you need to define or declare the function which you will be calling in another function, because the c++ compiler follows top-down approach, it start compiling from the top of the program and when it finds some function call, it looks for that function in the above code, if any function gets matched it call that function. You cannot call the function inside another function if you are declaring the called function below the calling function. You need to give the declaration or defination of the called function above the calling function.

For e.g there are two approaches to call one function inside another function :

Suppose you want to call first() inside second(), then you can do this in two ways

1) By declaring and defining the called function above the calling function

void first(){ /* You can change the return type of the function,if you want to get some values returned from the function and change the return type appropriately,here i am using void so i need not to return any value.*/

//statements

}

void second{

first(); //function called within another function

}

2) Second approach is by first declaring the called function above the calling function and then defining it below the calling function.

void first(); //function only declared but not defined

void second(){

first();

}

void first(){ //function defined

//statements

}

/* In your code given,simply add this line below using namespace std */

void getOutcome(int choice , int computer);

Add a comment
Know the answer?
Add Answer to:
How to call a function in another function?C++ Project assistance. In the Project I am attempting...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • i have created a program for a game of rock, paper, scissors but i must make...

    i have created a program for a game of rock, paper, scissors but i must make it run more than once in some kind of loop. i want to add a function named runGame that will control the flow of a single game. The main function will be used to determine which game mode to initiate or exit program in Player vs. Computer, the user will be asked to enter their name and specify the number of rounds for this...

  • This is a C++ question need to complete project 2. attached the completed simple version of...

    This is a C++ question need to complete project 2. attached the completed simple version of the program below but need the updated version which is listed in the project 2 outline Project 2 Rock, Paper, Scissors Updated Take the solution to Lab 3 and make sure the user enters a letter of R, P, or S and the computer generates a number between 0-2 and changes that number to a letter of R, P or S, using a switch....

  • Use Dev C++ for program and include all of the following. Im lost. Make sure the...

    Use Dev C++ for program and include all of the following. Im lost. Make sure the user enters a letter of R, P, or S and the computer generates a number between 0-2 and changes that number to a letter of R, P or S, using a switch. Make sure the user knows why he/she has won or lost – print a message like “Paper covers Rock” or “Scissors cuts Paper”. Ask the user how many times he/she wants to...

  • (C++) Hey guys we just went over loops and it got me confused, it has me...

    (C++) Hey guys we just went over loops and it got me confused, it has me scrathcing my head for the past two days. I would appreciate any help you guys have to offer. Few places I have a hard time is get input and determine winner(cummulative part). Write a program that lets the user play the Rock, Paper, Scissors game against the computer. The computer first chooses randomly between rock, paper and scissors, but does not display its choice....

  • please help me fix this. I'm getting it all mixed up in user defined functions. here's...

    please help me fix this. I'm getting it all mixed up in user defined functions. here's the question. This week we'll write a C program that lets the user play the game of Rock, Paper, Scissors against the computer. Your program will call a user-defined function named display_rules that displays the rules of the game. It then proceeds to play the game. To determine the winner, your program will call another user- defined function called determine_winner that takes two integer...

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • Hello, I have this programing assignment that I have to do and I am having issues...

    Hello, I have this programing assignment that I have to do and I am having issues on how to solve it. The program has to be on Java. -thank you in advance. 1- The game Paper, Rock, Scissors is played between two people. Each person counts "one, two, three" and on three displays their hands as either flat - Paper, a fist - Rock or two spread fingers - Scissors. The winner is determined by these rules: Paper covers Rock...

  • Create the second part of a Rock, Paper, Scissors game. The game keeps playing as long...

    Create the second part of a Rock, Paper, Scissors game. The game keeps playing as long as the user enters 'Y'. Make this case sensitive; if they enter a lower case y the game will not continue. If the user enters in anything besides upper case Y the game will end. Your text must exactly match the examples below: Example 1 with correct input Let's play Rock, Paper, Scissors Enter 1 for rock, 2 for paper, 3 for scissors 2...

  • Please help I am struggling with this and 3*Math.random())+1; and how to use it within the...

    Please help I am struggling with this and 3*Math.random())+1; and how to use it within the program For this project you will write a Java program that will play a simple game of Rock, Paper, Scissors.  If you have never played this game before the rules are simple - each player chooses one of Rock, Paper or Scissors and they reveal their choices simultaneously. • The choice of Rock beats the choice of Scissors ("Rock smashes Scissors") • The choice of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT