Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need this C++ program to quit when -99 is entered. Everything is working except when you enter -99 on the first run it does

I need this C++ program to quit when -99 is entered. Everything is working except when you enter -99 on the first run it does not quit

example output :Please enter desired length of sequence. -1 Please enter a number greater than or equal to 01 0 1 The sum of these integers is 1 Enter another number or -99 to quit2 0 1 2 The sum of these integers is 3 Enter another number or -99 to quit-99

Goodbye

-99 entered on first input :Please enter desired length of sequence. -99 The sum of these integers is 0 Enter another number or -99 to quit-99 Goodbye Process finished with exit code 0

#include  using namespace std; int main() // Print welcome message and prompt user for desired number of sequence { cout<<"Welcome to the Pell Numbers program"<> seqnum; //check for valid input while (seqnum < 0) { //end program if user enters in -99 if (seqnum ==-99) break ; cout << "Please enter a number greater than or equal to 0"; cin >> seqnum; } //run program until user enters -99 while (seqnum !=99) { //validate that input on second and subsequent rounds is valid if (seqnum < 0){ while (seqnum < 0){ //quit if user enters -99 if (seqnum ==-99) break ; cout << "Please enter a number greater than or equal to 0"; cin >> seqnum; } } // make two variables equal to the first two numbers of the Pell Number Sequence int p1 = 0, p2 = 1; //if user enters 0, print 0 if (seqnum == 0) { cout << p1 << endl; //if user enters 1 print 0, and 1 } else if (seqnum == 1) { cout << p1 << endl; cout << p2 << endl; sum1 = 1; // For sequences number bigger than 1 this will print the first two of the sequence } else if (seqnum != -99) { cout << p1 << endl; cout << p2 << endl; //update sum 1 to include p2 sum1 = 1; // display the pell numbers for the desired sequence length for (int curSeq = 2; curSeq <= seqnum; curSeq++) { //calculate each pell number using the pell number formula. int nextnum = ((p2 * 2) + p1); //add pell number to the accumulator sum1 += nextnum; //display number cout << nextnum << endl; //update p1 and p2 to the next numbers in the sequence p1 = p2; p2 = nextnum; } } //print the sum of the integers cout << "The sum of these integers is "<< sum1 << endl; //prompt user to continue or quit the program cout << "Enter another number or -99 to quit"; cin >>seqnum; if (seqnum == -99) break; } cout << "Goodbye"; return 0; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions