My console app has stopped working and windows has to shut it down?

Why is windows shutting down my console app?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2432 Views - Last Post: 22 October 2009 - 05:32 AM Rate Topic: -----

#1 schlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 19-October 09

My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 02:28 PM

Ok, I don't have a better way to expalain this except by showing you my code, and you telling me whats wrong with it. Everything worked up until I changed some double data type stuff to int. But it should cause this......

In my header file, I have this.....

/* be sure to....
#include "tylersfunctions.h"
in the program*/

#ifndef TYLERSFUNCTIONS_H_INCLUDE
#define TYLERSFUNCTIONS_H_INCLUDE
using namespace std;

int Atk = 10;
int random(int n);
int fact(int n);
int nCr(int n, int r);
int CalcDamage(int Atk, int randnump);

/*returns a random number that is between 1 and the number that you pass to it
it's form is...
random(#);
where # is the number/variable you want to pass to it
also include these
#include <math.h>
#include <stdlib.h>
#include <time.h>*/
int random(int n)
{
	return rand() % n+1;
}

int nCr(int n, int r)
{
	int sum = 0;
	sum = (fact(n))/(fact(n-r) * fact(r));
	return sum;
}

int fact(int n)
{
	int sum = 1, i = 1;
	while (i <= n)
	{
		sum = sum * i;
		i++;
	}
	return sum;
}

int CalcDamage(int Atk, int randnump)
{
	int damage;
	damage = Atk + Atk % nCr(random(randnump), random(randnump-1));
	return damage;
}

#endif //TYLERSFUNCTIONS_H_INCLUDE


And in the main function I have this.....

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <strsafe.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <string>
#include "tylersfucntions.h"
using namespace std;

int main()
{
	int playerrand;
	while (1)
	{
	cout << "Random number for player: ";
	cin >> playerrand;
	cout << endl << CalcDamage(Atk, playerrand) << endl;
	}
	return 0;
}


Yes, there are some what appear to be extra includes that I don't need. But I chopped out a few of the extra functions that I know are not causing the problem from my header file. Any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: My console app has stopped working and windows has to shut it down?

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 04:16 PM

Are you inputing a value more than 15? int can't hold the factorial of a number larger than that without an overflow.
Was This Post Helpful? 0
  • +
  • -

#3 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 04:46 PM

why do you do your work in a header file?
why not link the header to another source file and do the work in
there?

concerning your code i believe Momerath is right although i'm not sure(i never like to use factorials)

if thats not the problem it would be alot easier if you posted the error messages..
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 04:54 PM

Quote

why do you do your work in a header file?
why not link the header to another source file and do the work in
there?

why does he have to work in a source file?
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 05:27 PM

Do work in a header file? This isn't about a workplace location...

Only one definition across all translation units that are linked together. If you have definitions in a header file, you can only include them in one source file, right? Because if you include into multiple source files, you'll have multiple TUs with multiple definitions of the same function. And then you can't link them together.

So defining in a header file prevents you from using a header file as you would use a header file...remind me again why you can have definitions in a header file?
Was This Post Helpful? 0
  • +
  • -

#6 schlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 19-October 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 05:38 PM

I have tested and int can do factorials greater than 100 with ease.

But here is what I get, in case this helps, because 1 gives the error still.

Posted Image
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 05:47 PM

Quote

I have tested and int can do factorials greater than 100 with ease.
Ummm...what???! Let's take a look at the implication. We need hardware such that an integer can hold a number of magnitude 100!. That means We need 2^N-1 >= 100!

Thus, N >= log(100!+1)/log(2), where N is integral and > 0. Google solves this to tell us you would need N >= 525.

Are you telling me you have computer hardware that can have >= 525 bit datatypes?
Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 06:05 PM

View Postschlynn, on 21 Oct, 2009 - 04:38 PM, said:

I have tested and int can do factorials greater than 100 with ease.

100! = 93,326,215,443,944,152,681,699,238,856,266,700,490,715,968,264,381,621,468,592,963,895,217,599,993,229,915,608,941,463,976,156,518,286,253,697,920,827,223,758,251,185,210,916,864,000,000,000,000,000,000,000,000

And you're saying your int can hold that?
Was This Post Helpful? 0
  • +
  • -

#9 schlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 19-October 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 06:20 PM

My bad guys. I have the data type as double back then. Now you can do up to 170!. This is 7.25742e306. But now that we have that cleared up. Can we get back to my problem?
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 06:24 PM

So now you're telling us that your computer has hardware that supports 1020 bits in a datatype.

Or wait, you don't care about the precision except for the most significant digits?

This post has been edited by Oler1s: 21 October 2009 - 06:28 PM

Was This Post Helpful? 0
  • +
  • -

#11 schlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 19-October 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 06:27 PM

View PostOler1s, on 21 Oct, 2009 - 05:24 PM, said:

So now you're telling us that your computer has hardware that supports 1020 bits in a datatype.



#include <iostream>
using namespace std;

double fact(double n)
{
	double sum = 1, i = 1;
	while (i <= n)
	{
		sum = sum * i;
		i++;
	}
	return sum;
}

int main()
{
   int n;
   cout << "Number: ";
   cin >> n;
   cout << fact(n) << endl;
   return 0;
}


Try it if you don't believe me.
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 06:31 PM

You do understand the limited precision you have, right?

And that your success so far depends on the implementation following IEEE 754, I should add, which is not guaranteed.

This post has been edited by Oler1s: 21 October 2009 - 06:34 PM

Was This Post Helpful? 0
  • +
  • -

#13 schlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 19-October 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 06:34 PM

View PostOler1s, on 21 Oct, 2009 - 05:31 PM, said:

You do understand the limited precision you have, right?


Cool story bro, you hijacked my thread, and now you just ranting. The fact is that it can be done. And my question is still not answered.
Was This Post Helpful? 0
  • +
  • -

#14 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: My console app has stopped working and windows has to shut it down?

Posted 21 October 2009 - 07:42 PM

Your problem is zero. What this when you pass a zero (which you do):

return rand() % n+1;
Was This Post Helpful? 0
  • +
  • -

#15 schlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 19-October 09

Re: My console app has stopped working and windows has to shut it down?

Posted 22 October 2009 - 04:43 AM

View PostMomerath, on 21 Oct, 2009 - 06:42 PM, said:

Your problem is zero. What this when you pass a zero (which you do):

return rand() % n+1;


When am I passing zero to the function?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2