Question: Why doesn't the following line of code pause my program? cin.ignore (numeric_limits : :max(), ' '); If I don't use this line of code to

Why doesn't the following line of code pause my program?

cin.ignore (numeric_limits : :max(), ' ');

If I don't use this line of code to pause my program I will receive a zero. It has happened before. It hurts.

I just can't seem to get it to work.

This is my program:

/* module9ProgrammingAssignment.cpp : This program dynamically allocates an array large enough to hold a

user-defined number of test scores, sorts them, and displays them with the average score by passing array

to separate functions.*/

//By Eric Mickell

#include "stdafx.h"

#include

#include

#include

using namespace std;

//function prototypes

void getScores(double* score, int size);

void displayScores(double* score, int size, double avg);

void sort(double* score, int size);

double average(double* score, int numScores);

int main()

{

//declaring and initializing variables

double *score = 0, avg = 0;

int size = 0;

//get user-defined number of test sores (size of array)

cout << "How many test grades will you input today? ";

cin >> size;

cout << endl;

score = new double[size]; //create array to hold scores

//call functions

getScores(score, size);

sort(score, size);

avg = average(score, size);

displayScores(score, size, avg);

//*******************************************************************************************************************************************************************************************************

//system("pause"); //I know system("pause") isn't the best way to pause a program, but the below doesn't pause program on my end

cin.ignore(numeric_limits ::max(), ' ');

return 0;

//*******************************************************************************************************************************************************************************************************

}

//defining get grades function

void getScores(double *score, int size)

{

for (int count = 0; count < size; count++)

{

cout << "Score # " << (count + 1) << ": ";

cin >> *(score + count);

while (*(score + count) < 0)

{

cout << "Please enter a positive number! ";

cin >> *(score + count);

}

}

}

//defining sorting function

void sort(double* score, int size)

{

int smallestNum = 0, nxtNum = 0, count = 0;

double temp = 0;

for (int count = 0; count < size - 1; count++) //outer loop

{

int smallestNum = count; //set smallestNum to current index of array

for (int nxtNum = count + 1; nxtNum < size; nxtNum++) //inner loop

{

if (*(score + nxtNum) < *(score + smallestNum))

{

smallestNum = nxtNum;

}

}

if (count != smallestNum) //if smallestNum isn't equal to count (it's now nxtNum) then there is a smaller number that must be swapped

{

temp = *(score + count);

*(score + count) = *(score + smallestNum);

*(score + smallestNum) = temp;

}

}

}

//defining average function

double average(double* score, int size)

{

double avg = 0, total = 0;

for (int count = 0; count < size; count++)

{

total += *(score + count);

}

avg = total / size;

return avg;

}

//defining display scores function

void displayScores(double *score, int size, double avg)

{

cout << " Yay!! Scores are in!";

cout << " ********************" << endl;

cout << " Scores:";

for (int count = 0; count < size; count++)

{

cout << "\t" << fixed << setprecision(2) << *(score + count) << endl;

}

cout << " Average: " << fixed << setprecision(2) << avg << endl;

cout << " ********************" << endl;

}

//end of program

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!