Hi
I am very new to C++ programming. When using one of the tutorial programs from a book I am learning from I have encountered a bug in the program. It has got me curious to the solution. The program requires a negative number to be entered to end a loop that ask the user to enter numbers. If I enter just the negative sign (-) it puts the program into a continuous loop.
What function can I add to filter for this situation and break the loop?
portion of the code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <chrono> //added for adding a time delay for debugging
#include <thread> //added for adding a time delay for debugging
using namespace std;
using namespace std::this_thread; //adds functions sleep_for and sleep_until
using namespace std::chrono_literals; // ns, us, ms, s, h, etc.
using std::chrono::system_clock; //not sure what this does...!!!
/* square - returns the square of its arguments
doubleVar - the value to be squared
returns - square of doubleVar
*/
/* displayExplanation - promt the user as to the rules
of the game
*/
void displayExplanation (void)
{
cout << "This program sums the squares of multiple\n"
<< "series of numbers. Terminate each sequence\n"
<< "by entering a negative number.\n"
<< "Terminate the series by entering an\n"
<< "empty sequence.\n"
<< endl;
}
/* sumSquareSequence - accumulate the square of the number
entered at the keyboard into the sequence
until the user enters a negative num
*/
double sumSquareSequence (void)
{
//loop foreever
double accumulator = 0.0;
for (;;)
{
//fetch another number
double dValue = 0;
cout << "Enter next number: ";
cin >> dValue;
sleep_for(1s); // I added for debuging continuous loop if "-" is entered as next number.
//if its negative...
if (dValue < 0)
{
// ... then exit from the loop
break;
}
//...otherwise calculate the square
double value = square (dValue);
//now add the square to the accumulator
accumulator += value;
}
//return the accumulated value
return accumulator;
}
Output when the code functions norma, I entered 10 three times then -1, and -1 again. The second example of the code out put is if I just enter the negative sign with out a number following.:
This program sums the squares of multiple
series of numbers. Terminate each sequence
by entering a negative number.
Terminate the series by entering an
empty sequence.
Enter next sequence
Enter next number: 10
Enter next number: 10
Enter next number: 10
Enter next number: -1
The total of the values squared is 300
Enter next sequence
Enter next number: -1
Press enter to continue..
Program ended with exit code: 0
-------------------------------------------
Output with negative sign, I terminated the program that's what returned the error code;
This program sums the squares of multiple
series of numbers. Terminate each sequence
by entering a negative number.
Terminate the series by entering an
empty sequence.
Enter next sequence
Enter next number: 10
Enter next number: 10
Enter next number: 10
Enter next number: -1
The total of the values squared is 300
Enter next sequence
Enter next number: -
Enter next number: Enter next number: Enter next number: Enter next number: Enter next number: Program ended with exit code: 9