Thread: Void Functions with an if else construct, not printing multiple lines.

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    3

    Cool Void Functions with an if else construct, not printing multiple lines.

    This code i made is a cent converter from 5 to 95 cents. The problem i'm receiving is when the 'cents' function is sent back to the 'main' function it only prints one line. It seems to just print the first if construct that complies with the statement. Is there anyway i can have this function print multiple cent values? For example if 60 cents was entered it would only print '50c', and i want it to print '50c' and '10c' instead, thanks.


    Code:
    #include <stdio.h>
    
    
    int x;
    void check(int x)
    {
    if( x < 5)
        printf("Less then 5 cannot be calculated\n");
    else if(x > 95)
        printf("More then 95 cannot be calculated\n");
    return;
    }
    
    
    void cents(int x)
    {
    if( x/5 >= 1)
        printf("5c");
    else
        if( x/10 >= 1)
            printf("10c");
        else
            if( x/20 >= 1)
                printf("20c");
            else
            
                if( x/50 >= 1)
                    printf("50c");
                else
    return;
    }
    
    
    int main()
    {
        int amount;
        
        printf("Please enter the amount of cents(5 to 95): ");
        scanf("%d%*c", &amount);
        printf("You're change is: \n");
        check(amount);
        cents(amount);
        return(0);
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Well imagine the user enters 50, what is the logic of the if - else statements going to do? Trace it through.

    Best idea is to check for the largest value first and gradually work down to smaller values.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Copy/paste from the tutorials here:If Statements in C - Cprogramming.com

    Another use of else is when there are multiple conditional statements that may all evaluate to true, yet you want only one if statement's body to execute. You can use an "else if" statement following an if statement and its body; that way, if the first statement is true, the "else if" will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements to ensure that only one block of code is executed.
    -------------------------------------------------------------------------------------------------------------

    Think of else ifs as sort of an exclusive or with arguments attached. It's why it will only print 5 cents (not 50). You will also have to operate on x so that the amount of change you've already displayed is subtracted from x in order to get correct change. Think if someone enters 60 and all those conditional statements did come true, you would be displaying 95 cents.
    Last edited by Alpo; 04-12-2014 at 05:29 PM. Reason: I < math

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Spoilers below: Still doesn't work well, but a bit better.

    Code:
    void cents(int x){
        while(x>=6){
            if(x/50>=1 && x/50<=2){
                printf("50c");
                x=x-50;
            }
            if(x/20>=1 && x/20<=2){
                printf("20c");
                x=x-20;
            }
            if(x/10>=1 && x/10<=2){
                printf("10c");
                x=x-10;
            }
            if(x/5>=1 && x/5<=2){
                printf("5c");
        }
    }
    Last edited by Alpo; 04-12-2014 at 05:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 12:45 PM
  2. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  3. Printing an array in lines of 10.
    By furiousferret in forum C++ Programming
    Replies: 10
    Last Post: 11-16-2004, 07:30 PM
  4. Printing 20 lines at a time
    By csmatheng in forum C Programming
    Replies: 5
    Last Post: 04-30-2002, 04:11 PM
  5. Printing multi-lines?
    By SyntaxBubble in forum Windows Programming
    Replies: 4
    Last Post: 11-27-2001, 04:52 PM

Tags for this Thread