【发布时间】:2014-01-31 01:04:32
【问题描述】:
我是一个初学者程序员,所以这看起来很乱,但我不断遇到标题中提到的问题。无论我尝试将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;
【问题讨论】:
-
你能显示导致错误的行吗?
-
我在你的程序中看不到任何 endl。粘贴您遇到错误的代码。
-
任何我放 endl 的地方都会导致错误,就像我在每个 cin 和 cout 之后都需要一个,但如果我把它们放在那里就会导致错误。
-
之后每个
cin?如:cin >> something >> endl;? -
在用所有
-=行减去它们之前,您还没有初始化任何widgetsLeftS1W1形式的变量。
标签: c++ function overloading