具有用户输入的控制台 UI
Console UI with User input
所以我正在尝试制作一个简单的控制台应用程序,要求用户提供某些特征。第一个问题,询问用户的年龄。例如,它应该看起来像"我>进入年龄
#include <iostream>
#include <string>
using namespace std;
//Variables
int Age;
//Functions
int AgeEnt(){
cin >> Age;
return Age;
}
//Main
int main (){
cout << "Welcome! Please enter your age to continuen";
cout << "I am " << AgeEnt << " years old";
return 0;
}
这会自动将 1 放在年龄应该的位置。我将如何到达可以在文本之间输入数字的位置?我仍然是初学者,所以如果这在控制台中是不可能的,或者非常贬值,请原谅我。
int main (){
cout << "Welcome! Please enter your age to continuen";
int age;
cin >> age;
return 0;
}
另外,为什么要在 main 上方声明函数和变量?除全局函数外,这在C++中不需要
#include <iostream>
using namespace std;
//Functions
int AgeEnt(){
int Age;
cin >> Age;
return Age;
}
//Main
int main (){
cout << "Welcome! Please enter your age to continuen";
AgeEnt();
cout << "I am " << AgeEnt() << " years old";
return 0;
}
几件事
- 为此,您不需要
<string>
头文件。它没有在任何地方使用。 - 无需将
Age
声明为全局变量。它只在一个函数中使用,所以在其中声明它。
另外,虽然我在这里没有做过,但我通常会做一些类似int a = AgeEnt(); cout<<a;
相关文章:
- C++:将控制台输出存储在宏中更好吗
- 如何在MS Visual Studio 2019中运行QT UI
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- C++控制台应用程序阻止退出
- 如何修复此iFile以将.txt输出到控制台
- 为什么控制台要求输入,即使代码中没有输入
- 如何处理使用.ui文件生成的.h文件
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 根据变量使Qt UI中的复选框为已选中/未选中
- 控制台输出在 Qt5 中未正确显示
- 一旦双簧管录制开始,主 UI 线程就会被阻止
- C++出现控制台错误.我无法识别源代码的问题
- C++程序不会从 Windows 控制台运行
- 有人可以向我解释为什么控制台输出 0 吗?
- Visual Studio 2017 停止工作,并在打开后显示许多控制台窗口
- Qt Quick,如何更改 Ui 源代码?
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- 具有用户输入的控制台 UI
- 模拟按钮从控制台单击UI类