我是初学程序员,所以这看起来很混乱,但我一直在解决标题中提到的问题。无论我在哪里尝试endl
;它一直给我同样的错误。此外,当我运行代码时,我对第二个商店的总数是正确的,但第一个商店总数没有。有关如何解决此问题的任何想法?我在Windows 7计算机上使用代码块。
#include <iostream> //Allows cout/cin
#include <ctime> //Allows time
#include <iomanip> //Allows setprecision
using namespace std;
int main()
{
//Include header
//Input variables
double widgetStores;
double numberSoldFirst1;
double numberSoldFirst2;
double numberSoldSecond1;
double numberSoldSecond2;
double widgetsLeftS1W2;
double widgetsLeftS2W1;
double widgetsLeftS2W2;
//Start Clock
clock_t begin, end;
double time_spent;
begin = clock();
//Prompt for total number in stores
cout << "Total number of widgets at each store starting with :";
cin >> widgetStores;
double widgetStore1=widgetStores;
double widgetStore2=widgetStores;
double widgetsLeftS1W1;
//Prompt for amount sold during first and second week
cout << "How many widgets were sold at Store 1 the first week? ";
cin >> numberSoldFirst1;
cout << "How many widgets were sold at Store 1 the 2nd week? ";
cin >> numberSoldSecond1;
cout << "How many widgets were sold at Store 2 the first week? ";
cin >> numberSoldFirst2;
cout << "How many widgets were sold at Store 2 the 2nd week? ";
cin >> numberSoldSecond2;
//Calculate Number of widgets
widgetsLeftS1W1-=(widgetStore1-numberSoldFirst1);
widgetsLeftS1W2-=(numberSoldFirst1-numberSoldSecond1);
widgetsLeftS2W1-=(widgetStore2-numberSoldFirst2);
widgetsLeftS2W2-=(numberSoldFirst2-numberSoldSecond2);
//Display Values
cout << "Store 1 has " << widgetsLeftS1W2 << " widgets left after the 2nd week.";
cout << "Store 2 has " <<widgetsLeftS2W2 << " widgets left after the 2nd week.";
//Show time elapsed
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
cout << setprecision(2) << fixed << "****Elapsed time:" <<time_spent/60 << "minutes****";
return 0;
答案 0 :(得分:0)
你有没有试过像
这样的东西cout << "Total number of widgets at each store starting with :";
cin >> widgetStores;
cout << endl; //Added this
cin没有&lt;&lt;运算符,所以你需要将它发送给cout。
编辑:我发现了您遇到的错误。我想你正试图按字面意思插入行
ENDL;
这不会去任何地方......
答案 1 :(得分:0)
此程序唯一的编译错误是您在初始化之前使用widgetsLeftS1W1
,widgetsLeftS1W2
,widgetsLeftS2W1
和widgetsLeftS2W2
。
您可能需要=
而不是-=
。
当你说
时widgetsLeftS1W1 -= (widgetStore1-numberSoldFirst1);
你的意思是
widgetsLeftS1W1 = widgetsLeftS1W1 - (widgetStore1-numberSoldFirst1);
计算机不知道 widgetsLeftS1W1
的值,因此它会给您错误。
结论:使用
widgetsLeftS1W1 = (widgetStore1 - numberSoldFirst1);
答案 2 :(得分:0)
尝试初始化
的值widgetsLeftS1W1
widgetsLeftS1W2
widgetsLeftS2W1
widgetsLeftS2W2
在将它们声明为顶部时为零。