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!

Code works fine for me.... Have you tried compiling other code? Maybe you have a slow computer?

I did get errors from you quotes but that could just be the symbol that the code brackets use for "".

You don't need system("pause"); , just use cin.get(); Take your declaration out of the loop. You don't even need to set it zero if you're just going to have the user give the value afterwards. Can the exit number be a zero too? If so then just have the 'if' statement as '=< 0'

Overall, I'm not seeing anything that would dramatically shootup the processing time. When C++ prompts a user, it generally takes up little(or no) time while it waits. What does TaskManager say while it's running?

Edited 6 Years Ago by MosaicFuneral: n/a

photoyid

Code works fine for me.... Have you tried compiling other code? Maybe you have a slow computer?

I think my computer is not that bad or slow, a dual-core AMD processor and 4g memory. And yes I tried to compile other programs with looping statements especially with this conditional statement if (value < 0) using Borland 5.5 and Dev-C++.

MosaicFuneral

You don't need system("pause"); , just use cin.get();

Take your declaration out of the loop. You don't even need to set it zero if you're just going to have the user give the value afterwards. Can the exit number be a zero too? If so then just have the 'if' statement as '=< 0'

Overall, I'm not seeing anything that would dramatically shootup the processing time. When C++ prompts a user, it generally takes up little(or no) time while it waits. What does TaskManager say while it's running?

I took my declaration out of the looping statement and removed the zero, it looks like this:

int accumulator;
int value;
for(;; )
{

}

...but I got this result

Enter next sequence
Enter next number: 6
Enter next number: 7
Enter next number: -7
The total is 2147348493

Enter next sequence
Enter next number: 56
Enter next number: 6
Enter next number: 7
Enter next number: -8
The total is 2147348562

Enter next sequence
Enter next number: -6
The total is 2147348562

Enter next sequence
Enter next number: -6
The total is 2147348562

Enter next sequence
Enter next number: -4
The total is 2147348562

Enter next sequence
Enter next number: -6
The total is 2147348562

Enter next sequence
Enter next number: -6
The total is 2147348562

Enter next sequence
Enter next number: -6
The total is 2147348562

Enter next sequence
Enter next number:

The program didn't add the sequence of numbers that I entered it shows a large amount of numbers as a total something like this: The total is 2147348562 , and it became like a infinite loop without any way of exit even if I already entered two or even six consecutive negative numbers, the program ignores the conditional statement if (value < 0) and continue to ask the user to enter next number. I also tried to change the conditional statement from if (value < 0) to if (value =< 0) but I got 2 errors and 1 warning

Error E2188 FunctionDemo.cpp 23: Expression syntax in function sumSequence()
Error E2377 FunctionDemo.cpp 30: If statement missing ) in function sumSequence()
Warning W8066 FunctionDemo.cpp 34: Unreachable code in function sumSequence()
*** 2 errors in Compile ***

I dont like using system("PAUSE") and I changed it to cin.get() but I noticed that cin.get() isn't working on some of my programs then I use system("PAUSE") then it worked. But for this program cin.get() is working.
Thanks for your effort.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I dont like using system("PAUSE") and I changed it to cin.get() but I noticed that cin.get() isn't working on some of my programs then I use system("PAUSE") then it worked. But for this program cin.get() is working.
Thanks for your effort.

That's because the other programs probably left the input buffer dirty. Carefully check your code and look for inputs that will leave \n in the buffer -- like reading a single character but not the ENTER that was typed in with the character.

I took my declaration out of the looping statement and removed the zero, it looks like this:

int accumulator;
int value;
for(;; )
{

}

...but I got this result

accumulator needs to be set to 0 each time, as you are adding to whatever its current value is, which is essentially nothing.

Change that to:

int accumulator = 0;
int value;
for( ;; )
{
//...
}

I also tried to change the conditional statement from if (value < 0) to if (value =< 0) but I got 2 errors and 1 warning

Error E2188 FunctionDemo.cpp 23: Expression syntax in function sumSequence()
Error E2377 FunctionDemo.cpp 30: If statement missing ) in function sumSequence()
Warning W8066 FunctionDemo.cpp 34: Unreachable code in function sumSequence()
*** 2 errors in Compile ***

That line needs to be:

if(value <= 0) //If value is less than or equal to zero, then...
{
//...
}

I hope this helps.

Edited 6 Years Ago by dwdude: n/a

Thanks for your help guys.

Just want to ask, if I use the conditional statement if (value < 0) after compiling I run the program it takes 13 seconds or more before the program appears on the screen and ask the user to enter next number, is it really normal for it to take 13 seconds if I use this kind of conditional statement if (value < 0), it only happens everytime I run the program right after compiling. And if I use if (value < = 0) the program only takes 2 seconds to display the output if I run the program right after compiling, but If(value < = 0) will make the program exit the sequence if the user enters a 0 and thats not what I want, I want the program to exit the sequence if the user enters a negative number thats why I use if (value < 0) which I think is the reason why it takes 13 seconds to display if I run the program right after compiling.

And have you experience using cin.get() and after running the program, when you like to end the program it immediately close the program instead of moving the cursor to the next line and wait for the user to press enter before terminating or closing the program?
I recently found out that cin.ignore() is one solution for that problem although I know that there are many other solutions for that kind of problem but cin.ignore() is working for me.
It should look like this:

cin.ignore();
cin.get();
return 0;

Thanks!

Edited 6 Years Ago by Sorb3t3ro: n/a

Thanks for your help guys.

Just want to ask, if I use the conditional statement if (value < 0) after compiling I run the program it takes 13 seconds or more before the program appears on the screen and ask the user to enter next number, is it really normal for it to take 13 seconds if I use this kind of conditional statement if (value < 0)

To answer your question, it really isn't normal that it takes so long to display. Upon compiling and running your program, I don't have that problem at all and I see no reason as to why you would be having it, either. Rather strange.

Sorry I couldn't be of more help.

I think Im the only one having this kind of problem, really strange for me because why you guys dont have the same problem, and it only happens when I use this if (value < 0) I dont have this problem with my other programs.

Well thanks dwdude, I hope I can figure this out soon.

And if I use if (value < = 0) the program only takes 2 seconds to display the output if I run the program right after compiling, but If(value < = 0) will make the program exit the sequence if the user enters a 0 and thats not what I want, I want the program to exit the sequence if the user enters a negative number thats why I use if (value < 0) which I think is the reason why it takes 13 seconds to display if I run the program right after compiling.

The red makes no sense. Compiling and executing have nothing to do with each other. 13 seconds from WHEN to WHEN? You can't count the compile time. If you are counting 13 seconds from when you click "Build and Run" or whatever to when the first text shows up, then you're comparing oranges and apples. Every time you change a program, then "Build and Run" from Dev C++, it's going to take longer than when you DIDN'T change it because there's nothing to recompile when you don't change it.

Compare oranges to oranges. The clock starts ticking when the program starts executing. The time it takes for an IDE to compile, create a window, load the program into memory, load any debugging, etc., shouldn't count as a 13 second pause. If there's a 13 second pause BETWEEN the display of two cout statements, then THAT doesn't make any sense.

Edited 6 Years Ago by VernonDozier: n/a

This article has been dead for over six months. Start a new discussion instead.