Code stops working when run :(

This is my code (a division calculator):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <stdlib.h>
#define p printf
#define s scanf


int x;
int y;
int z;

void divider()
{
z=x/y;
}

int main()
{
    s("%d", x);
    s("%d", y);
    divider();
    system ("pause");
    return 0;
}


I can compile it, but when I run it and input a value for x the program stops working. :(
Last edited on
I can compile it


That's odd, because the code you supplied won't compile.
Sorry for that, I defined s as scanf and p as printf. I forgot to put it there
Even doing that wouldn't be enough to make it compile.

However,

scanf( "%d", &x);

scanf expects a pointer, not an int.
But I can compile it with Dev C++ :/
and what's a pointer? (:
I'm new to this (:
I wouldn't bother using defines for printf or scanf. Are you wanting to learn C or C++? C++ uses cout & cin instead of print & scanf. You might find these easier to use, if you a not familiar with pointers.

Your program uses integer division - which won't give the answer you expect. 5 / 3 is 1. So you need to change the type to double.

You should also check for division by zero. To make this easier at this stage, check that y is not less than 0.0001 say, rather than a direct comparison with 0.0.

I think you need to read up some tutorials:

http://www.cplusplus.com/doc/tutorial/


The reference section on cplusplus is a great resource- try to make use of it and Google

Hope all goes well. :D
Topic archived. No new replies allowed.