转到一个新块
C++ Go to a new block?
我是一个完全的c++新手,如果这是可能的,我想从int main()切换到int game(),我不确定这是否完全可能。
#include <iostream>
#include <cstdlib>
using namespace std;
string name;
int main()
{
//So this is where we get the users name.
string namechoice;
cout << "Hello adventurer." << endl;
cout << "What is your name?n";
cout << "n";
cout << "n";
cout << "n";
cout << "n";
cout << " Name: ";
cin >> name;
system("CLS");
cout << "So your name is " << name << ", Correct? (Y / N)n";
cin >> namechoice;
namechoice[0] = toupper(namechoice[0]);
system("CLS");
if (namechoice == "Y"){
}
else if (namechoice == "N"){
while(namechoice == "N"){
cout << "Please enter your name: ";
cin >> name;
system("CLS");
cout << "You're name is " << name << ", Correct? (Y / N)n";
cin >> namechoice;
namechoice[0] = toupper(namechoice[0]);
system("CLS");
}
}
}
int game()
{
cout << "Test";
return 0;
}
那么我要问的是,如果我在int main中的任何一个条件最终都满足了,我如何去int game()。
提前感谢!
把game()
;去你想去的地方。它将调用相应的函数,将控制权转移给它。虽然如果你想在main
之后保留game
函数,你需要在main
之前像int game();
一样的前向声明,这样main
就知道它是什么样子的,以及如何调用它。
int game(); // forward declaration
int main() {
if (condition) {
// calling a function transfers control to it.
int result = game();
// After its return, the flow continues from where we _stopped_.
// You can print game's result, if you wish.
cout << result << "n";
} else {
// do something else
}
return 0;
}
int game() {
cout << "Testn";
// _return_ will "terminate" this function's flow and return control back
// to the caller function.
return 0; // This value will be returned to the caller function.
}
您需要添加#include <sstream>
来输入字符串!你可以在任何地方添加游戏功能,它将在扫描的线上被调用。另外一个很好的做法是在底部添加main函数,以及使用void关键字。下面是我的例子:
void game(){
cout<<"Test";
}
int main(){
game();
return 0;
}
相关文章:
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 如何使用CLion在Mac上创建一个新的.txt文件
- 重新定位图像时如何前进到下一个内存块
- 将一个 QWidget 链接到另一个新创建的 QWidget
- 使用Visual Studio在虚幻引擎中创建一个新的类c ++给了我太多的错误
- C++,一个内存块与多个内存块
- C++,每个循环初始化一个新的静态变量
- 如何在C++中将用户数据从一个 Lua 块传递到另一个块
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- 在SFML(C++)中启动一个新窗口
- 有没有一种方法可以从函数中返回一个新对象或对现有对象的引用
- r-在Rcpp和C++之间转换矢量(使用Rcpp::as或Rcpp:::wrap)是否会创建一个新的矢量并复制元素
- 实现一个函数,该函数将字符串作为输入并返回一个新字符串,辅音字母不替换为 "!"
- 如何编译和运行一个新的 C++ Actor 框架项目?
- 当我尝试在 tensorflow 中添加一个新操作时如何 #include "include/libxsmm.h"?
- 从给定矢量的块中生成一个新矢量
- 在输入一个语句块时会创建一个新的堆栈帧吗
- 转到一个新块
- 是否可以将一个新分配的块初始化为0