这个问题在这里已经有了答案:
Calling a function in main
(4 个回答)
3年前关闭。
我是一个新的相对较新的程序员,我决定用 C++ 编写代码。目前,我正在开发一个转换器应用程序项目。我想从金钱转换为体重,再转换为温度,如果可能的话,甚至更多。我写了一个很好的代码块,我想检查它是否有效。出于某种原因,代码确实编译了,但所有应用程序确实给了我一个空白屏幕,没有区域可以输入值或任何东西。它没有显示我写的说明或任何东西。
这是代码:
#include "std_lib_facilities.h"
int a = 0;
int b = 0;
int c = 0;
int money()
{
cout << "This will convert CAD to either USD or EUR\n";
cout << "Please input USD or EUR followed by an enter keystroke for conversion\n";
string a;
while(cin >> a){
if( a == "USD"){
cout << "If you would like to convert USD into CAD, enter 1.\n";
cout << "If you would like to convert CAD into USD, enter 2.\n";
int x;
cin >> x;
if( x == 1){
cout << "Please enter the amount you wish to convert.\n";
double j;
cin >> j;
double k = j*1.29;
cout << j << "USD is " << k << "CAD.\n";
}
if ( x == 2){
cout << "Please enter the amount you wish to convert.\n";
double o;
cin >> o;
double p = o*0.77;
cout << o << "CAD is " << p << "USD.\n";
}
}
if( a == "EUR"){
cout << "If you would like to convert EUR into CAD, enter 1.\n";
cout << "If you would like to convert CAD into EUR, enter 2.\n";
int y;
cin >> y;
if(y == 1){
cout << "Please enter the amount you wish to convert.\n";
double g;
cin >> g;
double h = g*1.46;
cout << g << "EUR is " << h << "CAD.\n";
}
if(y == 2){
cout << "Please enter the amount you wish to convert.\n";
double z;
cin >> z;
double x = z*0.69;
cout << z << "CAD is " << x << "EUR.\n";
}
}
}
}
int weight()
{
cout << "This will convert pounds to kilograms.\n";
cout << "Please input the amount you wish to convert.\n";
}
int temperature()
{
}
int setup()
{
cout << "Please enter either a,b or c for the corresponding conversions.\n";
cout << "(Where a is for currency, b is for weight, and c is for temperature.\n";
string a;
while ( cin >> a){
if( a == "a"){
int money();
}
if( a == "b"){
int weight();
}
if( a == "c"){
int temperature();
}
}
}
int main()
{
cout << "Welcome to the ultimate converter app.\n";
int setup();
return 0;
}
任何帮助,将不胜感激。
编辑:抱歉给大家带来了麻烦。感谢所有回答的人,我刚刚意识到我声明了一个函数而不是调用它。
最佳答案
替换 int setup();
至setup();
在 main()
.int setup();
只是声明函数,但不要调用它。
您还必须更正setup()
:
void setup()
{
cout << "Please enter either a, b or c for the corresponding conversions." << endl;
cout << "Where a is for currency, b is for weight, and c is for temperature." << endl;
string str;
while (cin >> str) {
if (str == "a") {
money();
}
else if (str == "b") {
weight();
}
else if (str == "c") {
temperature();
}
else {
cout << "Invalid input." << endl;
}
}
}
money()
, weight()
和 temperature()
必须返回 void
也输入。
关于没有调用 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46595825/