gpt4 book ai didi

c++ - 函数中的表达式语法错误帮助! C/C++

转载 作者:行者123 更新时间:2023-11-30 19:09:06 26 4
gpt4 key购买 nike

请帮助我处理我的开关盒,我不确定如何将我的功能放入开关盒中。我要保留参数吗?

void ChoiceConvert(char unit){      

char c, f;

printf("Enter your choice for converting from Kelvin\n1. 'c' to convert to Celcius\n2. 'f' to convert to Farenheit");

switch(unit)
{
case 'c':
ConvertCel(int temp, const int Freeze);
break;
case 'f':
ConvertFar(int temp, const int Freeze);
break;
default:
printf("\n");
}

return;}

这是我收到的错误。

错误 E2188 state.cpp 53:函数 ChoiceConvert(char) 中的表达式语法

错误 E2188 state.cpp 56:函数 ChoiceConvert(char) 中的表达式语法

最佳答案

您的代码存在一些问题。第一个:此函数旨在转换温度 - 您传递想要将其转换为的字符单位 - C 或 F。SO

char c, f;    
printf("Enter your choice for converting from Kelvin\n1. 'c' to convert to
Celcius\n2. 'f' to convert to Farenheit");

根本不应该在函数内部 - 应该在函数之前询问,然后将答案传递到 ChoiceConvert 中。

第二:这里没有温度。我假设您也应该在 main() 中询问用户,并将其作为参数和单位传递。

第三:正如其他人所说,您正在编写函数参数,而不是其参数。您应该将参数(即函数声明和定义)放在该函数的外部,然后在该函数内调用它。 Check this out in order to learn the difference between declarations, definitions, and calling.这是编程非常重要的基础知识。

我会这样做:

int main() 
{
char conversion;
int temperature;

printf("Enter your choice for converting from Kelvin\n1. 'c' to convert to Celcius\n2. 'f' to convert to Fahrenheit");
std::cin >> conversion; // requires #include <iostream>
printf("Enter the temperature you wish to convert:");
std::cin >> temperature; // requires #include <iostream>

ChoiceConvert(temperature, conversion); // calling the function
}

void ChoiceConvert(double temp, char unit)
{
switch(unit)
{
case 'c':
ConvertCel(temp);
break;
case 'f':
ConvertFar(temp);
break;
default:
printf("\n");
}
}

void ConvertCel(double temp)
{
// conversion here
}
void ConvertFar(double temp)
{
// conversion here
}

另外,UI 旁注 - 请注意:如果您正在寻找用户输入字符,将其设置为编号列表可能会造成困惑。

关于c++ - 函数中的表达式语法错误帮助! C/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43826519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com