【问题标题】:Is my understanding of how a variable works, wrong?我对变量如何工作的理解是错误的吗?
【发布时间】:2020-08-22 19:00:50
【问题描述】:

你好。我对 C++ 和一般编程非常陌生。我试图通过玩弄我已经理解的小组件来建立我的信心,直到我在学习更多之前尽可能地熟悉。然而,我对自己感到困惑和沮丧,因为我似乎无法让这段简单的代码工作。我希望任何读到这篇文章的人都能理解我的尝试,并能告诉我哪里出错了。谢谢,麻烦您了。

#include <iostream>

int main(int argc, const char * argv[]) {


    char computer[100];
    char something[100];



    double price;
    double price2;
    double total = (price + price2);
    double budget;
    if (budget < total){ double need = total - budget;}
    if (budget > total ){ double surplus = budget - total;}
    double perday = total / 365;
    double remain = budget - perday;
    int A;

    std::cout << "Please tell me, the 'name' of the new computer that you would like to buy.\n";
    std::cin >> computer;
    std::cout << " How much does this " << computer << " cost?\n";
    std::cin >> price;
    std::cout << "Cool! Now tell me something else you would like to buy.\n";
    std::cin >> something;
    std::cout << "How much does " << something << " cost?\n";
    std::cin >> price2;
    std::cout << "I need to know your technology budget:\n £";
    std::cin >> budget;


    if (budget >= total)
    {std::cout << "Wow you have enough cash to get both " << computer << " and " << something << "\n with a surplus budget of £" << surplus;}

    else (budget < total) {
    std::cout << "I am sorry you lack the necessary funds right now. \n";
    std::cout << "Would you like to hear a payment plan?\n 1 for yes / 2 for no \n";
    std::cin >> A;
    }

    if (A = 1) {
            std::cout << "If you wanted to buy both " << computer << " and " << something << " \n by this time next year, you could pay £" << perday << "each day from now\n ";
            std::cout << "If I take today's payment now, you will have £" << remain << "left of your budget";}
        else return 0;}
}

【问题讨论】:

  • "我希望读到这篇文章的人能理解我在尝试什么,并能告诉我哪里出错了。" 首先:你为什么这么认为,任何事情是错的?请描述预期输出、实际输出和提供的输入。
  • 你是在设置变量之前对它们求和......你真的应该从基础开始......learncpp.com
  • 你好 Algirdas,谢谢。
  • 我认为您误解的一件事是变量仅存在于声明它们的范围内。当声明它们的程序部分(又称范围)关闭时,它们将停止存在。
  • 程序打印了所有输出而不接受输入(只有第一个 std::cin 被读取并按预期在下一行打印)我是否要声明全局变量以使其工作?我将查看 XraySensei 刚刚发送的链接,因为到目前为止我只使用 codeacademy。

标签: c++ variables syntax


【解决方案1】:

首先在一个范围内声明一个变量是一个坏习惯,除非你打算只在那个范围内使用那个变量。

if (budget < total){ double need = total - budget;}
if (budget > total ){ double surplus = budget - total;}

在这种情况下,needsurplus 仅存在于这些 { } 之间,不能在它们之外使用。如果您想稍后在程序中使用它们,请确保将它们声明在范围之外。

我注意到的另一件事是,在编程语言中赋值运算符 = 用于向变量添加值。在您的情况下,if (A = 1) 将不起作用,因为 cpp 不会检查 A 是否为 1,但它会将 A 分配为 1。您应该做的是检查 if (A == 1) 与双 ==Check here for operators

我建议为 cpp 阅读一本好书,并在此过程中变得更好。

如果您想知道,这里是您的程序的working 版本

【讨论】:

  • 非常感谢 anthino12。我现在有了更好的理解。
  • “在一个范围内声明一个变量是一个坏习惯,除了 [...]” 这是非常具有误导性的建议。变量应始终在包含其所有用途的最内层范围内声明。
  • 关于你的working 版本:最后的else return 0; 看起来有点误导。一方面,这使得if 分支没有return。另一方面,main() 是唯一一个具有非void 返回类型且不需要return 的函数。 (默认情况下假定return 0;。)因此,简而言之,您可以完全省略else return 0;。 (关于格式,我什至不确定你是否知道这一点。)
  • @Scheff 我同意你的看法。我没有注意这一点,我只是修复了有错误的行并运行它。很棒的评论,值得一读
  • 好的,感谢今天帮助我的所有人。从今天早上开始,我重写并通过简化代码和声明全局变量成功执行(这是我今天学到的一件事)我意识到我还有很长的路要走,但我学习的方式是玩它和它搏斗,感谢您的意见建议和鼓励。
猜你喜欢
  • 1970-01-01
  • 2022-01-16
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
相关资源
最近更新 更多