Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Why does this program crash?

Name: Anonymous 2009-12-05 20:51

#include <stdio.h>

int main(){
   
    int i, j, num;
   
    scanf("%d", num);
    for(i=2;i<num;i++){
    j=i;
    if(num%j==0){
    printf("The number is not prime.\n");
    break;}
    else
    printf("The number is prime.\n");
}

system("pause");

return 0;
}

Name: Anonymous 2009-12-05 20:56

Read scanf's signature more carefully.
You must pass a pointer to the place where it must write the values. You're passing the value instead.

Name: Anonymous 2009-12-05 20:57

Holy fuck nevermind, I am ass retarded.

Name: Anonymous 2009-12-05 20:57

scanf("%d", &num);

Name: Anonymous 2009-12-05 20:59

I'm feeling nice today,so num should be &num.
Now for the complaints
1. Formatting failure
2. No [code] tags
3. Using printf when you don't have formatting to do
4. system("pause");

Name: Anonymous 2009-12-05 20:59

>>5
Wow, I really should refresh more often.

Name: Anonymous 2009-12-05 21:03

scanf
Here's your problem

Name: Anonymous 2009-12-05 21:03

3. Using printf when you don't have formatting to do

is that really such a big deal?

also op:
j=i;

the hell you are doing this for?

Name: Anonymous 2009-12-05 21:10

I'm bad.

How do I do [code] tags? (if this works, then I got it right!)

Also what's wrong with system("pause")?

Name: Anonymous 2009-12-05 21:14

>>8
Oh wow, How come I didn't notice that before

Name: Anonymous 2009-12-05 21:25

>>9

If you run your program from the command line instead of just executing it from the IDE (I'm assuming you're using one) you don't have to use a delay, since the window doesn't close itself automatically.

Name: Anonymous 2009-12-05 21:34

The i=j thing is just a remnant from when I tried to figure out what I did wrong. Here's another piece of code that I'm having problems with:


#include <stdio.h>

int main(){
   
    float* num;
    int i, j;
    i=0;
    printf("Enter float: \n");
    scanf("%f", &num[i]);
    i++;
    printf("Enter float: \n");
    scanf("%f", &num[i]);
   
    printf("%f\n", num[i]*num[i-1]);
    i++;
    while(num[i]<num[i-1]){
    printf("Enter float: \n");
    scanf("%f", &num[i]);
    i++;
    printf("%f\n", num[i]*num[i-1]);
}

printf("Number less than previous. Program ending.\n");

system("pause");

return 0;
}


I know that it probably breaks all sorts of conventions, but it's only a quick practice that I wrote for finals next week. For the life of me I can't figure out why this one is crashing. The program doesn't even reach the second printf statement.

Name: Anonymous 2009-12-05 21:36

>>12
num is an uninitialized pointer. You're writing to random memory. You should alloc the num array somehow if you want to use it.

How about you go read K&R, instead of wasting /prog/'s time with newbie questions?

Name: Anonymous 2009-12-05 21:39

>>9
It's non-portable.  And unnecessary unless you are executing the program in an environment where you need to use some stupid work-around to keep the window open just so you can see your program's output.

Name: Anonymous 2009-12-05 22:03

Stop doing this guys homework.  He's a fucking idiot and it's better he learns it sooner than later.

Name: Anonymous 2009-12-05 22:32

>>15
I think it's because we have a soft spot for Cee, if he'd used #include<iostream>, cout, cin, etc I imagine he would have received an ungodly amount of abuse.

Name: Anonymous 2009-12-06 3:54

>>1
IT'S BECAUSE YOU FORGOT THE CODE TAGS

Name: Anonymous 2009-12-06 3:54

>>16
It's pronounced, "Seh"

Name: Anonymous 2009-12-06 7:31

shit was so crash

Don't change these.
Name: Email:
Entire Thread Thread List