-
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.
-
> 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.
-
Ok, I'll try that tomorrow.
-
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");
}
-
- 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?
-
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.
-
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.
-
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