Thread: Void Function

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    4

    Void Function

    Hello I have been encountering some problems with my void function coding. When I enter my three values the command prompt just stops and nothing happens, I would appreciate any advice thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    
    void repayments(float, float, float); // function prototype
    
    
    int main() 
    {
        float cost, deposit, interest_rate;
        
        // get values from user
    
    
        do
        {
         printf("Enter the cost of the car:\n");
        scanf("%f", &cost);
          if (cost <= 0)
        printf("ERROR: the cost must be higher than 0\n");
          }
        while(cost <= 0);
    
    
        do
        {
          printf("Enter the deposit paid:\n");
          scanf("%f", &deposit);
        if (deposit <= 0 || deposit > cost)
        printf("ERROR: the deposit can't be less than or equal to 0 or greater than cost\n");
        }
        while(deposit <= 0 || deposit > cost);
    
    
        do
        {
        printf("Enter the interest rate:\n");
        scanf("%f", &interest_rate);
        if (interest_rate < 0 || interest_rate > 100)
        printf("ERROR: Interest rate must be between 1 and 100\n");
        }
        while(interest_rate < 0 || interest_rate > 100);
        
        repayments(cost, deposit, interest_rate); // function call with 3 arguments
        
        return 0;
    }
    
    
    void repayments(float c, float d, float ir) // function header
    {
        short int i; // local variable
        
        for(i = 1; i <= 5; i+12); // 5 iterations - 12, 24, 36, 48, 60
        {
            printf("Your repayment after %hd months is %f\n" , i+12, ((c - d)/i+12)*(ir));
        }
        
        return;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your for loop increment is wrong. It should be i++
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. control reaches end of non void function in bool function
    By Giwrgows x in forum C Programming
    Replies: 26
    Last Post: 12-19-2015, 02:58 PM
  2. difference between void and static void function
    By mahaju in forum C++ Programming
    Replies: 7
    Last Post: 12-27-2011, 04:02 AM
  3. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM
  4. void, void function
    By Furious_George in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2003, 05:04 PM
  5. void non void function
    By modance in forum C Programming
    Replies: 6
    Last Post: 01-28-2003, 08:06 AM

Tags for this Thread