Thread: Program suddenly stops working.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    14

    Exclamation Program suddenly stops working.

    Hello, I'm fairly new to C, and after taking several examples from the book I decided to write my own. Yet I kept running into several problems with it, so i decided I'd just continue reading, and maybe I'd find the answer there. So I put in another example involving the switch statement, and ran into the same problem.

    The problem is, every time I run the program, it will bring up the little mini window that says "___ has stopped working" or something along those lines. But I think I've linked it to having to get a value from a variable, then using that same variable within the parentheses of a printf() function and using it in an arithmetic equation with another variable. The examples of code that I encountered this problem with were the following:


    Code:
    #include <stdio.h>
    
    int main(void)
    {
    float f, ff;
    char c;
    {
    printf("Type number, operator, number\n");
            
    scanf("%f %c %f", f, c, f);
            
    switch (c)
    {
    case '+':
        printf(" = %f", f + ff);
    case '-':
        printf(" = %f", f - ff);
    case '*':
        printf(" = %f", f * ff);
    case '/':
        printf(" = %f", f / ff);
    default:
        printf("Unknown operator");
    }
    printf("\n\n");
    }
    return 0;
    }
    This is the one from the book, only I had to add the heaer file, the void and the int around the main function, and return 0;. This is taken almost exactly from the book, but I used different names for variables, and indented differently. But I already know that doesn't matter.

    But the examples that I did narrowing it down to the conclusin that it only happened when it has <stdio.h>, printf, and variable arithmetic within it.

    Code:
    #include <stdio.h>
    
    main();
    {
    printf("%d\n" 2 + 2);
    
    return 0;
    }
    Code:
    #include <stdio.h>
    
    main();
    {
    int i = 2;
    
    printf("%d\n" i + i);
    
    return 0;
    }
    Code:
    #include <stdio.h>
    
    main();
    {
    int i;
    
    scanf("%d" i);
    
    printf("%d\n" i + i);
    
    return 0;
    }
    Those three were something similar, I'm not sure what they were exactly, but they were similar to that. I didn't test them, other than the last one, and when I compiled it, it gave me that same error, where it said that the program was not responding, and it needed to close. If it matters, I am using the Digital Mars compiler, the free version, and I'm running it from the command line.

    tl;dr: I have a problem where when I run a program that has a scanf(), and a variable within the printf() function's parentheses that I use with another variable in an arithmetic equation, it gives me the small window saying that the program is not responding andneeds to close. I am using the Digital Mars C compiler, and I'm running the free version from the Command Line. Any advice as to how I can fix this?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add the & to all your non-string scanf() calls:

    scanf("%f %c", &f, &c);

    etc.

    See what that does, I haven't checked the rest of your code.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    14
    Okay, it works now. It stopped giving me that error. Now, instead, it adds the addresses of the numbers together... but I can go figure that out somewhere else. I don't think it needs to be answered here.

    Thanks for the help!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by KittenAqua View Post
    Okay, it works now. It stopped giving me that error. Now, instead, it adds the addresses of the numbers together... but I can go figure that out somewhere else. I don't think it needs to be answered here.

    Thanks for the help!
    From your original posting... you are using ff in an uninitialized state.

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Also, you need commas between the arguments in functions:

    Code:
    printf( "%d\n" thing );  /*  WRONG  */
    printf( "%d\n", thing ); /* BETTER */
    Code:
    while(!asleep) {
       sheep++;
    }

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    14
    Oh ya... I was using it uninitialized. It was supposed to be the third variable who's value is gotten from scanf(), but I just didn't even realize it. It's not exactly an easy error to find when you're not told where the error is.

    Other than the bottom 3, which I rewrote hurriedly after deleting and didn't bother to test, I did put commas between arguments in the top one. At least, out of what I can see.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compiler stops working
    By akkiphadnis in forum C Programming
    Replies: 2
    Last Post: 04-04-2011, 03:42 AM
  2. Program stops working as soon as I input value?..
    By darkmagic in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2011, 02:18 AM
  3. Replies: 8
    Last Post: 03-29-2010, 04:38 AM
  4. program stops before EOF
    By doia in forum C Programming
    Replies: 2
    Last Post: 03-22-2010, 12:10 PM
  5. Program stops executing
    By bargomer in forum C++ Programming
    Replies: 16
    Last Post: 04-26-2008, 12:05 PM

Tags for this Thread