I am running a program in xcode and the program works perfectly for smaller numbers and runs all the way through, but just closes the run window at the same place every time for larger numbers. Why is this happening?
// Jesse Strober , 7/8/12 , CIS 15A , Dave Harden
// Assignment 7 Part 1
/**/
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
void getProbsPerSet(int &probsPerSet);
void doOneSet(char operation, int probsPerSet, int &setCorrect);
void printHeader(char operation);
void getMaxNum(int &maxNum);
void doOneProblem(char operation, int maxNum, int &numCorrect);
void generateOperands(int &num1, int &num2, int maxNum);
int calculateCorrectAnswer(int num1, int num2, char operation);
void checkAnswer(int userAnswer, int trueAnswer, int &numCorrect);
void printReport(int setOneCorrect, int setTwoCorrect, int setThreeCorrect, double probsPerSet);
int main()
{
int probsPerSet;
int setOneCorrect;
int setTwoCorrect;
int setThreeCorrect;
srand(time(0));
getProbsPerSet(probsPerSet);
doOneSet('+', probsPerSet, setOneCorrect);
doOneSet('-', probsPerSet, setTwoCorrect);
doOneSet('*', probsPerSet, setThreeCorrect);
printReport(setOneCorrect, setTwoCorrect, setThreeCorrect, probsPerSet);
}
void getProbsPerSet(int &probsPerSet)
{
cout << "Please enter the problems per set: ";
cin >> probsPerSet;
cout << endl;
}
void doOneSet(char operation, int probsPerSet, int &numCorrect)
{
int probCount;
int maxNum;
printHeader(operation);
getMaxNum(maxNum);
for (probCount = 0; probCount < probsPerSet; probCount++) {
doOneProblem(operation, maxNum, numCorrect);
}
}
void getMaxNum(int &maxNum)
{
cout << "What is the maximum number for this set? ";
cin >> maxNum;
cout << endl;
}
void printHeader(char operation)
{
if (operation == '+') {
cout << "Set #1" << endl << "---------";
}else if (operation == '-'){
cout << "Set #2" << endl << "---------";
}else if (operation == '*'){
cout << "Set #3" << endl << "---------";
}
cout << endl;
}
void doOneProblem(char operation, int maxNum, int &numCorrect)
{
int userAnswer;
int trueAnswer;
int num1;
int num2;
generateOperands(num1, num2, maxNum);
cout << num1 << " " << operation << " " << num2 << " = ";
cin >> userAnswer;
trueAnswer = calculateCorrectAnswer(num1, num2, operation);
checkAnswer(userAnswer, trueAnswer, numCorrect);
}
void generateOperands(int &num1, int &num2, int maxNum)
{
num1 = rand() % (maxNum + 1);
num2 = rand() % (maxNum + 1);
}
int calculateCorrectAnswer(int num1, int num2, char operation)
{
if (operation == '+') {
return num1 + num2;
}else if (operation == '-'){
return num1 - num2;
}
return num1 * num2;
}
void checkAnswer(int userAnswer, int trueAnswer, int &numCorrect)
{
if (userAnswer == trueAnswer) {
cout << "Correct" << endl << endl;
numCorrect++;
}else{
cout << "Incorrect" << endl << endl;
}
}
void printReport(int setOneCorrect, int setTwoCorrect, int setThreeCorrect, double probsPerSet)
{
double percentageOne, percentageTwo, percentageThree, overallPercentage;
int totalCorrect;
double totalProbs;
percentageOne = floor(((setOneCorrect / probsPerSet) * 100) + 0.5);
percentageTwo = floor(((setTwoCorrect / probsPerSet) * 100) + 0.5);
percentageThree = floor(((setThreeCorrect / probsPerSet) * 100) + 0.5);
totalCorrect = setOneCorrect + setTwoCorrect + setTwoCorrect;
totalProbs = probsPerSet * 3;
overallPercentage = floor(((totalCorrect / totalProbs) * 100) + 0.5);
cout << "Set #1: You got " << setOneCorrect << " correct out of " << probsPerSet << " for " <<
percentageOne << "%" << endl;
cout << "Set #2: You got " << setTwoCorrect << " correct out of " << probsPerSet << " for " <<
percentageTwo << "%" << endl;
cout << "Set #3: You got " << setThreeCorrect << " correct out of " << probsPerSet << " for " <<
percentageThree << "%" << endl;
cout << "Overall you got " << totalCorrect << " correct out of " << totalProbs << " for " <<
overallPercentage << "%" << endl;
}
I tested my code in the older version of Xcode that is already installed on your computer on my laptop. The code worked perfectly and I got the output I wanted and needed. This leads me to suspect it is a problem with the Xcode application that you install from the app store.
As for the initializing comment, I agree that it is much more proper to initialize the variables and I will, but I do get the correct output anyways.