1

I'm a beginner programmer, so this is going to look messy, but I keep getting the problem that is mentioned in the title. No matter where I try to put endl; it keeps giving me the same error. Also when I run the code my total for the second store comes out right but the first store total does not. Any idea on how to fix this? I'm using codeblocks on a windows 7 computer.

#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;
kotAPI
  • 1,073
  • 2
  • 13
  • 37
user3255581
  • 11
  • 1
  • 2

3 Answers3

0

Did you try something like

cout << "Total number of widgets at each store starting with :";
cin >> widgetStores;
cout << endl; //Added this

cin doesn't have a << operator, so you need to send it to cout.

Edit: I found the error you're having. I guess that you are trying to put in lines as literally

endl;

and that doesn't go anywhere...

chrisb2244
  • 2,940
  • 22
  • 44
  • That's pointless since the input operation will end with a newline anyway (since you have to hit Enter before the input is accepted). – ooga Jan 31 '14 at 01:19
  • The question asks how to get an `endl` working. Not whether it's relevant. Maybe he wants a line of space between each pair of input/output. – chrisb2244 Jan 31 '14 at 01:21
0

The only compile error this program has, is that you're using widgetsLeftS1W1, widgetsLeftS1W2, widgetsLeftS2W1 and widgetsLeftS2W2 before initializing them.

enter image description here

You probably need = instead of -=.

When you say

widgetsLeftS1W1 -= (widgetStore1-numberSoldFirst1);

what you actually mean is

widgetsLeftS1W1 = widgetsLeftS1W1 - (widgetStore1-numberSoldFirst1);

The computer doesn't know the value of widgetsLeftS1W1, so it gives you the error.


Conclusion: use

widgetsLeftS1W1 = (widgetStore1 - numberSoldFirst1); 
Oleksiy
  • 37,477
  • 22
  • 74
  • 122
  • That is not a compile error, in fact a diagnostic isn't even required. It is undefined behavior, but the best you'll get out of the vast majority of compilers is a warning. – jerry Jan 31 '14 at 01:30
  • @jerry it is definitely a compile error. See the screenshot – Oleksiy Jan 31 '14 at 01:33
  • While I should have been precise and said that the standard doesn't **require** this to be treated as an error, I stand by my assertion that most compilers will only emit a warning (if anything) by default. [GCC happily compiles it](http://ideone.com/MW4pdj), for instance. You seem to be using Visual Studio, even there [C4700 is only a warning by default](http://msdn.microsoft.com/en-us/library/axhfhh6x.aspx). I'm guessing you've turned on a "treat warnings as errors" option. – jerry Jan 31 '14 at 03:58
0

Try initializing the values of

widgetsLeftS1W1
widgetsLeftS1W2
widgetsLeftS2W1
widgetsLeftS2W2

with zero while declaring them at the top.

lorro
  • 10,687
  • 23
  • 36
DrDonkey
  • 45
  • 1
  • 8