PDA

View Full Version : Code stops running in Xcode if input stream is long




skatedate22
Jul 9, 2012, 11:48 PM
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?



lee1210
Jul 10, 2012, 12:03 AM
It is completely impossible for us to say. Post the code and sample input that crashes.

-Lee

skatedate22
Jul 10, 2012, 12:25 AM
// 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;

}

----------[COLOR="#808080"]

----------
Input that will close run window after enter is pressed

Please enter the problems per set: 3

Set #1
---------
What is the maximum number for this set? 100

30 + 37 = 67
Correct

59 + 76 = 135
Correct

0 + 14 = 43
Incorrect

Set #2
---------
What is the maximum number for this set? 90

76 - 0 = 76
Correct

73 - 85 = 23
Incorrect

35 - 77 = -42
Correct

Set #3
---------
What is the maximum number for this set? 20

3 * 12 = 36
Correct

1 * 15 = 43
Incorrect

20 * 19 = 56

----------
Working Input

Please enter the problems per set: 2

Set #1
---------
What is the maximum number for this set? 20

12 + 13 = 23
Incorrect

11 + 2 = 13
Correct

Set #2
---------
What is the maximum number for this set? 4

3 - 2 = 1
Correct

1 - 0 = 3
Incorrect

Set #3
---------
What is the maximum number for this set? 10

7 * 5 = 35
Correct

1 * 5 = 3
Incorrect

Set #1: You got 1 correct out of 2 for 50%
Set #2: You got 1 correct out of 2 for 50%
Set #3: You got 1 correct out of 2 for 50%
Overall you got 3 correct out of 6 for 50%

lee1210
Jul 10, 2012, 01:16 AM
I don't get a crash. I'm not running from XCode, I'm just going from terminal, but it works (meaning it doesn't crash, it's not correct) for me.

I'd suggest trying from Terminal. If you don't know how to use Terminal let us know and we can give you some steps.

You need to initialize
probsPerSet
setOneCorrect
setTwoCorrect
setThreeCorrect
to 0. Right now they're set to garbage when the program starts. They might get changed to different garbage, but you'll get wrong results.

No matter what input I used, though, I didn't get a crash. There aren't any smoking guns in the code. Even the divide by 0 possible when there's 0 problems per set doesn't seem to crash. Does the debugger get invoked when the run window closes?

-Lee

skatedate22
Jul 10, 2012, 01:45 AM
First of all thanks for your time I really appreciate it.

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. Also, I observed that when I typed in the inputs very rapidly on the App-store version, the window did not close, but when I typed it in at a regular pace it did.

I would appreciate if you could tell me how to use terminal to run my code for any problems I have in the future and just for general knowledge.

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. When the run window closes, it just goes back to the regular screen where you type your code. Not a debugger as much as I can tell.

gnasher729
Jul 10, 2012, 03:28 AM
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.

1. Assuming that a bug is not your fault but the fault of your tools means you won't be able to find the bug. Here is the most important rule of programming: If your program doesn't work, it is _always_ because _you_ did something wrong. Your task is to find _what_ you did wrong.

2. Uninitialized variables mean that you introduce random behaviour to your program. Random behaviour can lead to code that is _sometimes_ working. It may break with any change that you make, so you are living dangerous.

FYI, where I work all possible warnings are turned on in the compiler (check your build settings) and it is set not to compile if there are any compiler warnings and analyzer warnings. Saves a lot of time.

skatedate22
Jul 10, 2012, 03:44 AM
Thanks for the help but it im confused why my code would run perfectly every time on one version on a compiler, and perfectly except for 1 part of the program in a few situations on another compiler. Xcode says that it built without any problems...I looked through some of the Xcode help sections and it never mentions any situation where the run window would just close. Ive had programs have many different errors, but the window still remains open. Is it wrong to still feel Xcode is the problem?

Also, if you look in the App Store, Apple is always updating and fixing bugs in Xcode and people experience situations like this all the time. In addition, I talked to my brother who is a comp sci expert about the problem and he said although my code is not perfect and may have problems if I do not initialize my variables, what is happening is a problem with Xcode, not my code. Its not a reaction Xcode should have to any error.