3 Replies - 676 Views - Last Post: 26 April 2015 - 07:48 PM Rate Topic: -----

#1 fallonrae   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-April 15

Problem with user defined function in C

Posted 26 April 2015 - 07:27 PM

my user defined function does not seem to be working correctly here is the code:
#include <stdio.h>

[// Function Declarations 

float sumNP(int nickels, int pennies);
 

int main()
{
   // Declarations 
   int quarters = 0, dimes = 0, nickels = 0, pennies = 0;
   float sumofQD = 0.0;
   float sumofNP = 0.0;
   float dollarAmt = 0.0;
   
   // Input
   printf("Please enter the amount of nickels: ");
   scanf("%d", &nickels);
   
   printf("Please enter the amount of pennies: ");
   scanf("%d", &pennies);
   
   // Process
   sumofNP = sumNP(nickels, pennies);
   scanf("%f", &sumofNP);
   
   
   // Output
   printf("The total dollar amount is :$%.2f", sumofNP);
   
   return 0;
   
}


float sumNP(int nickels, int pennies)
{
   float sumNP;
   sumNP = .05 * nickels + .01 * pennies;
   return sumNP;
}]


instead of printing out the actual sum this is what it does when I try to run it:
[Please enter the amount of nickels: 3
Please enter the amount of pennies: 3
3
The total dollar amount is :$3.00]

after I press 3 for pennies and press enter it does to the next line and expects input, then that is the output.

Overall my question is what is wrong with my function? why isn't the program coming up with the correct output?

This post has been edited by jimblumberg: 26 April 2015 - 07:55 PM
Reason for edit:: Added missing code tags please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with user defined function in C

#2 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Problem with user defined function in C

Posted 26 April 2015 - 07:36 PM

you requested a scanf the overwrote sumofNP , you typed 3 it printed $3.00
Was This Post Helpful? 0
  • +
  • -

#3 tarmizi_adam2005   User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 986
  • Joined: 18-April 09

Re: Problem with user defined function in C

Posted 26 April 2015 - 07:38 PM

Hi,

Your code produces a bunch warnings and an error. You should increase your compiler warning level and fix these warnings/error:

||In function 'main':|
|10|warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]|
|10|warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]|
|11|warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]|
|11|warning: incompatible implicit declaration of built-in function 'scanf' [enabled by default]|
|17|warning: implicit declaration of function 'sumNP' [-Wimplicit-function-declaration]|
|7|warning: unused variable 'dollarAmt' [-Wunused-variable]|
|5|warning: unused variable 'sumofQD' [-Wunused-variable]|
|4|warning: unused variable 'dimes' [-Wunused-variable]|
|4|warning: unused variable 'quarters' [-Wunused-variable]|
|29|error: conflicting types for 'sumNP'|
|17|note: previous implicit declaration of 'sumNP' was here|
||=== Build failed: 1 error(s), 9 warning(s) (0 minute(s), 5 second(s)) ===|


Notice the error "conflicting types for 'sumNP'". You have a function sumNP() and a variable float sumNP in side that function ?
Was This Post Helpful? 1
  • +
  • -

#4 fallonrae   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-April 15

Re: Problem with user defined function in C

Posted 26 April 2015 - 07:48 PM

View Postinfernorthor, on 26 April 2015 - 07:36 PM, said:

you requested a scanf the overwrote sumofNP , you typed 3 it printed $3.00


Wow! Thank you so much! it works now!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1