我不明白这个错误,我已经在前几行声明了变量我想知道如何获取它以执行语句

问题描述 投票:0回答:1

即使我已经声明了变量,我也不明白这个错误

int total;    
int op;
int f;
int c;
int k;

cout<<" Type the number of corresponding conversion: ";
 cin>>op;



    if(op == 1)
    { cout<<"\nEnter Fahrenheit: ";
      cin>>f;
      cout<<endl<<(f-32) * .5556 = total<<"°C";
    }
    if(op == 2)
    {
    cout<<"\nEnter Celcius: ";
    cin>>c;
    cout<<endl<<(c*1.8)+32=total<<"°F";

    }
     if(op == 3)
    {
        cout<<"\nEnter Fahrenheit: ";
        cin>>f;
        cout<<endl<<5/9(f-32)+273=total<<"K";

    }
     if(op == 4)
    {
        cout<<"\nEnter Kelvin: ";
        cin>>k;
        cout<<endl<<(k-273.15)*9/5+32=total<<"°F";

    }
    if(op == 5)
    {
        cout<<"\nEnter Celcius: ";
        cin>>c;
        cout<<endl<<c+273.15=total<<"K";

    }
    if(op == 6)
    {
       cout<<"\nEnter Kelvin: ";
       cin>>k;
       count<<endl<<k-273.15=total<<"°C";

    }
     else
     {
cout<<"\nInvalid Input";
     }

我尝试了重新排列和更改,我期待如何在最少的帮助下让它工作

c++ if-statement variables
1个回答
0
投票

导致你的代码无法正常运行的原因有很多 -

  1. 缺少
    iostream
    cin 和 cout 的导入
  2. 命名空间
    std
    如果没有它,您需要使用
    std::cout
    而不是
    cout
  3. 缺少程序中所需的主要驱动程序功能
  4. 条件语句需要使用 if-elseif-else 块正确构建
  5. 考虑到输入可以采用浮点形式,所有其他输入和输出都需要采用浮点形式:例如:23.5 度
    Celsius
    并具有相应的
    Fahrenheit
    输出
    • 此外,考虑到有人可能会错误地输入不正确的浮动选项(
      op
      ),您的操作也需要处于浮动状态才能到达
      Invalid input
#include <iostream>
using namespace std;

int main() {
    float total;
    float op;
    float f;
    float c;
    float k;

    cout << "Type the number of corresponding conversion: ";
    cin >> op;

    if (op == 1) {
        cout << "\nEnter Fahrenheit: ";
        cin >> f;
        total = (f - 32) * 5 / 9;
        cout << endl << total << "°C";
    }
    else if (op == 2) {
        cout << "\nEnter Celsius: ";
        cin >> c;
        total = c * 9 / 5 + 32;
        cout << endl << total << "°F";
    }
    else if (op == 3) {
        cout << "\nEnter Fahrenheit: ";
        cin >> f;
        total = 5 * (f - 32) / 9 + 273;
        cout << endl << total << "K";
    }
    else if (op == 4) {
        cout << "\nEnter Kelvin: ";
        cin >> k;
        total = (k - 273.15) * 9 / 5 + 32;
        cout << endl << total << "°F";
    }
    else if (op == 5) {
        cout << "\nEnter Celsius: ";
        cin >> c;
        total = c + 273.15;
        cout << endl << total << "K";
    }
    else if (op == 6) {
        cout << "\nEnter Kelvin: ";
        cin >> k;
        total = k - 273.15;
        cout << endl << total << "°C";
    }
    else {
        cout << "\nInvalid Input";
    }

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.