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
  • C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats...

    C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats paper; paper beats rock. You must use these specific functions in your program. These are the prototypes: char generateP2toss(); int checkThrow(char, char); void printStatistics(int, int, int, int); void finalStatistics(int, int, int, int); void welcome(); Notes on these functions: generateP2toss() will generate the computer's toss (the computer is player2). It returns either an "r", "p", or "s/" checkThrow(char char) will check the throw by passing...

  • 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...

  • I'm trying to write this program in C++ and I'm having trouble initializing the overload function...

    I'm trying to write this program in C++ and I'm having trouble initializing the overload function I have to use. Here's the assignment and what I got so far, thanks for the help! /*2. Implement the game of Rock-Paper-Scissors in C++, where you can play against the computer. a. Scissors beat paper, paper beats rock, and rock beats scissors. If both you and computer choose the same tool, it is a tie. b. Your program should have a class game...

  • 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....

  • (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....

  • 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...

  • 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...

  • 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...

  • 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...

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