방금 C++로 어지럽게 시작했지만 막혔습니다. 어떤 도움이라도 대단히 감사 할 것입니다. 다른 C++ 함수를 통해 함수 변수 변경
나는 그러나, 다른 함수에 전달하여 false
에 true
에서 gameState의 값을 변경하려면
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
void changeState(bool gameState){
string answer;
cout << "End game? Y/N" << endl;
cin >> answer;
if(answer == "Y"){
gameState = false;
} else if (answer == "N"){
gameState = true;
}
}
void gameLoop(){
bool gameState = true;
while(gameState == true){
cout << "Game is being played" << endl;
changeState(gameState);
}
}
int main(){
gameLoop();
return 0;
}
패스 할 수있다. – sharyex
[참조로 전달할 때와 값으로 전달하는 경우의 차이점은 무엇입니까?] (http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing- by value) – user4581301
또는 gameState를 전역 변수로 만들 수 있습니다 (예 : "bool gameState = true;"행을 이동하여 "changeState void (bool gameState) {"행 위에 나타남) –