-1

I'm just learning C++ and I need this program to work for class. Logically it works. I'm using bloodshed.net's IDE... and it seems to just ignore the cin >> kwh;. I think its bloodsheds fault, not mine, but I could be mistake.

Here the code:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

    double kwh = -1, monthCost = 0, totalCost = 0;
    int monthCount = 0;
    bool nextMonth = 0;

    do
    {  
       kwh = -1;
       cout << "How many KWH did you use month " << ++monthCount << "?\n";
       cin >> kwh;

       if (kwh <= 1000 && kwh >= 0)
       {
               monthCost = kwh * 0.6;
       }

       else if (kwh < 0)
       {      
            cout << "\nInvalid number of KWH.";
            while (kwh < 0)
            {
                  cout << "How many KWH did you use month " << monthCount 
                          << "?\n";
                  // Could you tell me what I'm doing wrong? I honestly think its
                  // the programs fault, not mine. Driectly below this comment
                  // I'm asking the user for the KWH and it just ignores it
                  // when I compile and run. WHY?!?
                  cin >> kwh;
            }
       }

       else if (kwh > 1000)
       {
            monthCost = ((kwh - 1000) * 0.45) + 600;
       }


       totalCost += monthCost;
       cout << "\nYour electric bill for this month " << monthCount << " is $"
               << monthCost << ".\n";

       cout << "Would you like to enter another month? (1/0)\n";
       cin >> nextMonth;
       cout << "\n";
   }
   while (nextMonth == 1);

   cout << "\n\nYour total for all " << nextMonth << " month(s) is %"
           << totalCost << ".\n";
   system("pause");


}
mandelbug
  • 1,548
  • 5
  • 20
  • 32
  • 5
    There's no `cin << kwh;` in the code; I assume you meant the second `cin >> kwh;` bit? – user9876 Sep 20 '12 at 19:50
  • It works well on my case with ide Code:Blocks after removing `system("pause");` – nKandel Sep 20 '12 at 19:52
  • nothing. must be the compiler you are using. – Software_Designer Sep 20 '12 at 19:53
  • You're modifying the value of `kwh` inside of an `if` statement which evaluates `kwh`. Try doing your validation up front, before any of the calculations, and see if that helps ... if nothing else, it will certainly clarify the flow of your code :) – David Sep 20 '12 at 19:54
  • Thanks guys. I was going insane trying to figure out why it wasn't working. – mandelbug Sep 20 '12 at 19:54
  • To be clear: when you say, `cin >> kwh` is being ignored, do you mean that 1) it is allowing you to input a value, but doesn't seem to do anything with it? or 2) it is not even pausing to give you time to input a value? – Kevin Sep 20 '12 at 19:55
  • Possibly caused by this issue?: http://stackoverflow.com/questions/2525352/problem-of-using-cin-twice?rq=1 – user9876 Sep 20 '12 at 19:57
  • As an aside, please don't use such an ancient IDE and compiler (last release 2005). There are several free up-to-date alternatives which are superior in pretty much every way. –  Sep 20 '12 at 19:58

2 Answers2

3

If kwh < 0, then monthCost is never calculated in that instance of the loop. As a result, it will use whatever value monthCost had in the previous instance of the loop, or it will be zero if this is the first time the loop has executed. So when you say that the program is ignoring your value for kwh, you're right; after you get the value, you don't do anything with it.

Kevin
  • 74,910
  • 12
  • 133
  • 166
-1

There is no cin << kwh; in your code. There's just those cin >> kwh;.

I don't know anything about bloodshed, but Maybe because you define kwh as an integer, and compiler doesn't accept the string input from the command line?

Krzysztof Jabłoński
  • 1,890
  • 1
  • 20
  • 29
arjan
  • 9
  • Learn to write gentle english here. Don't overuse shortcuts, check grammar, format clearly, answer compactly. Don't behave like a kid, even if you are one. It's a priviledge to write here, not a right. Try to earn it. – Krzysztof Jabłoński Sep 20 '12 at 20:02