Sunday, August 2, 2009

Why this program loop infinitely? [C++]?

#include %26lt;iostream%26gt;


using namespace std;





int main()


{


int accNum, begin , charges, credits ,


limit;








cout%26lt;%26lt; "Enter account number: ";


cin%26gt;%26gt;accNum;


while (accNum !=-1)


{





cout%26lt;%26lt;"Beginning balance: ";


cin%26gt;%26gt;begin;


cout%26lt;%26lt;endl%26lt;%26lt;"Total charges: ";


cin%26gt;%26gt;charges;


cout%26lt;%26lt;endl%26lt;%26lt;"Total credits: ";


cin%26gt;%26gt;credits;


cout%26lt;%26lt;endl%26lt;%26lt;"Credit limit: ";


cin%26gt;%26gt;limit;


if (begin+charges-credits %26gt;limit){


cout%26lt;%26lt;"\t Calculating.."%26lt;%26lt;endl


%26lt;%26lt;"Account: "%26lt;%26lt;accNum;


cout%26lt;%26lt;"Credit limit: "%26lt;%26lt;limit


%26lt;%26lt;endl;


cout%26lt;%26lt;"Balace: "%26lt;%26lt;begin+charges-credits


%26lt;%26lt;endl;


cout%26lt;%26lt;"Exceeded !"%26lt;%26lt;endl;


}


cout%26lt;%26lt; "Enter account number: ";


cin%26gt;%26gt;accNum;


}


}

Why this program loop infinitely? [C++]?
There is nothing wrong with this program.


It only 'loops endlessly' when you input a non-number value.
Reply:It will loop forever as long as your account number is not -1
Reply:Basically, what is happening here is this loop will break when the account number -1 is entered into the program. For the section "Enter Account Number:" .. cin %26gt;%26gt; accNum.





The moment that happens, the loop will break since the condition will be met.





Also, as a practice, if you have an int main() you should return a value like a standard function... otherwise, make it a void main().





*Edit* You should change the values for credit limit etc. to floats or doubles since the user would input those as such [rounding is pretty stupid in an economical program]. Changing the account number to a double won't solve anything and is wasting space.
Reply:This is not an infinite loop. It ends once the loop control condition is met: when the input value equals -1. Therefore, this is a finite loop.





Perhaps you misunderstand what an infinite loop is. Just because you do not know when the user will input a -1, this does not mean the loop is infinite. It only means that the number of times the body of the loop will be processed is unknown. Many loops are like this; you don't know in advance how many times they will execute. That is a major benefit of a computer program. It can perform tedious, monotonous, and repetitious tasks.





______________________________________





Below are some definitions of an infinite loop.





An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition or having one that can never be met.





An infinite loop (sometimes called an endless loop) is a piece of coding that lacks a functional exit so that it repeats indefinitely.





Usually, an infinite loop results from a programming error - for example, where the conditions for exit are incorrectly written. Intentional uses for infinite loops include programs that are supposed to run continuously, such as product demos or in programming for embedded systems.





A pseudo-infinite loop is one that looks as if it will be infinite, but that will actually stop at some point.





_________________

snow of june

No comments:

Post a Comment