I just want to share and ask about what I experienced, Im just a starter in C++ and I noticed everytime I make a program which has a looping statement, after compiling the program and try to run or execute the program(it happens only after compiling) it takes 13 to 17 seconds to display the output. Im using Borland C++ 5.5 I also try to create the same programs with looping statements in Dev-C++ but still the same(still lagging). Im sure that the programs are error free because it came from the book that I am using.
Here's a sample of the programs and this one lags in Borland C++ 5.5 even in Dev-C++:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
// function
int sumSequence(void)
{
// loop forever
int accumulator = 0;
for(;;)
{
// fetch another number
int value = 0;
cout << “Enter next number: “;
cin >> value;
// if it’s negative...
if (value < 0)
{
// ...then exit from the loop
break;
}
// ...otherwise add the number to the
// accumulator
accumulator= accumulator + value;
}
// return the accumulated value
return accumulator;
}
int main()
{
cout << “This program sums multiple series\n”
<< “of numbers. Terminate each sequence\n”
<< “by entering a negative number.\n”
<< “Terminate the series by entering two\n”
<< “negative numbers in a row\n”
<< endl;
// accumulate sequences of numbers...
int accumulatedValue;
for(;;)
{
cout << “Enter next sequence” << endl;
accumulatedValue = sumSequence();
// terminate the loop if sumSequence() returns
// a zero
if (accumulatedValue == 0)
{
break;
}
// now output the accumulated result
cout << “The total is “
<< accumulatedValue
<< “\n”
<< endl;
}
cout << “Thank you” << endl;
system(“PAUSE”);
return 0;
}
But I noticed something in the conditional statement of the function sumSequence():
if (value < 0)
...I changed it to :
if (value <= 0)
I compile and execute or run the program and its faster now both in Borland C++ 5.5 and Dev-C++, but that's not what I want to I want the program to exit the sequence if the user enters a negative number(not 0) and display the total of that sequence, and exit the program if the user enters two consecutive negative numbers.
As a starter its really confusing for me, can you explain why this happen? is there any problem with my compilers(both Borland and Dev-C++) or with my code?
Any suggestions, help and explanations that might answer my questions and make things clearer for me will be appreciated.
Thanks!