【发布时间】: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。