C++ - formula in a while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zelmila19
    New Member
    • Apr 2008
    • 16

    C++ - formula in a while loop

    How do i write the formula for calculating the total sales and average for this program:

    [code=cpp]
    while (sales !=-1)
    {
    i <= 12; i++;
    cout << fixed << setprecision(2) << "Please enter your sales for month " << i << ": ";
    cin >> sales;
    } //end while

    i <= 12; i--;
    cout << "For " << i++ << " months: ";

    cout << fixed << setprecision(2) << "Total sales: $ " << totalSales << endl;
    cout << fixed << setprecision (2) << "Average sales: $ " << average << endl;

    [/code]

    Can someone help me if I'm doing the right thing here because it is giving some kind of warning but dont know why. Please help!
    Last edited by sicarie; Apr 21 '08, 01:06 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Code:
    using namespace std;
    using std::fixed;
    using std::setprecision;
    No big deal, but the second two using directives are unnecessary here.
    Code:
    	i <= 12; i--;
    What are these two statements meant to accomplish? Are you trying to implement a for loop?

    Beyond that, post the text of the warning you're getting.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Generally, you cannot use operators like ==, !=, >, <, <=, >=, etc wth floating point numbers.
      Due to the automatic rounding, these operators may report a condition as true when the numbers
      are only close in value.

      Google for Floating Point Arithmetic or read IEEE 754 standard specification for details.

      To compare two floating point numbers, you have to establish a sigma error tolerance.

      Code:
      if (fabs(i - j) < 0.000001) { ... // almost equal }

      Comment

      • zelmila19
        New Member
        • Apr 2008
        • 16

        #4
        Originally posted by scruggsy
        Code:
        using namespace std;
        using std::fixed;
        using std::setprecision;
        No big deal, but the second two using directives are unnecessary here.
        Code:
        	i <= 12; i--;
        What are these two statements meant to accomplish? Are you trying to implement a for loop?

        Beyond that, post the text of the warning you're getting.
        No am not trying to implement a for loop I just want to use a while loop but I want the months to automatically just come as Month1...Month2 ...etc.
        The program is giving me this warning:

        warning C4552: '<=' : operator has no effect; expected operator with side-effect

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          i <= 12 will be either true or false. That is, this will evaluate to 0 or 1. Plus the 0 or 1 is not used so the whole statement is pointless. That's the essence of the compiler warning.

          Comment

          • zelmila19
            New Member
            • Apr 2008
            • 16

            #6
            Originally posted by weaknessforcats
            i <= 12 will be either true or false. That is, this will evaluate to 0 or 1. Plus the 0 or 1 is not used so the whole statement is pointless. That's the essence of the compiler warning.
            Thanks guys but i still cant get the expected results cos I do not know how to code the calculations for the total sales and average sales.......cou ld someone give me an idea on how to do that???

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              For starters, you have one sales variable. If you want sales by month I would expect you would need 12 sales variables. Maybe in an array. That way the 5th variable could be May.

              All you have to do is display the variables. Add them up and divide by 12 and you have average monthly sales. etc...

              Comment

              Working...