Thread: help with coding...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    25

    help with coding...

    ok what i need help here is with calling the function 'inputinfo' in the 'main()'. what am i doing wrong here..? and also if i try the same method with the rest of the functions will there be errors as well? (program is obviously incomplete working on it now, i just want to get each function working bit by bit in the main() by calling each one. the question requires me to write functions for each operation such as inputting numbers and printing out numbers or formulae. i just need to call them now in the main(). thanks for the help.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int isValidInput(double amount, double annualeRate, int nmonths);
    float interestmonth(float IM, int IY);	/* declaration */
    int valueP(int P, int IM, int NM);
    int valueQ(int Q, int P);
    int monthlypayment(int MP, int PR, int IM, int Q);
    void inputInfo(double *amountPtr, double *annualRatePtr, int *nmonthsPtr);
    
    main ()
    {
    	int NM, IM, PR, P, Q, MP;
       int *nmonthsPtr;
       double *amountPtr, *annualRatePtr;
       float IY;
    
       inputInfo(double *amountPtr, double *annualRatePtr, int *nmonthsPtr)
    
    
    	return 0;
    }
    
    void inputInfo(double *amountPtr, double *annualRatePtr, int *nmonthsPtr)
    {   /*Use the double type specifier to define an identifier to be a floating-point data type*/
    
       int *amountPtr = NULL;           /*initializing pointers*/
       float *annualRatePtr = NULL;
       int *nmonthsPtr = NULL;
    
       amountPtr = &PR;            /*assigning address of a variable to a*/
       annualRatePtr = &IY;        /*compatible pointer using the & operator*/
       nmonthsPtr = &NM;
    
    
    	printf("1. Amount of loan (Principal)?");                /* a) */
       scanf("%d", &PR);                                       /*input numbers*/
       printf("2. Interest rate per year (in percent)?");
       scanf("%f", &IY);
       printf("3. Number of months to amortize loan?");
       scanf("%d", &NM);
       printf("Input entered\n 1. %d\n 2. %f\n 3. %d\n", PR, IY, NM);   /*prints numbers entered*/
       scanf("%d");
    }
    
    /*functions: math formulae*/
    
    
    
    
    int isValidInput (double amount, double annualRate, int nmonths)
       {
       	if(0 > amount || annualRate || nmonths > 100)
          {
          return true;                            /* validating entry of amount */
          }                                       /* annualRate and nmonths     */
          else
          {
          return false;
          }
       }
    
    
    float interestmonth(float IM, float IY)
    {                                              /*interest month equation*/
    	IM = (IY) / (12 * 100);
    }
    
    int valueP(int P, int IM, int NM)                     /*equation for valueP*/
    {
    	P = ((1 + IM)pow(NM));
    }
    
    int valueQ(int Q, int P)
    {                                             /* equation for valueQ*/
    	Q = P / (P - 1);
    }
    
    int monthlypayment(int MP, int PR, int IM, int Q)         /*equation for monthlypayment*/
    {
    	MP = (PR * IM * Q);
    }
    Last edited by Salem; 04-26-2004 at 06:38 AM. Reason: Tagging

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This is what you need
    Code:
    int main ()
    {
       int NM, IM, PR, P, Q, MP;
       int nmonths;
       double amount, annualRate;
       float IY;
    
       inputInfo(&amount, &annualRate, &nmonths);
       return 0;
    }
    
    void inputInfo(double *amountPtr, double *annualRatePtr, int *nmonthsPtr)
    {   /*Use the double type specifier to define an identifier to be a floating-point data type*/
       printf("1. Amount of loan (Principal)?");                /* a) */
       scanf("%f", amountPtr);                                       /*input numbers*/
       printf("2. Interest rate per year (in percent)?");
       scanf("%f", annualRatePtr);
       printf("3. Number of months to amortize loan?");
       scanf("%d", nmonthsPtr);
       printf("Input entered\n 1. %f\n 2. %f\n 3. %d\n",
          *amountPtr, *annualRatePtr, nmonthsPtr);   /*prints numbers entered*/
    }
    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.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    25
    why didn't you have this in your code?? and you above code with the part calling the function inputinfo(blah blah); doesn't work.

    Code:
    int *amountPtr = NULL;           /*initializing pointers*/
       float *annualRatePtr = NULL;
       int *nmonthsPtr = NULL;
    
       amountPtr = &PR;            /*assigning address of a variable to a*/
       annualRatePtr = &IY;        /*compatible pointer using the & operator*/
       nmonthsPtr = &NM;
    Last edited by ahming; 04-26-2004 at 06:51 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This works!!!
    Code:
    #include <stdio.h>
    void inputInfo(double *amountPtr, double *annualRatePtr, int *nmonthsPtr);
    int main ()
    {
       int nmonths;
       double amount, annualRate;
       inputInfo(&amount, &annualRate, &nmonths);
       return 0;
    }
    
    void inputInfo(double *amountPtr, double *annualRatePtr, int *nmonthsPtr)
    {
       printf("1. Amount of loan (Principal)?");
       scanf("%lf", amountPtr);
       printf("2. Interest rate per year (in percent)?");
       scanf("%lf", annualRatePtr);
       printf("3. Number of months to amortize loan?");
       scanf("%d", nmonthsPtr);
       printf("Input entered\n 1. %f\n 2. %f\n 3. %d\n",
          *amountPtr, *annualRatePtr, *nmonthsPtr);
    }
    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.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    25
    cool thanks thanks i understand now

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    25
    been spendint he the past 1 hour trying to implement this code into the above code posted by salem... can't get it to work

    Code:
    int isValidInput (double amount, double annualRate, int nmonths)
       {
       	if(0 > amount || annualRate || nmonths > 100)
          {
          return true;                            /* validating entry of amount */
          }                                       /* annualRate and nmonths     */
          else
          {
          return false;
          }
       }

  7. #7
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    Code:
    int main (void) {
    	int annualrate = 1;
    	int annualyield = 0;
    	int currentrate = -5;
    	
    	if (annualrate) {
    		printf("true\n");
    	} else {
    		printf("false\n");
    	}
    	
    	if (annualyield) {
    		printf("true\n");
    	} else {
    		printf("false\n");
    	}
    	
    	if (currentrate) {
    		printf("true\n");
    	} else {
    		printf("false\n");
    	}
    	
    	return 0;
    }
    Output:
    true
    false
    true

    Notice that if you're only using a single variable in a conditional expression, the conditional expression will only be false if the value is 0 and only 0.

    EDIT:
    Take the above code, and the value you're passing into the function, and see if you can find what is wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 02:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM