Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

my assingment is to make loops in my program to spit out the number of sevens in

ID: 3600266 • Letter: M

Question

my assingment is to make loops in my program to spit out the number of sevens in my range... this template is wrong and needs to be fix.

// Gerson Pliego
// 10/22/2017
//

// Loop checklist
// 0. initialization (before loop - often accumulators)
// 1. "end" test (in C++, it's a "not end" or "continue" test)
// 2. "step" - something that leads to end test
// 3. loop "body" - whatever we want to repeat

#include
using namespace std;

int main(void)
{
// structure:
// while ( condition )
//  statement;
// do
//  statement;
// while ( condidion );

// 1 counted loop
// 1.1 input count
int lowRange;
int highRange;
while (true)
{
  cout << "Enter a Numerical Range with a low and high bound: ";
  cin >> lowRange>> highRange;
  cin.ignore(999, ' ');
  if (cin.fail())
  {
   cin.clear();
   cin.ignore(999, ' ');
   cout << "data format error, try again" << endl;
   continue;
  }
  if ( lowRange >= 0)
   break; // end test
  cout << "no negative numbers, try again" << endl;
}

// 1.2
int howManyTimesAccomplished = 0;
while (howManyTimesAccomplished < lowRange)
{
  // 1.2.1 output greeting
  cout << "Hello, World!" << endl;
  // howManyTimesAccomplished = howManyTimesAccomplished + 1;
  // howManyTimesAccomplished += 1;
  ++howManyTimesAccomplished; // howManyTimesAccomplished++
}

// 1.3 output greetings
for (int sumRange = 0
  ; int sumTwoRange = 1
  ; sumTwoRange <= highRange
  ;)
  cout << "Hello, World!" << endl;

cout << endl;

// 2 more greetings (not counted):
cout << "more greetings" << endl;
while (true)
{
  // 2.1 output greeting
  cout << "Hello World" << endl;

  // 2.2 ask if user wants to do again
  bool doAgain;
  while (true)
  {
   char userResponse;
   cout << "Do you want to do it again? ";
   cin >> userResponse;
   cin.ignore(999, ' ');
   if (cin.fail())
   {
    cin.clear();
    cin.ignore(999, ' ');
    cout << "Please enter y or n." << endl;
    continue;
   }
   if (userResponse == 'y' || userResponse == 'Y')
   {
    doAgain = true;
    break;
   }
   if (userResponse == 'n' || userResponse == 'N')
   {
    doAgain = false;
    break;
   }
   cout << "Please enter y or n." << endl;
  }
  if (!doAgain) break;
}

}

Explanation / Answer

#include
using namespace std;

int main(void)
{
// structure:
// while ( condition )
//  statement;
// do
//  statement;
// while ( condidion );

// 1 counted loop
// 1.1 input count
int lowRange;
int highRange;
while (true)
{
  cout << "Enter a Numerical Range with a low and high bound: ";
  cin >> lowRange>> highRange;
  cin.ignore(999, ' ');
  if (cin.fail())
  {
   cin.clear();
   cin.ignore(999, ' ');
   cout << "data format error, try again" << endl;
   continue;
  }
  if ( lowRange >= 0)
   break; // end test
  cout << "no negative numbers, try again" << endl;
}

// 1.2
int howManyTimesAccomplished = 0;
while (howManyTimesAccomplished < lowRange)
{
  // 1.2.1 output greeting
  cout << "Hello, World!" << endl;
  // howManyTimesAccomplished = howManyTimesAccomplished + 1;
  // howManyTimesAccomplished += 1;
  ++howManyTimesAccomplished; // howManyTimesAccomplished++
}

// 1.3 output greetings
for (int sumRange = 0
  ; int sumTwoRange = 1
  ; sumTwoRange <= highRange
  ;)
  cout << "Hello, World!" << endl;

cout << endl;

// 2 more greetings (not counted):
cout << "more greetings" << endl;
while (true)
{
  // 2.1 output greeting
  cout << "Hello World" << endl;

  // 2.2 ask if user wants to do again
  bool doAgain;
  while (true)
  {
   char userResponse;
   cout << "Do you want to do it again? ";
   cin >> userResponse;
   cin.ignore(999, ' ');
   if (cin.fail())
   {
    cin.clear();
    cin.ignore(999, ' ');
    cout << "Please enter y or n." << endl;
    continue;
   }
   if (userResponse == 'y' || userResponse == 'Y')
   {
    doAgain = true;
    break;
   }
   if (userResponse == 'n' || userResponse == 'N')
   {
    doAgain = false;
    break;
   }
   cout << "Please enter y or n." << endl;
  }
  if (!doAgain) break;
}

}