Thread: Credit account

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    5

    Credit account

    Hey everyone,
    I am new to programming, been taking a programming concepts course at school. So far so good with some exceptions. Anyhow this is my question. I am working on this credit account assignment. It was already due and I have turned in a half-ass POS, because honestly I was lost on how to start it. So I went to barnes&noble and bough a C programming absolute beginners guide.
    What I have done so far after the fact looks better and functions like I want it to, but my issue now is getting the it to loop again if there is available credit. This is what I have done so far. I know it's not a good looking code, but so far it works.

    felix87871

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    main() {
        int charge, totalCharge = 0, credit, creditPayment = 0, beginningBal, balance, creditLimit = 500, newBalance, availableCredit, availableCredit1, acctNumber;
    
    
        acctNumber = 407;
        printf("Account number: %i \n", acctNumber);
    
    
        beginningBal = 250;
        printf("Balance at the beginning of the month: %i \n", beginningBal);
    
    
        printf("Enter a credit charge (-1 to quit):");
        scanf_s("%i", &charge);
    
    
        while (charge != -1) {
            totalCharge = totalCharge + charge;
            printf("Enter a credit charge (-1 to quit):");
            scanf_s("%i", &charge);
        }
        printf("The sum of the credit charges is: %i\n", totalCharge);
        balance = totalCharge + beginningBal;
        printf("Balance on credit after charges is: %i\n", balance);
    
    
        printf("Enter a credit payment (-1 to quit):");
        scanf_s("%i", &credit);
    
    
        while (credit != -1) {
            creditPayment = creditPayment + credit;
            printf("Enter a credit payment (-1 to quit):");
            scanf_s("%i", &credit);
            printf("The sum of the credit payments is: %i\n", creditPayment);
    
    
            newBalance = beginningBal + totalCharge - creditPayment;
    
    
            creditLimit = 500;
            if (newBalance > creditLimit){
                availableCredit1 = 0;
                newBalance = beginningBal + totalCharge - creditPayment;
                printf("Account number: %i \n", acctNumber);
                printf("creditLimit : %i \n", creditLimit);
                printf("available credit is: %i \n", availableCredit1);
                printf("The new balance is: %i\n", newBalance);
                printf("You have exceeded your credit limit! \n");
            } else{
                availableCredit = creditLimit - newBalance;
                printf("Available credit is: %i\n", availableCredit);
                printf("Do you wish to continue? \n");
            }
        }
        
        system("pause");
    }


    I'm doing this for my own personal experience and to get a better understanding of programming.

  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
    > but my issue now is getting the it to loop again if there is available credit.
    You already have
    while (credit != -1)

    It's the same idea.
    You find the start and end statements of the block of code you want to repeat, and you put some kind of loop around it (with braces), and then apply whatever condition you want.
    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
    Nov 2013
    Posts
    5
    Ok, I'll try that tomorrow.

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    Ok, here is what I think the finished code is.....any thoughts are welcomed.

    felix87871

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    main() {
    	int charge, totalCharge = 0, credit, creditPayment = 0, beginningBal, balance, creditLimit = 500, newBalance, availableCredit, availableCredit1, acctNumber;
    
    
    	acctNumber = 407;
    	printf("Account number: %i \n", acctNumber);
    
    
    	beginningBal = 250;
    	printf("Balance at the beginning of the month: %i \n", beginningBal);
    
    
    	printf("Enter a credit charge (-1 to quit):");
    	scanf_s("%i", &charge);
    
    
    	while (charge != -1) {
    		totalCharge = totalCharge + charge;
    		printf("Enter a credit charge (-1 to quit):");
    		scanf_s("%i", &charge);
    	}
    	printf("The sum of the credit charges is: %i\n", totalCharge);
    	balance = totalCharge + beginningBal;
    	printf("Balance on credit after charges is: %i\n", balance);
    	
    	newBalance = beginningBal + totalCharge - creditPayment;
    	creditLimit = 500;
    		if (newBalance > creditLimit){
    			availableCredit1 = 0;
    			newBalance = beginningBal + totalCharge - creditPayment;
    			printf("Account number: %i \n", acctNumber);
    			printf("creditLimit : %i \n", creditLimit);
    			printf("available credit is: %i \n", availableCredit1);
    			printf("The new balance is: %i\n", newBalance);
    			printf("You have exceeded your credit limit! \n");
    		} else{
    			availableCredit = creditLimit - newBalance;
    			printf("Available credit is: %i\n", availableCredit);
    			printf("Do you wish to continue? \n");
    
    
    			printf("Enter a credit charge (-1 to quit):");
    			scanf_s("%i", &charge);
    			while (charge != -1) {
    				totalCharge = totalCharge + charge;
    				printf("Enter a credit charge (-1 to quit):");
    				scanf_s("%i", &charge);
    			}
    		}
    
    
    	printf("Enter a credit payment (-1 to quit):");
    	scanf_s("%i", &credit);
    
    
    	while (credit != -1) {
    		creditPayment = creditPayment + credit;
    		printf("Enter a credit payment (-1 to quit):");
    		scanf_s("%i", &credit);
    	}		
    		printf("The sum of the credit payments is: %i\n", creditPayment);
    		newBalance = beginningBal + totalCharge - creditPayment;
    		printf("The new balance is: %i\n", newBalance);
    		printf("Do you wish to continue? \n");
    
    
    	system("pause");
    }

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    • Change main() to int main(void)
    • system("pause"); will only work on Windows/MS-DOS or other systems where there is a system program called pause (Don't use it)
    • There is no return value from main, although technically this is not an error (for main(), reaching the close brace is equivalent to return 0; although I'd explicitly have a return value there; returning 0 by default is not true for other functions)


    Edit: scanf_s is an optional function only available (if the compiler wants to) in C11. I'd avoid it unless you have some special reason for using it?
    Last edited by SirPrattlepod; 11-02-2013 at 10:36 PM.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    Hey Sirprattlepod
    I'll make those changes, when I get home later today.
    Forgive my idiotness, but I do have questions now from what you posted.
    Why change from main() to int main(void)?
    I am working on a Windows system, I've noticed that if system("pause") wasn't there it would quickly do the calculation and go away.
    In my online class they haven't shown me the reason behind having a return value from main, so I wouldn't know how to utilize this, sorry. I'm just going by what I was taught and know.
    The reason for me using scanf_s is that when I use scanf and debug it, it tells me there's an error (using visual basics 2012 ultimate). That's the only reason. If it didn't give me an error I wouldn't use it.
    felix87871
    Edit... Thank you and +rep to you and Salem with the feed back that I'm getting in understanding C language.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    I just want to mention that what I have learned from my online course is;
    The very basic of C. main(), printf, scanf, int, double, system("pause"), %, &, while loop, sentinel loop, math.h, time.h, switch, case, break, if else statement...... Still have a video that I have to watch. Reading a book (called C programming absolute beginners guide) in my car that gives more clarity and definition on what it is that I'm doing while the wife and son is at church.

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    If you are programming in windows and want to keep the console open just use getchar(); at the bottom of main.

    As far as int main vs void main, read this
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Credit Reports
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-10-2003, 12:53 AM
  2. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM
  3. Accepting Credit Cards
    By Mattman in forum C++ Programming
    Replies: 4
    Last Post: 03-17-2003, 03:55 PM
  4. Please help me with this Extra Credit program
    By DenisGFX in forum C Programming
    Replies: 10
    Last Post: 05-19-2002, 01:48 AM
  5. Deserving Credit
    By golfinguy4 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-02-2002, 01:50 PM