我该如何调试这个程序? [英] How do I debug this program?

查看:80
本文介绍了我该如何调试这个程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在程序中清除数据时遇到问题。我真的需要帮助解决它。在重新提起用户被问到是否想要输入另一个学生的姓名并计算成绩后,程序会冻结



我尝试过的方法:



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:

/ 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;
}

推荐答案

我在你的代码中看到了一些问题,但它们不是你正在寻找的问题。 />
results()以调用 mainEvent()结束,这是调用它的函数,一般来说,这是一个坏主意,因为它看起来像是一次意外的递归。

每个函数都应以返回调用者结束。

为学生输入数据,代码逻辑应如下所示:

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:

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



您的代码行为不符合您的预期,而您不会理解为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你应该做什么,它不要找不到错误,只是通过告诉你发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


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.


这篇关于我该如何调试这个程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