Console crashes after compiling fine

Hi. I'm trying to create a program to test my abilities (and it seems liek I need more practice :p) on what I've learned so far. I'm trying to calculate the circumference of a circle, with the user inputting their own diameter/radius. It compiles fine in my IDE with no errors, but when I select run, it crashes after I put my first thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "stdafx.h"
#include <iostream>

#define PI 3.14159

using namespace std;

void select()
{
	cout << "Select r for radius or d for diameter: ";
	char choice;
	cin >> choice;
}

void calculation(void select(), char choice)
{
	if (choice == 'r')
		cout << "Select a value for your radius: " << endl;
	double radius;
	cin >> radius;

	double circRadius;
	circRadius = 2 * PI * radius;

	if (choice == 'd')
		cout << "You have selected a diameter." << endl;
	double diameter;
	cin >> diameter;

	double circDiameter;
	circDiameter = 2 * PI * (diameter / 2);
}


int main(char choice, void calculation())
{
	select();
	calculation();

	return 0;
}


Any help?
What do you mean by saying "it crashes "?
It goes to command prompt, displays "Select r for radius or d for diameter: ", and when I press r, or d, it crashes saying "has stopped working properly. circumference.exe must close."
Your select function doesn't return anything, so it is essentially useless.

Your calculation function has weird parameters. You don't need to pass the select function into the calculation function at all, and it isn't done that simply anyway. When you call calculation, you give it no parameters at all.

I don't really see how this makes it through the compliler with no errors; there are a lot.
I wonder but your code shall not be compiled. For example this call of the function

calculation();

does not correspond its declaration.
Inside this function you also use an uninitialized variable.

If the code indeed is compiled you should write Microsoft about the bug of their compiler.

This statement is also very interesting!:)

int main(char choice, void calculation())


I think that the program crashes due to the statement above. Remove parameters from the declaration of main.

int main()
Last edited on
Saw the code taught it wouldn't compile, try to (with code blocks) had to remove #include "stdafx.h" and just gave me 2 warnings...
The code will be compiled because the declaration of the parameter calculation in the following statement

int main(char choice, void calculation())

hides the declaration of the function with the same name.:)
Leonardo94,
function `main' is a special function, it should be declared specially, as

int main ( int argc, char* argv[] )
{
}

or

int main ()

in your case both are possible.

Remember this once and for the rest of your (hope happy) life with C/C++.
int main ( int argc, char* argv[] ) -- this is the preferred way to do this.

You also have a number of other errors in the code, but this one is the main one, the one why this crashes.

Topic archived. No new replies allowed.