Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem getting data cleared in the program. I really need help fixing it. The program freezes after the reprompt where the user is asked if they would like to enter another student's name and calculate the grades

What I have tried:

C++
/ ZakAttack.cpp--A program for calculating student grades and
// displaying the overall GPA
// Shaheen Fathima Abdul Jamal Nazar, CISP 360
// Instructor: Professor Fowler
// Date: 04/27/2018

#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
using namespace std;

// Variable Declarations
string stName;
int scores[50];
int *ptr = scores;
int count = 0;

// Function Prototypes
void results();
void gradeAdd();
void mainMenu();
int getUserChoice();
void mainEvent();

int main() 
{
	// Program Greeting
	cout << " Hi! Welcome to the Zak Attack Program!\n" << endl;
	cout << " This program calculates the grades for a\n" << endl;
	cout << " specific number of quizzes taken by a student.\n" << endl;
	cout << " It also displays the overall grades along with\n" << endl;
	cout << " the letters (A-F) based on the scores attained by the student! "
		 << endl;
	cout << endl;
  //Specification 2- Prompt for user to enter both first and last name 
	cout << " Enter the name of the student (First and Last): ";
	getline(cin, stName);
	cout << endl;

	mainEvent();

	return 0;
}

void mainEvent() 
{
	int userInput;
	bool task = true;

	do {
		mainMenu();
		cout << "\nEnter your choice: ";
		userInput = getUserChoice();

		if (userInput == 1) {
			gradeAdd();
		} else if (userInput == 2) {
			results();
			break;
		} else if (userInput == 3) {
			task = false;
		}
	} while (task == true);
}

void results() 
{
	// variables to be used
	int tem, sNum, rNum, pNum;
	bool swapNum;
	int scCopy[50];
	int *ptrCopy = scCopy;
	int lowScore[15];
	int *ptrLow = lowScore;
	double gpAvg = 0;
	char rChoice;

	cout << " Name of Student: " << stName << endl;
	// Copying scores[] to scCopy[] array
	for (int i = 0; i < count; i++) {
		*(ptrCopy + i) = *(ptr + i);
	}
	//Feature 6- Bubble Sort Array Copy
	do {
		swapNum = false;
		for (int j = 0; j < count; j++) {
			if (*(ptrCopy + j) > *(ptrCopy + (j + 1))) {
				tem = *(ptrCopy + j);
				*(ptrCopy + j) = *(ptrCopy + (j + 1));
				*(ptrCopy + (j + 1)) = tem;
				swapNum = true;
			}
		}
	} while (swapNum);
	sNum = (count * 0.3);
	rNum = count;
	pNum = sNum;

  //Feature 7- Copying the lowest 30% to low[] and 
  //Convert any copied out grades to 0 in scCopy[]
	for (int i = 0; i < sNum; i++) {
		*(ptrLow + i) = *(ptrCopy + i);
		*(ptrCopy + i) = 0;
		pNum--;
	}
   //Specification 3- Range test the input data for the grades
	//Display the grades as letters and overall GPA
	for (int i = 0; i < count; i++) {
		if (*(ptrCopy + i) >= 94) {
			cout << *(ptrCopy + i) << ": A " << endl;
			gpAvg += 4;
		} else if (*(ptrCopy + i) >= 90 && *(ptrCopy + i) < 94) {
			cout << *(ptrCopy + i) << ": A- " << endl;
			gpAvg += 3.7;
		} else if (*(ptrCopy + i) >= 87 && *(ptrCopy + i) < 90) {
			cout << *(ptrCopy + i) << ": B+ " << endl;
			gpAvg += 3.3;
		} else if (*(ptrCopy + i) >= 83 && *(ptrCopy + i) < 87) {
			cout << *(ptrCopy + i) << ": B " << endl;
			gpAvg += 3;
		} else if (*(ptrCopy + i) >= 80 && *(ptrCopy + i) < 83) {
			cout << *(ptrCopy + i) << ": B- " << endl;
			gpAvg += 2.7;
		} else if (*(ptrCopy + i) >= 77 && *(ptrCopy + i) < 80) {
			cout << *(ptrCopy + i) << ": C+ " << endl;
			gpAvg += 2.3;
		} else if (*(ptrCopy + i) >= 73 && *(ptrCopy + i) < 77) {
			cout << *(ptrCopy + i) << ": C " << endl;
			gpAvg += 2;
		} else if (*(ptrCopy + i) >= 70 && *(ptrCopy + i) < 73) {
			cout << *(ptrCopy + i) << ": C- " << endl;
			gpAvg += 1.7;
		} else if (*(ptrCopy + i) >= 67 && *(ptrCopy + i) < 70) {
			cout << *(ptrCopy + i) << ": D+ " << endl;
			gpAvg += 1.3;
		} else if (*(ptrCopy + i) >= 60 && *(ptrCopy + i) < 67) {
			cout << *(ptrCopy + i) << ": D " << endl;
			gpAvg += 1;
		} else if (*(ptrCopy + i) > 1 && *(ptrCopy + i) < 60) {
			cout << *(ptrCopy + i) << ": F " << endl;
		}
	}
	cout << "*******************" << endl;

	//Feature 1- Dropped scores
	for (int i = 0; i < sNum; i++)
		cout << *(ptrLow + i) << " [Dropped Score] " << endl;

	// Calculation of GPA and displaying results
	rNum -= sNum;
	cout << fixed << setprecision(2) << endl;
	gpAvg = (gpAvg / rNum);
	cout << " Grade Point Average (GPA): " << gpAvg << endl;
	cout << endl;
	
  //Feature 3- Overall grades, GPA and Comments for the grades
	if (gpAvg == 4) {
		cout << " Grade: A" << endl << endl;
		cout << " Excellent Job! " << endl;
		cout << " Keep up the good progress! " << endl;
	} else if (gpAvg < 4 && gpAvg > 3.67) {
		cout << " Grade: A-" << endl << endl;
		cout << " Good Score! " << endl;
		cout << " Keep Going! " << endl;
	} else if (gpAvg < 3.67 && gpAvg > 3.33) {
		cout << " Grade: B+" << endl << endl;
		cout << " Good Work! " << endl;
		cout << " Study a little more. " << endl;
	} else if (gpAvg < 3.33 && gpAvg > 3) {
		cout << " Grade: B" << endl << endl;
		cout << " Good " << endl;
		cout << " Need to study even more! " << endl;
	} else if (gpAvg < 3 && gpAvg > 2.67) {
		cout << " Grade: B- " << endl << endl;
		cout << " Well done " << endl;
		cout << " but need to work hard, " << endl;
		cout << " since you are close to a C! " << endl;
	} else if (gpAvg < 2.67 && gpAvg > 2.33) {
		cout << " Grade: C+ " << endl << endl;
		cout << " Looks like the grades are slipping " << endl;
		cout << " Need to spend more time studying! " << endl;
	} else if (gpAvg < 2.33 && gpAvg > 2) {
		cout << " Grade: C " << endl << endl;
		cout << " Getting low! " << endl;
		cout << " Need to work even harder! " << endl;
	} else if (gpAvg < 2 && gpAvg > 1.67) {
		cout << " Grade: C- " << endl << endl;
		cout << " Risky! Gotta study even more! " << endl;
	} else if (gpAvg < 1.67 && gpAvg > 1.33) {
		cout << " Grade: D+ " << endl << endl;
		cout << " Going low on your " << endl;
		cout << " chances of passing! " << endl;
		cout << " Work even more harder! " << endl;
	} else if (gpAvg < 1.33 && gpAvg > 1) {
		cout << " Grade: D " << endl;
		cout << " Chances of passing the class " << endl;
		cout << " are getting even lower! " << endl;
		cout << " Concentrate and study or seek help " << endl;
		cout << " from a tutor! " << endl;
	} else if (gpAvg < 1 && gpAvg > 0.67) {
		cout << " Grade: D- " << endl;
		cout << " Nearly no chances of passing! " << endl;
	} else if (gpAvg < 0.67) {
		cout << " Grade: F " << endl;
		cout << " Disappointing and dejecting! " << endl;
		cout << " Try Again or Opt for something else " << endl;
	}
	//Specification 1- Allows the user to calcualte the grades for as many students as they want
	cout << " Would you like to enter the grades for another student?";
	cin >> rChoice;
	rChoice = toupper(rChoice);
	
	if (rChoice == 'Y') 
	{
	  cin.clear();
	  cin.ignore(numeric_limits<streamsize>::max(), '\n');
	  cout << " Enter name of the student (First and Last): ";
	  getline(cin, stName);
	  
	  cin.clear();
	  cin.ignore();
	  mainEvent();
	}
	//Specification 5- dumps data before program ends
	else if (rChoice == 'N') 
	{
	  cout << " Good Bye! " << endl;
	}
}

