Thread: Code worked, and then started crashing compiler

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    4

    Code worked, and then started crashing compiler

    Hello. I wrote this code to solve the common change question requiring the use of cases. It worked at first, I ran through it several times. Then for some reason it started crashing my compiler and I don't know why. I'm using Code::Blocks. I'm getting two message when I compile it but don't try to run it:



    |24|warning: passing argument 3 of 'change' from incompatible pointer type [enabled by default]|


    |18|note: expected 'int *' but argument is of type 'int **'|



    The code is as follows:



    Code:
    #include <stdio.h>
    
    
    int main()
    {
       /*Declare variables*/
       float total;
       int quarters, dimes, nickels, pennies;
    
    
       /*Function prototype*/
       void change (float total, int *quarters, int *dimes, int *nickels, int *pennies)
    
    
       {  /* First amount*/
          total =1.88;
    
    
          /*Call change function*/
          change(total, &quarters, &dimes, &nickels, &pennies);
    
    
          printf("\nTotal value: $%0.2f\n", total);
          printf("  Quarters: %d\n", quarters);
          printf("  Dimes: %d\n", dimes);
          printf("  Nickels: %d\n", nickels);
          printf("  Pennies: %d\n", pennies);
       }
    
    
       {  /*Second amount*/
          total =0.32;
    
    
          /*Call change function*/
          change(total, &quarters, &dimes, &nickels, &pennies);
    
    
          printf("\nTotal value: $%0.2f\n", total);
          printf("  Quarters: %d\n", quarters);
          printf("  Dimes: %d\n", dimes);
          printf("  Nickels: %d\n", nickels);
          printf("  Pennies: %d\n", pennies);
       }
    
    
       {  /*User entered amount*/
          printf("\n\nEnter the amount money in change that you are carrying:\n");
          scanf(" %f", &total);
    
    
          /*Call change function*/
          change(total, &quarters, &dimes, &nickels, &pennies);
    
    
          printf("\nTotal value entered: $%0.2f\n", total);
          printf("  Quarters: %d\n", quarters);
          printf("  Dimes: %d\n", dimes);
          printf("  Nickels: %d\n", nickels);
          printf("  Pennies: %d\n", pennies);
       }
    
    
       return 0;
    } /* end of main()*/
    
    
    
    
    /*This is the change function*/
    void change (float total, int *quarters, int *dimes, int *nickels, int *pennies)
    {
          if(total >= 0.25);
             *quarters = (total/0.25);
    
    
          if(total >=0.10);
             *dimes = (total - (*quarters * 0.25))/0.10;
    
    
          if(total >= 0.05);
             *nickels = (total - (*quarters * 0.25)-(*dimes * 0.10))/0.05;
    
    
          if(total < 0.05);
             *pennies = (total - (*quarters * 0.25)-(*dimes * 0.10)-
                        (*nickels * 0.05))/0.01 + 0.005;
    
    
          return 0;
    }


    Could someone please give me some idea why this is now crashing my compiler and not running when it worked before? Thanks in advance.

    Bridget

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Bridee123 View Post
    Then for some reason it started crashing my compiler and I don't know why.
    Your compiler isn't "crashing". Those are called compiler diagnostics, and they tell you where they've identified problems. What you need to do is go to the indicated line numbers and try to figure out what the problem is and correct it. If you can't figure it out, post the error message here along with the relevant context. For example, if your compiler says there is a problem on line 24, you probably should post lines 20-30 in code tags here in the message.

    Note that the line numbers in your editor may be different than what's on the screen here.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,655
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2015
    Posts
    4
    Quote Originally Posted by c99tutorial View Post
    Your compiler isn't "crashing". Those are called compiler diagnostics, and they tell you where they've identified problems. What you need to do is go to the indicated line numbers and try to figure out what the problem is and correct it. If you can't figure it out, post the error message here along with the relevant context. For example, if your compiler says there is a problem on line 24, you probably should post lines 20-30 in code tags here in the message.

    Note that the line numbers in your editor may be different than what's on the screen here.
    Hello. Thank you for the reply Actually, my program was literally crashing when I tried to run it. It was giving me the typical message about contacting Windows for a fix and then shutting down! I know, very odd, but it definitely wasn't the compiler diagnostics.

    I did post the compiler messages I was receiving; they are in my message above the code. They mention lines 18 and 24. Oddly enough though, the problem wound up being on line 10, I forgot to put a semicolon at the end of the function prototype. Actually, I had it there (that's why the program originally worked), but I must have accidentally deleted it, leading to the problem.

    Anyway, thank you for your help.

    Bridget

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    1. Why are you declaring a function inside another function?
    2. Why are you putting curly brackets around sections of your code?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Sep 2015
    Posts
    4
    Quote Originally Posted by Elkvis View Post
    1. Why are you declaring a function inside another function?
    2. Why are you putting curly brackets around sections of your code?

    1. The instructions for the assignment said to do it from within the main function.
    2. The book I'm using said to use the curly brackets for clarity.

    Please let me know if that's not actually a good way to code, but I have to follow the directions in this particular instance.

    Bridget

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Quote Originally Posted by Bridee123
    1. The instructions for the assignment said to do it from within the main function.
    The instructions probably told you either to declare (but not define) the function within the main function, or more likely, to call the function within the main function. Standard C does not permit you to define a function within another function.

    Quote Originally Posted by Bridee123
    2. The book I'm using said to use the curly brackets for clarity.
    What your book probably meant is that when you have control statements in which the braces are optional, include them anyway. For example, you could write:
    Code:
    if (x)
        y();
    else
        z();
    but it can be clearer to write:
    Code:
    if (x)
    {
        y();
    }
    else
    {
        z();
    }
    This is different from having code blocks the way you did all over your function's body: if you need to do that, consider pulling out these sections of code into their own functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2015
    Posts
    4
    Quote Originally Posted by laserlight View Post
    The instructions probably told you either to declare (but not define) the function within the main function, or more likely, to call the function within the main function. Standard C does not permit you to define a function within another function.


    What your book probably meant is that when you have control statements in which the braces are optional, include them anyway. For example, you could write:
    Code:
    if (x)
        y();
    else
        z();
    but it can be clearer to write:
    Code:
    if (x)
    {
        y();
    }
    else
    {
        z();
    }
    This is different from having code blocks the way you did all over your function's body: if you need to do that, consider pulling out these sections of code into their own functions.


    Okay, I understand. You are right on both counts (no surprise there lol). Thank you for taking the time to explain these issues to me

    Bridget

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler crashing
    By happynova in forum C Programming
    Replies: 11
    Last Post: 05-03-2015, 08:29 AM
  2. C program Quincy Compiler keeps Crashing after use of malloc
    By awfulyconfused in forum C Programming
    Replies: 4
    Last Post: 08-17-2011, 02:42 AM
  3. C code crashing question.
    By goflswl00 in forum C Programming
    Replies: 7
    Last Post: 05-31-2010, 08:44 PM
  4. Compiler Crashing... WHY?
    By kippwinger in forum C++ Programming
    Replies: 6
    Last Post: 07-03-2003, 12:10 AM
  5. crashing code
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 06-09-2003, 10:09 AM

Tags for this Thread