Could somebody please help me with this C program

This is a discussion on Could somebody please help me with this C program within the C Programming forums, part of the General Programming Boards category; I've been working on this program for school for the last little while and I don't really have anybody around ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    7

    Exclamation Could somebody please help me with this C program

    I've been working on this program for school for the last little while and I don't really have anybody around who could help me, so I was wondering if I could get some help. My teacher isn't the best and the text book is terrible so I don't really have much to go by in the way of debugging it, anyways, any help would be greatly appreciated. Here is my code so far:

    Code:
    #include <stdio.h>
    
    
    float wd(float*, char*[], float*);
    float dep(float*, char*[], float*);
    float bpm(float*, char*[], float*);
    void PrintResults(float, float, float, float, int, int, int, char[], char[], char[], float, float, float);
    void main(void)
    {
    float balance, wd1, dep1, bpm1, wdbal, depbal, bpmbal;
    char junk, cont, wddate[15], depdate[15], bpmdate[15], trans1[5], trans2[5], trans3[5];
    int trans;
    
    printf("Please type the last balance as float: \n");
    scanf("%f", &balance);
    scanf("%c", &junk);
    printf("Transactions \n");
    printf("Please type Y to continue or N to stop: \n");
    scanf("%c", &cont);
    while (cont != 'N')
    {
    printf("Please type 1 for Withdrawl, 2 for deposit, or 3 for Bill Payment \n");
    scanf("%d", &trans);
    if (trans == 1)
    {
    wdbal = wd(&balance, &wddate, &wd1);
                      trans1 = "wd";
    }
    else if (trans == 2)
    {
    depbal = dep(&balance, &depdate, &dep1);
                            trans2 = "dep";
    }
    else if (trans == 3)
    bpmbal = bpm(&balance, &bpmdate, &bpm1); 
                            trans3 = "bpm";
    }
    else
    printf("Unknown transaction type \n");
     
    scanf("%c", &junk);
    printf("Please type Y to continue or N to stop: \n");
    scanf("%c", &cont);
    }
    PrintResults(wdbal, depbal, bpmbal, balance, trans1, trans2, trans3, wddate, depdate, bpmdate, wd1, dep1, bpm1)
    }
    float wd(float *balance, char *wddate[], float *wd1)
    {
    printf("Please enter the date of withdrawl \n");
    scanf("%s", &wddate);
    float wdbal;
    printf("Please enter the amount to withdrawl: \n");
    scanf("%f", &wd1);
    wdbal = balance - wd1;
    balance = balance - wd1;
    return wdbal;
    }
    float dep(float *balance, char *depdate[], float *dep1)
    {
    printf("Please enter the date of deposit \n");
    scanf("%s", &depdate);
    float depbal;
    printf("Please enter the amount to deposit: \n");
    scanf("%f", &dep1);
    depbal = balance + dep1;
    balance = balance + dep1;
    return depbal;
    }
    float bpm(float *balance, char *bpmdate[],float *bpm1)
    {
    printf("Please enter the date of bill payment \n");
    scanf("%s", &depdate);
    float bpm1, bpmbal;
    printf("Please enter the amount to be paid to bills: \n");
    scanf("%f", &bpm1);
    depbal = balance - bpm1;
    balance = balance bpm1;
    return bpmbal;
    }
    void PrintResults(float wdbal, float depbal,float bpmbal,float balance,char trans1[],char trans2[],char trans3[],char wddate[],char depdate[],char bpmdate[], float wd1, float dep1, float bpm1)
    {
    printf("Date            Type            Amount          Balance");
    printf("%s              %s                $%.2f           $%.2f \n", wddate, trans1, wd1, wdbal);
    printf("%s              %s                 $%.2f           $%.2f \n", depdate, trans2, dep1, depbal);
    printf("%s                %s               $%.2f           $%.2f \n", bpmdate, trans3, bpm1, bpmbal);
    printf("\n");
    printf("Current balance: $%.2f", balance);
    }
    the program is supposed to first take the balance in a bank account, then ask if there are transactions, if yes it goes into the loop and asks what type of transactions. When a type is entered the specific funtion is supposed to take place and then the balance's are returned. After there are no more transactions the print function is to take place. However, I have a lot of errors and we were never really taught as to what they meant. I will list the errors:

    "Text1.c", line 26: warning: argument #2 is incompatible with prototype:
    prototype: pointer to pointer to char : "Text1.c", line 4
    argument : pointer to array[15] of char
    "Text1.c", line 27: left operand must be modifiable lvalue: op "="
    "Text1.c", line 31: warning: argument #2 is incompatible with prototype:
    prototype: pointer to pointer to char : "Text1.c", line 5
    argument : pointer to array[15] of char
    "Text1.c", line 32: left operand must be modifiable lvalue: op "="
    "Text1.c", line 35: warning: argument #2 is incompatible with prototype:
    prototype: pointer to pointer to char : "Text1.c", line 6
    argument : pointer to array[15] of char
    "Text1.c", line 36: left operand must be modifiable lvalue: op "="
    "Text1.c", line 38: syntax error before or at: else
    "Text1.c", line 46: warning: old-style declaration or incorrect type for: PrintResults
    "Text1.c", line 46: identifier redeclared: PrintResults
    current : function() returning int
    previous: function(float, float, float, float, int, int, int, pointer to char, pointer to char, pointer to char, float, float, float) returning void : "Text1.c", line 7
    "Text1.c", line 46: syntax error before or at: }
    "Text1.c", line 55: warning: improper pointer/integer combination: op "="
    "Text1.c", line 65: operands have incompatible types:
    pointer to float "+" pointer to float
    "Text1.c", line 65: assignment type mismatch:
    float "=" pointer to float
    "Text1.c", line 66: operands have incompatible types:
    pointer to float "+" pointer to float
    "Text1.c", line 72: undefined symbol: depdate
    "Text1.c", line 73: declaration hides parameter: bpm1
    declaration: float
    parameter : pointer to float
    "Text1.c", line 76: undefined symbol: depbal
    "Text1.c", line 76: operands have incompatible types:
    pointer to float "-" float
    "Text1.c", line 76: warning: improper pointer/integer combination: op "="
    "Text1.c", line 77: syntax error before or at: bpm1
    "Text1.c", line 81: identifier redefined: PrintResults
    current : function(float, float, float, float, pointer to char, pointer to char, pointer to char, pointer to char, pointer to char, pointer to char, float, float, float) returning void
    previous: function(float, float, float, float, int, int, int, pointer to char, pointer to char, pointer to char, float, float, float) returning int : "Text1.c", line 46
    cc: acomp failed for Text1.c



    anyways, any help would be great, thanks.

  2. #2
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Hum.. some hardcore type messing.. But that's no prob
    I think that your assignment should be correcting all these errors, because they're plenty. I've already been correcting your code, but I think you should 1st effort a little more, to try to understand what you're doing. The compiler error/warnings say what's wrong. read carefully.
    Anyway here a little snipet:
    Indent well your code.
    Try to match { and }. Look at the functions prototypes, and check if the declaration is the same. Also take a good look when de-referencing pointers. And you're using variables in functions that aren't declared. They're declared in other functions: bad copy/paste.
    Last edited by xErath; 11-24-2004 at 07:58 PM.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    7
    Sorry about the look of my previous code, but my friend and I actually are working on a new version, this is what we have so far

    Code:
    #include<stdio.h>
    
    void printr(float,float,float,float, float, float, float, char, char, char);
    void main(void)
    {
    
    float b1,b2,b3, bal, tran, with, dep, bill, amount;
    char junk, ans;
    char date[15],withdate, depdate, billdate;
    printf("Please type the last balance as float\n");
    scanf("%f", &bal);
    scanf("%c",&junk);
    printf("Transactions\n");
    printf("Please type Y to continue or N if you want to stop\n");
    scanf("%c", &ans);
    while(ans=='Y')
    	{	
    		printf("Please type the date as one string\n");
    		scanf("%s", &date);	    		
    		printf("Next type transtype as integer and amount as float\n");
    		scanf("%d%f", &tran, &amount);
    		
    			if(tran=1)
    				{
    					bal=bal-amount;
    					with=amount;
    					withdate=date;
    				}
    			else if(tran=2)
    				{
    					bal=bal+amount;
    					dep=amount;
    					depdate=date;	 	 	 	 
    				}
    			else
    				{
    					billdate=date;
    					bal-=amount;
    					bill=amount;
    				}
    scanf("%c",&junk);
    printf("Please type Y to continue or N if you want to stop\n");
    scanf("%c", &ans);	
    	}
    b3=bal;
    b2=b1+dep;
    b1=6000-with;
    
    printr(with, dep, bill, bal, b3, b2, b1, withdate[15], depdate[15], billdate[15]); 
    }
    void printr(float with, float dep, float bill, float bal, float b3, float b2, float b1, char withdate[], char depdate[], char billdate[])
    {
    printf("Date \t type \t amount \t balance\n");
    printf("%c \t wd \t %f \t %f\n", withdate, with, b1);
    printf("%c \t wd \t %f \t %f\n", depdate, dep, b2);
    printf("%c \t wd \t %f \t %f\n", billdate, bill, b3);
    }
    but once again I get some of the same errors i got last time and i really don't see what they are talking about. These are the new errors, a little less than before

    "1a22.c", line 27: left operand must be modifiable lvalue: op "="
    "1a22.c", line 33: left operand must be modifiable lvalue: op "="
    "1a22.c", line 37: left operand must be modifiable lvalue: op "="
    "1a22.c", line 49: warning: improper pointer/integer combination: arg #8
    "1a22.c", line 49: warning: improper pointer/integer combination: arg #9
    "1a22.c", line 49: warning: improper pointer/integer combination: arg #10
    "1a22.c", line 52: identifier redeclared: printr
    current : function(float, float, float, float, float, float, float, pointer to char, pointer to char, pointer to char) returning void
    previous: function(float, float, float, float, float, float, float, char, char, char) returning void : "1a22.c", line 3
    cc: acomp failed for 1a22.c

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Code:
    scanf("%d%f", &tran, &amount);
    		
    			if(tran=1)
    				{
    					bal=bal-amount;
    					with=amount;
    					withdate=date;
    				}
    			else if(tran=2)
    				{
    					bal=bal+amount;
    					dep=amount;
    					depdate=date;	 	 	 	 
    				}
    			else
    				{
    					billdate=date;
    					bal-=amount;
    					bill=amount;
    				}
    You if look like they should be testing for equality not assignment (== vs. =)

    if (tran == 1)
    {

    }
    else if (tran == 2)
    {

    }

    That looks like one problem...
    Mr. C: Author and Instructor

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Baahhhhhhh... I'm looking at your original code:
    See these functions??
    Code:
    float wd(float*, char*[], float*);
    float dep(float*, char*[], float*);
    float bpm(float*, char*[], float*);
    Chande the prototypes to
    Code:
    float wd(float*, char[], float*);
    float dep(float*, char[], float*);
    float bpm(float*, char[], float*);
    Then when calling them
    Code:
    wdbal = wd(&balance, wddate, &wd1);//wdbal = wd(&balance, &wddate, &wd1);
    depbal = dep(&balance, depdate, &dep1);//depbal = dep(&balance, &depdate, &dep1);
    bpmbal = bpm(&balance, bpmdate, &bpm1);//bpmbal = bpm(&balance, &bpmdate, &bpm1);
    Your code is the one commented. The fix the one before.
    Thsi function definition is different from the prototype
    Code:
    void PrintResults(float, float, float, float, int, int, int, char[], char[], char[], float, float, float);
    ....
    void PrintResults(float wdbal, float depbal,float bpmbal,float balance,char trans1[],
    				  char trans2[],char trans3[],char wddate[],char depdate[],char bpmdate[],
    				  float wd1, float dep1, float bpm1)
    {.............}
    Change this variable type from arrays to pointers, because array are l-values
    Code:
    char trans1[5], trans2[5], trans3[5];
    char *trans1, *trans2[5], *trans3;
    You missed this bracket
    Code:
    else if (trans == 3){
    And a comma here
    Code:
    	PrintResults(wdbal, depbal, bpmbal, balance, trans1, trans2, trans3, wddate, depdate, bpmdate, wd1, dep1, bpm1);
    Don't declare variable in the middle of code. That's C99 standard, but I doubt that's your intention. move these to the beginning of the functions where they're declared
    Code:
    	float  bpmbal;
    	float depbal;
    	float wdbal;
    And don't re-declare bpm1 in bpm function. All these were wrong. You have to dereference pointers.
    Code:
    	wdbal = *balance - *wd1;
    	*balance = *balance - *wd1;
    ....
    	depbal = *balance + *dep1;
    	*balance = *balance + *dep1;
    ...
    	depbal = *balance - *bpm1;
    	*balance = *balance - *bpm1;
    These variable doens't exist in function bpm
    Code:
    	scanf("%s", &depdate);
    ...
         depbal=...;
    Should be bpmdate, and bpmbal

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    7
    Ok, i got this one to compile now with no errors, but this is what is supposed to be printed:

    Date type amount balance



    12,Mar,2003 wd 500.00 5500.00

    25, Mar,2003 dep 100.70 5600.70

    27, Mar,2003 bpm 57.50 5543.20

    but this is what we get

    Date type amount balance

    <null> wd 0.00 6000
    <null> dep 0.00

    then the program crashes, this is the code we have now:

    Code:
    #include<stdio.h>
    
    void printr(float,float,float,float, float, float, float, char[], char[], char[]);
    void main(void)
    {
    
    float b1,b2,b3, bal, tran, with, dep, bill, amount;
    char junk, ans;
    char date[15],withdate, depdate, billdate;
    printf("Please type the last balance as float\n");
    scanf("%f", &bal);
    scanf("%c",&junk);
    printf("Transactions\n");
    printf("Please type Y to continue or N if you want to stop\n");
    scanf("%c", &ans);
    while(ans=='Y')
    	{	
    		printf("Please type the date as one string\n");
    		scanf("%s", &date);	    		
    		printf("Next type transtype as integer and amount as float\n");
    		scanf("%d%f", &tran, &amount);
    		
    			if(tran==1)
    				{
    					bal=bal-amount;
    					with=amount;
    					withdate=date;
    				}
    			else if(tran==2)
    				{
    					bal=bal+amount;
    					dep=amount;
    					depdate=date;	 	 	 	 
    				}
    			else
    				{
    					billdate=date;
    					bal-=amount;
    					bill=amount;
    				}
    scanf("%c",&junk);
    printf("Please type Y to continue or N if you want to stop\n");
    scanf("%c", &ans);	
    	}
    b3=bal;
    b2=b1+dep;
    b1=6000-with;
    
    printr(with, dep, bill, bal, b3, b2, b1, withdate, depdate, billdate); 
    }
    void printr(float with, float dep, float bill, float bal, float b3, float b2, float b1, char withdate[], char depdate[], char billdate[])
    {
    printf("Date	type	amount		balance\n");
    printf("%s  	wd  	%.2f		 %.2f\n", withdate, with, b1);
    printf("%s	dep 	%.2f  		 %.2f\n", depdate, dep, b2);
    printf("%s	bpm 	%.2f		 %.2f\n", billdate, bill, b3);
    }

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    1. Don't use tabs in your source code - the board indents too agressively
    4 spaces per indent ought to be enough

    2. void main(void)
    wrong - main returns an int

    3. char date[15],withdate, depdate, billdate;
    withdate is neither an array, nor is it initialised.
    So doing
    printr(with, dep, bill, bal, b3, b2, b1, withdate, depdate, billdate);
    passes all kinds of garbage to the function.
    Does your compiler even complain about that?
    Because it should!!!

    > void printr(float with, float dep, float bill, float bal, float b3, float b2, float b1, char withdate[], char depdate[], char billdate[])
    Use some newlines
    Code:
    void printr(float with, float dep, float bill, float bal,
          float b3, float b2, float b1,
          char withdate[], char depdate[], char billdate[])

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21