#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    11
    Rep Power
    0

    Help with Ideas for my program


    I'd like some ideas of how to make this program.

    The program would prompt the user 3 options, Upfront Payment, Crediting Payment or Pay off Credit for Customer.

    If Upfront payment is chosen, Prompt for Customer_name and Purchase Amount. Purchase amount is added to total sales.

    If Crediting Payment is chosen, Prompt for Customer_name, Crediting Amount, Tel_num, and Address. Crediting Amount is added to Total Credits.

    If Pay off Credit for Customer is chosen, Prompt for Customer_name and amount paying. Amount Paying will then be subtracted from the customer's crediting amount and the total credits and added to total sales.

    Print Crediting Customer along with their Remaining crediting balance and information.



    I am "kind of" a beginner in C so may I ask you to please go a long way in your ideas for me. This may actually be kind of too hard for my level but I wanted to try this out.

    I know it involves Files and maybe structs but I don't know how to utilize them well yet (I'd love advice and ideas in these areas)

    It's actually an assignment that I came up with myself that I have to turn in Friday.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    the algorithm is self explanatory.create some functions and link it with the main() function.
    try to do yourself and post your code here and it will be checked for further improvements.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    ..
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,704
    Rep Power
    480
    Code:
    #include<stdlib.h>
    int main() {
      system("quickbooks");
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    11
    Rep Power
    0

    Here is my code. It doesn't work. Stops somewhere at the first fprintf.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char customer_name[30], cred_payingnm[30], address[30], choice2;
    	int choice1, tel_num, counter = 1;
    	float purchase_amt, total_sales = 0, credit_amt, creditor_bal = 0, total_credit = 0, paid, remaining_cred = 0;
    
    	FILE*fp;
    		fp = fopen("customer.dat", "w+");
    		
    
    				for(;;)
    				{
    					printf("To choose option, type the appropriate number.\n");
    					printf("Upfront Payments(1), Crediting Payments(2), Payoff Credits(3)\n");
    					scanf("%d", &choice1);
    						switch(choice1)
    						{
    							case 1: printf("Enter Customer name and Purchase Amount\n");
    								scanf("%s %f", customer_name, &purchase_amt);
    								total_sales = total_sales + purchase_amt;
    								break;
    			
    							case 2: if(fp = NULL)
    									{
    				           				printf("File could not be opened\n");
    									}
    									else
    									{
    
    										printf("Enter Customer name, Credit Amount, Telephone Number and Address\n");
    										scanf("%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
    
    										fprintf(fp, "%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
    
    										total_credit = total_credit + credit_amt;
    										remaining_cred = total_credit;
    
    									}
    								break;
    
    							case 3: if(fp = NULL)
    									{
    				           				printf("File could not be opened\n");
    									}
    									else
    									{
    					
    										printf("Enter Customer name and amount of money\n");
    										scanf("%s %f", cred_payingnm, &paid);
    
    					
    
    										fscanf(fp, "%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
    											if(strcmp(cred_payingnm, customer_name) == 0)
    											{
    												 creditor_bal= credit_amt - paid;
    											}
    											else
    											{
    												 printf("Creditor name does not exist\n");
    											}
    
    										remaining_cred = total_credit - paid;
    										total_sales = total_sales + paid;
    									}
    								break;
    
    								default: printf("Wrong number chosen\n");
    								break;
    
    						}
    			
    
    							printf("Do you want to do anything else? Y or N\n");
    							scanf(" %c", &choice2);
    								if(choice2 == 'Y'|| choice2 == 'y')
    								{
    								continue;
    								}
    								else
    								{
    								break;
    								}
    
    				}
    
    				fscanf(fp, "%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
    
    					while(strcmp(customer_name, "ENDDATA") != 0)
    					{
    						printf("%-10s %-10s %-10s %-10s", "Customer", "Credited Amount", "Creditor's Balance", "Telephone #", "Address");
    						printf("%-10s %10f %10f %-10d %-10s", customer_name, credit_amt,  creditor_bal, tel_num, address);
    					}
    
    				printf("Total Sales %.2f \n", total_sales);
    				printf("Total credited %.2f\n", total_credit);
    				printf("Total Remaining Credit\n%.2f", remaining_cred);
    
    					fclose(fp);
    
    			getchar();
    			getchar();
    			return 0;
    }
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,090
    Rep Power
    2222
    Your indentation is still horrific and inconsistent, but at least you are no longer trying to hide the open braces. I had to reindent it in order to make it half-way readable, so the line numbers will have changed.

    You are still ignoring warnings! Never ignore warnings!

    C:TEST>gcc -Wall legend02.c
    legend02.c: In function `main':
    legend02.c:28: warning: suggest parentheses around assignment used as truth valu
    e
    legend02.c:38: warning: double format, pointer arg (arg 4)
    legend02.c:38: warning: int format, pointer arg (arg 5)
    legend02.c:47: warning: suggest parentheses around assignment used as truth valu
    e
    legend02.c:93: warning: too many arguments for format
    legend02.c:7: warning: unused variable `counter'

    C:TEST>
    Line 27 and 28 (I added Line 27 to help you find Line 28 in your code):
    Code:
            case 2: 
                if(fp = NULL)
    You are setting fp to NULL. Since the result is zero, that if always evaluates to false. Always bear in mind that there is a difference between equality (==) and assignment (=).

    Line 38:
    Code:
                    fprintf(fp, "%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
    &credit_amt is not a floating-point variable, but rather an float pointer. Similarly, &tel_num is not an int, but rather an int pointer. Lose the ampers.

    Lines 46 and 47 (ditto about Line 46):
    Code:
            case 3: 
                if(fp = NULL)
    Same problem as with Line 28.

    Line 93:
    Code:
            printf("%-10s %-10s %-10s %-10s", "Customer", "Credited Amount", 
                    "Creditor's Balance", "Telephone #", "Address");
    You gave printf five values to print, but you told it that you would only give it four. That is what is meant by "too many arguments for format". You lied to the compiler. It is a very bad idea to lie to the compiler.
    Originally Posted by Henry Spencer
    If you lie to the compiler, it will get its revenge.
    Now, this mistake can go the other way too, where the format strings says to expect more argument you actually give it. If you give printf fewer arguments than you tell it, then it will go ahead a try to look for those missing arguments and it will display whatever random garbage it just happens to find. It's comical to watch beginners panic when their program starts spewing garbage.

    Always ensure that you give printf the exact same number of arguments as your format string tells it to expect.

    That you didn't use the variable, counter, should be no problem. Unless you are certain that you had used it, in which case you should double-check.

    Yet again, the warnings pointed you directly to the problems in your code. Why do you persist in ignoring warnings? Why do you persist in the sheer idiocy of running a program that is throwing warnings?

    Never ignore warnings! Will you never learn?

    Also, I would recommend that you test fp immediately after the fopen and act upon it then and there. I assume that you don't want to do anything if the file doesn't open.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,090
    Rep Power
    2222
    I prepared this yesterday:

    Properly formatting your code is extremely important. Any programmer reading your code, including you yourself, needs to be able to read it with ease and to understand it readily. That is why we indent code. That is why we use and maintain levels of indentation. Without proper and consistent indentation, code becomes much more difficult to read and very difficult, if not nearly impossible, to understand.

    Anyone you present your code to for review or for help, such as you are doing here, needs to be able to read and readily understand your code. Your instructor must be able to read and understand your code in order to grade it. In the real world, programmers do not work alone but rather in teams, so every member of that team must be able to read and understand each other's code. Much more important, every piece of code that is developed must be maintained. There is the concept of a software life cycle in which software is designed and deployed and then for a very extended period of time (years, even decades) it must be maintained. That means that as users report problems with the software (AKA "bugs"), somebody has to find the cause of those bugs and correct them. And as new features are needed, somebody has to add those new features. And as new software is developed based on existing software ("Our new product must function just like this other product, but with these major differences." -- yes, that happens a lot!), somebody will have to thoroughly understand that old software in order to figure out how to transform it into the new software. And those somebodies working hard during that maintenance phase of the software's life cycle will almost never be the same somebodies who had originally designed it. And even if it is the same somebody who had designed it, after a year (or even just after six months) none of us can recognize nor remember our own code.

    Every company that generates software publishes coding and formatting standards that all the programmers are required to use and to follow. Refusal to follow those standards can lead to disciplinary action or even dismissal. It is that important!

    But the most important person you need to be writing for, especially right now, is you. You need to understand your own code. You may not believe me, but it is true. We get many beginners posting here who get confused by their own code and cannot see how their own code is structured because they won't format it. They will use that K&R style because their book uses it (but their book uses it because the publisher wants to cram as many lines of code as possible on a single page; they don't care how readable it is) and they end up losing track of the open braces. We have seen that happen too many times! You need to understand your own code. You need to format it so that you can understand it.

    This Wikipedia article lists and describes various indenting styles: Indent style. Choose one and use it consistently.

    You were using the K&R style. It can work, but you absolutely must be consistent with your indenting. By hiding the open brace, the only thing than signals a new level of indentation is the actual indentation of the code. If you do not provide that required extra indentation, then the reader has to go looking for that hidden open brace, which he might not find (especially likely to happen when it's hidden in the middle of a line). If you refuse to be consistent and meticulous in your indenting, then stop using this style immediately!

    I very much prefer the Allman style which you have started to adopt here, since it brings the open brace out to the same level of indentation as the close brace, thus making it much more apparent. This style can better survive inconsistent indentation, though you must still always be consistent.

    Choose your style, unless there are coding standards that you are required to follow. Whatever style you end up using, always be consistent! Life will be so much better for it.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    11
    Rep Power
    0
    Code:
    case 3: if(fp == NULL)
    									{
    				           				printf("File could not be opened\n");
    									}
    									else
    									{
    					
    										printf("Enter Customer name and amount of money\n");
    										scanf("%s %f", cred_payingnm, &paid);
    
    					
    
    										fscanf(fp, "%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
    											if(strcmp(cred_payingnm, customer_name) == 0)
    											{
    												 creditor_bal= credit_amt - paid;
    											}
    											else
    											{
    												 printf("Creditor name does not exist\n");
    											}
    
    										remaining_cred = total_credit - paid;
    										total_sales = total_sales + paid;
    									}
    								break;
    
    								default: printf("Wrong number chosen\n");
    break;



    Thanks for all your help guys. But this part of the code doesn't execute and it's a crucial part of what I want to do. It is suppose ask for a customer name and amount of money that is being paid and then read from the file the Creditors that were inputted before hand, go through each Creditor and subtract an amount of money that is inputted from that Creditor's credit amount.
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,090
    Rep Power
    2222
    Crappy formatting! Do you lie awake at night dreaming up new ways to screw up your code's formatting?

    Code:
                case 3: 
                    if(fp == NULL)
                    {
                        printf("File could not be opened\n");
                    }
                    else
                    {
                        printf("Enter Customer name and amount of money\n");
                        scanf("%s %f", cred_payingnm, &paid);
    
                        fscanf(fp, "%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
                        if(strcmp(cred_payingnm, customer_name) == 0)
                        {
                            creditor_bal= credit_amt - paid;
                        }
                        else
                        {
                            printf("Creditor name does not exist\n");
                        }
    
                        remaining_cred = total_credit - paid;
                        total_sales = total_sales + paid;
                    }
                    break;
    
                default: 
                    printf("Wrong number chosen\n");
                    break;
    What do you mean by "this part of the code doesn't execute"? How do you know that? What does it do as opposed to what you expect it to do? IOW, what are the symptoms? Or do you always expect everybody to be able to read your mind?

    Does the file open or doesn't it?

    Also, what warnings is the compiler throwing at you? You do have warnings turned on and up, don't you? If not, then you are a complete idiot. That you are still ignoring those warnings does not speak highly of you either.

    Look at that list of warnings that I provided you! Look at these particular warnings:
    legend02.c:38: warning: double format, pointer arg (arg 4)
    legend02.c:38: warning: int format, pointer arg (arg 5)


    Now look again at my commentary on those warnings:
    Originally Posted by DWise1_aol
    Line 38:

    Code:
                    fprintf(fp, "%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
    &credit_amt is not a floating-point variable, but rather an float pointer. Similarly, &tel_num is not an int, but rather an int pointer. Lose the ampers.
    Now let's look at this line of code that you just posted:
    Code:
                        fscanf(fp, "%s %f %d %s", customer_name, &credit_amt, &tel_num, address);
    Is that the part that doesn't work? Why hasn't that been corrected? Why did you ignore what I told you about that? Lose the ampers! And why do you still ignore the warnings that your compiler is throwing at you about that line?

    Never ignore warnings!

IMN logo majestic logo threadwatch logo seochat tools logo