10 Replies - 898 Views - Last Post: 22 September 2015 - 08:54 PM Rate Topic: -----

#1 Bridee123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-September 15

Code worked, then started crashing compiler

Posted 22 September 2015 - 12:39 PM

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:

#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

Is This A Good Question/Topic? 0
  • +

Replies To: Code worked, then started crashing compiler

#2 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Code worked, then started crashing compiler

Posted 22 September 2015 - 01:12 PM

you have mised the ; off the end of the function prototype in line 10
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3854
  • View blog
  • Posts: 14,102
  • Joined: 08-August 08

Re: Code worked, then started crashing compiler

Posted 22 September 2015 - 01:16 PM

Why are you mixing pointers and references?? This being C you should probably stick with pointers, but not for int and float. Just pass by value.

I'd take 100 * total and store it in an integer, then divide by 25 to get the number of quarters and use modulus to get the remaining change. Continue with dimes....
Was This Post Helpful? 0
  • +
  • -

#4 Bridee123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-September 15

Re: Code worked, then started crashing compiler

Posted 22 September 2015 - 01:17 PM

View Posthorace, on 22 September 2015 - 01:12 PM, said:

you have mised the ; off the end of the function prototype in line 10


Horace,

Thank you so much. I did look it over a ton of times but I missed that. It must have been there originally for the program to run and then I must have deleted it accidentally.

I wish the compiler would have indicated what was wrong, or at least that it was on line 10/11.

Again, thank you.

Bridget
Was This Post Helpful? 0
  • +
  • -

#5 Bridee123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-September 15

Re: Code worked, then started crashing compiler

Posted 22 September 2015 - 01:49 PM

View PostCTphpnwb, on 22 September 2015 - 01:16 PM, said:

Why are you mixing pointers and references?? This being C you should probably stick with pointers, but not for int and float. Just pass by value.

I'd take 100 * total and store it in an integer, then divide by 25 to get the number of quarters and use modulus to get the remaining change. Continue with dimes....


Hi. Thank you for the tip :) I started teaching myself C programming from a book two days ago so I'm probably overthinking some things and under-thinking others.

Bridget
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3854
  • View blog
  • Posts: 14,102
  • Joined: 08-August 08

Re: Code worked, then started crashing compiler

Posted 22 September 2015 - 02:00 PM

Just remember that floating point numbers have rounding errors. This code:
	float num1 = 2.02;
	float num2 = .02;
	for (int i = 0; i < 100; i++) {
		num1 += num2;
	}
	printf("%f\n",num1);


produces this output on my system:
4.019998
This is why money is often store in pennies and converted to dollars.cents for display.
Was This Post Helpful? 0
  • +
  • -

#7 Bridee123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-September 15

Re: Code worked, then started crashing compiler

Posted 22 September 2015 - 02:42 PM

View PostCTphpnwb, on 22 September 2015 - 02:00 PM, said:

Just remember that floating point numbers have rounding errors. This code:
	float num1 = 2.02;
	float num2 = .02;
	for (int i = 0; i < 100; i++) {
		num1 += num2;
	}
	printf("%f\n",num1);


produces this output on my system:
4.019998
This is why money is often store in pennies and converted to dollars.cents for display.



Hi. I hadn't really thought about rounding errors with floating point numbers. I have been worried about the way the result from integer division is cut off instead of rounded. It seems so odd.

I will try out that code. Thanks :)


Bridget
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5910
  • View blog
  • Posts: 17,924
  • Joined: 25-December 09

Re: Code worked, then started crashing compiler

Posted 22 September 2015 - 03:09 PM

Also posted here.
Was This Post Helpful? 1
  • +
  • -

#9 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 12288
  • View blog
  • Posts: 20,837
  • Joined: 19-March 11

Re: Code worked, then started crashing compiler

Posted 22 September 2015 - 03:18 PM

Just as a side note, your code did not "crash your compiler". I suppose it would be possible for a very fragile compiler to crash on some source code input, but nothing that you're likely to use will ever exhibit this sort of misbehavior. Instead, what's going to happen - and what did happen here - is that the compiler will stop processing input once it knows that you have an error, and it'll do its best to help you understand what was the nature of the error and where it might have occurred. This is a little difficult to do, so you're not always going to get a nice "the problem is here". Instead, you're often going to learn where the compiler first became aware of the problem, and what sort of problem it discovered. It's your job, as the programmer, to work your way back from this problem to your failure to correctly express your intention.
Was This Post Helpful? 1
  • +
  • -

#10 Bridee123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-September 15

Re: Code worked, then started crashing compiler

Posted 22 September 2015 - 03:42 PM

View Postjon.kiparsky, on 22 September 2015 - 03:18 PM, said:

Just as a side note, your code did not "crash your compiler". I suppose it would be possible for a very fragile compiler to crash on some source code input, but nothing that you're likely to use will ever exhibit this sort of misbehavior. Instead, what's going to happen - and what did happen here - is that the compiler will stop processing input once it knows that you have an error, and it'll do its best to help you understand what was the nature of the error and where it might have occurred. This is a little difficult to do, so you're not always going to get a nice "the problem is here". Instead, you're often going to learn where the compiler first became aware of the problem, and what sort of problem it discovered. It's your job, as the programmer, to work your way back from this problem to your failure to correctly express your intention.


Hi. Okay, that made much more sense to me than before. I see that I'm going to have to be very careful on these forum and not use words like "crash" as a layperson would lol.

Thank you for taking the time to explain what happened, and how the errors might appear. That will help me in the future.

Bridget
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 12288
  • View blog
  • Posts: 20,837
  • Joined: 19-March 11

Re: Code worked, then started crashing compiler

Posted 22 September 2015 - 08:54 PM

Glad I could help. Have fun!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1