当前位置: 首页 > 知识库问答 >
问题:

为什么我在C++中的函数没有被执行?

江飞章
2023-03-14

我有一个C++实验室,问题是:用户应该为X输入一个值(X是所持有的测试数)。如果x<15,程序不计算任何东西。如果X在16和30之间,程序应计算C=1.0/10.0*(24a);如果X>30,程序应计算C=0.15(24*a)。我的multiple if代码可以工作,但是当我输入X的值时,方程没有解出。有人知道吗??

#include<iostream>
#include<cmath>

#define _USE_MATH_DEFINES

using namespace std;

int main()
{
    //variables defined here
    float A, X, C, F;
    //A stands for number of classes scheduled

    //X is for number of tests and C is the modification 

    cout << "This program predicts the number of cars parked at HVCC at any given hour \n";
    cout << "enter a value for A \n";
    cin >> A;

    cout << "enter a value for number of tests X \n";
    cin >> X;

    if (X < 15)
    {
        cout << "No modificatons needed for under 15 tests \n";
    }

    else if (X > 15 && X < 20)
    {
        cout << "Approximation for between 15-30 tests \n";
        C = 1.0 / 10.0 * (24 * A);
        cin >> C;
    }

    else
    {
        cout << "Approximation for more than 30 tests \n";
        C = 0.15 * (24 * A);
        cin >> C;
    }
}

共有1个答案

杜绍元
2023-03-14

使用CIN读取用户输入。使用cout打印结果:

#include<iostream>
#include<cmath>

#define _USE_MATH_DEFINES

using namespace std;

int main()
{
    //variables defined here
    float A, X, C, F;
    //A stands for number of classes scheduled

    //X is for number of tests and C is the modification 

    cout << "This program predicts the number of cars parked at HVCC at any given hour \n";
    cout << "enter a value for A \n";
    cin >> A;

    cout << "enter a value for number of tests X \n";
    cin >> X;

    if (X < 15)
    {
        cout << "No modificatons needed for under 15 tests \n";
    }

    else if (X > 15 && X < 20)
    {
        cout << "Approximation for between 15-30 tests \n";
        C = 1.0 / 10.0 * (24 * A);
        cout << C; // replace cin >> with cout <<
    }

    else
    {
        cout << "Approximation for more than 30 tests \n";
        C = 0.15 * (24 * A);
        cout << C; // replace cin >> with cout <<
    }
}
 类似资料:
  • 我正在学习Java,正在使用java 8,spring 5.3.9和Apache Tomcat 9。我已经将我的jar文件添加到我的构建路径中的类路径中,将Apache Tomcat添加到我的服务器中,我的项目运行得非常好。现在我开始使用beans和xml文件,我遇到了一个问题。我的代码的一部分被触发,另一部分被忽略。 我有以下界面 FortuneService.java: 和一个快乐财富服务类:

  • 下午好, 在做了React教程,并阅读了所有React指南之后,我做了Redux教程来重新编写我的工作身份验证组件,以使用Redux而不是组件状态(我也从类切换到了功能组件)。 我的还原设置如下: 在我的登录组件中,我存储了身份验证令牌和用户名,如下所示: 如果Redux中没有存储数据,我的应用程序组件将显示登录表单;如果Redux中存在用户名和令牌,则显示用户名和令牌: 这一切几乎都奏效了。lo

  • 问题内容: 我有一个想法,可能是因为我正在做一些样式设计来更改单选按钮,但是我不确定。我正在设置一个onClick事件,该事件两次调用了我的函数。我已删除它以确保它没有在其他地方被触发,并且onClick似乎是罪魁祸首。 我的功能目前仅是运输选项的简单控制台日志: 如果没有任何理由可以在这里看到为什么会发生这种情况,我可以发布其余代码,但是有很多方面,我认为这与之无关,但是我认为这是一个很好的起点

  • 问题内容: 最近,我开始使用Python3,它缺乏xrange的好处。 简单的例子: 1) Python2: 2) Python3: 结果分别是: 1) 1.53888392448 2) 3.215819835662842 这是为什么?我的意思是,为什么xrange被删除了?这是学习的好工具。对于初学者来说,就像我自己一样,就像我们都处在某个时刻。为什么要删除它?有人可以指出我正确的PEP,我找不

  • 我使用surefire和failsafe分别执行单元测试和集成测试。所有测试都位于文件夹中。到目前为止,我有一个集成测试类,其测试方法(用@test注释)在所有单元测试运行时从不执行。这是我的pom的摘录。xml: 我使用maven目标来运行测试。

  • 我正在尝试将一个梅文Spring靴(2.3.12)应用程序从JUnit4转换为JUnit5。我已经阅读了很多关于如何做到这一点的不同帖子。 我能够在Eclipse中执行我的JUnit5测试。 我的问题是我无法让Maven Surefire执行我的JUnit5测试。我尝试了各种配置变体。当它到达Surefire步骤时,它只执行我以前的JUnit4测试,并且简单地忽略任何JUnit5测试。我已经验证了