void gradeAdd() 
{
	cout << "\nEnter quiz # " << (count) << " score: ";
	cin >> *(ptr + count);
	
	//Feature 4- Input Validation to make sure that the test scores are neither negative nor more than 100 
	while (*(ptr + count) > 100 || *(ptr + count) < 0) {
		cout << " Sorry! Invalid Input! Please enter a value from 0-100: "
			 << endl;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cin >> *(ptr + count);
	}
	count++;
}

//Specification 4- Main Menu 
void mainMenu() {
	cout << "1. Add a grade" << endl;
	cout << "2. Display Results" << endl;
	cout << "3. Quit" << endl;
}

int getUserChoice() 
{
	int userInput;

	cin >> userInput;

 //Feature 5- Input validation to make sure user enters only options 1- 3 from the menu 
	while (userInput != 1 && userInput != 2 && userInput != 3) {
		cout << " Invalid Choice! Please enter a choice from 1 to 3: " << endl;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cin >> userInput;
	}
	return userInput;
}
Posted
Updated 27-Apr-18 15:27pm

1 solution

I see a few problems in your code, but they are not the problem you are hunting.
results() is ending by calling mainEvent() which is the function that called it, generally speaking, it is a bad idea because it look like an unintended recursion.
Each function should end by returning to caller.
To input data for students, your code logic should look like:
begin of loop
  prompt for student name
  if end of input
    exit of loop
  end of if
  call a function do handle GPA or anything you need (the function must end by returning here)
end of loop


This code is other complicated for nothing:
C++
if (*(ptrCopy + i) >= 94) {
    cout << *(ptrCopy + i) << ": A " << endl;
    gpAvg += 4;
} else if (*(ptrCopy + i) >= 90 && *(ptrCopy + i) < 94) { // because when you reach this point, you already know that the grade is not >=94 from previous test
    cout << *(ptrCopy + i) << ": A- " << endl;
    gpAvg += 3.7;
} else if (*(ptrCopy + i) >= 87 && *(ptrCopy + i) < 90) { // because when you reach this point, you already know that the grade is not >=90 from previous tests
    cout << *(ptrCopy + i) << ": B+ " << endl;
    gpAvg += 3.3;
} else if (*(ptrCopy + i) >= 83 && *(ptrCopy + i) < 87) {
    cout << *(ptrCopy + i) << ": B " << endl;
    gpAvg += 3;


Quote:
The program freezes after the reprompt where the user is asked if they would like to enter another student's name and calculate the grades

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900