Thread: I need some help debugging homework.

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    I need some help debugging homework.

    Hi all,
    I need help debugging my homework. It is due tonight and I have been working on it for 2 days. I can't figure out how to
    1. reduce the float variables to dollar format example: $5.00 instead of 5.000000
    2. Am I not declaring the functions right? I have 4 functions and I am trying to call them from the main function.
    3. I am using MS Visual C++. I have also tried Miracle C and I can't seem to get it to work with either.
    4. I am not sure if I have included all the required files.


    Here is the following errors:

    C:\Documents and Settings\Administrator\Desktop\exam3.c(24) : warning C4013: 'Printf' undefined; assuming extern returning int
    C:\Documents and Settings\Administrator\Desktop\exam3.c(52) : warning C4244: 'return' : conversion from 'float ' to 'int ', possible loss of data
    C:\Documents and Settings\Administrator\Desktop\exam3.c(61) : error C2143: syntax error : missing ';' before 'type'
    C:\Documents and Settings\Administrator\Desktop\exam3.c(62) : warning C4047: '>=' : 'int (__cdecl *)(float ,float )' differs in levels of indirection from 'int '
    C:\Documents and Settings\Administrator\Desktop\exam3.c(65) : warning C4550: expression evaluates to a function which is missing an argument list
    C:\Documents and Settings\Administrator\Desktop\exam3.c(65) : warning C4047: 'return' : 'int ' differs in levels of indirection from 'int (__cdecl *)(float ,float )'
    C:\Documents and Settings\Administrator\Desktop\exam3.c(92) : warning C4244: '=' : conversion from 'int ' to 'float ', possible loss of data
    C:\Documents and Settings\Administrator\Desktop\exam3.c(93) : warning C4244: '=' : conversion from 'int ' to 'float ', possible loss of data
    C:\Documents and Settings\Administrator\Desktop\exam3.c(95) : error C2063: 'total' : not a function
    C:\Documents and Settings\Administrator\Desktop\exam3.c(97) : error C2115: 'function' : incompatible types
    C:\Documents and Settings\Administrator\Desktop\exam3.c(97) : warning C4024: 'show' : different types for formal and actual parameter 1
    C:\Documents and Settings\Administrator\Desktop\exam3.c(97) : error C2115: 'function' : incompatible types
    C:\Documents and Settings\Administrator\Desktop\exam3.c(97) : warning C4024: 'show' : different types for formal and actual parameter 2
    C:\Documents and Settings\Administrator\Desktop\exam3.c(97) : error C2115: 'function' : incompatible types
    C:\Documents and Settings\Administrator\Desktop\exam3.c(97) : warning C4024: 'show' : different types for formal and actual parameter 3
    Error executing cl.exe.

    exam3.exe - 5 error(s), 10 warning(s)
    Any help or suggestions would be GREAATTTLLLYY APPRECIATED.
    Thanks in advance.

    /* ********* Compiler: MS Visual C++************** */

    PHP Code:
    #include <stdio.h> 


    getfloat(float minimumint calltype){ 



                
    float amount

                if (
    calltype == 1){                         

                             

                            
    printf("\nPlease enter the Amount for Raffle Ticket"); 

                            
    scanf("%f",amount); 

                             

                            while (
    amount minimum){ 

                                        
    Printf("\nInvalid Amount! Please Enter Amount greater than $%1f"minimum); 

                                        
    scanf("%f",amount); 

                            } 

                } 

                if (
    calltype == 2){                         

                             

                            
    printf("\nPlease enter the Number of Raffle Ticket"); 

                            
    scanf("%f",amount); 

                             

                            while (
    amount minimum){ 

                                        
    Printf("\nInvalid Number! Please Enter Number greater than %1f"minimum); 

                                        
    scanf("%f",amount); 

                            } 

                } 

                return 
    amount


         
         

    total(float numoftixfloat amount) { 
        
    int max
        
    max 500
            
    float total numoftix*amount
                if(
    total>=max)    { 
                    return 
    max
                        } else { 
                    return 
    total

                    } 

        } 


        
    show(float numoftixfloat amountfloat total) { 

            
    printf("\nYou purchased %1f tickets at %1f each, for a total of $%1f\n"numoftixamounttotal); 
         
        }     




    main() { 

            
    float amountnumoftixtotal

                
    amount getfloat(5.00,1); 
                
    numoftix getfloat(0,2); 

            
    total(&numoftix,&amount); 

            
    show(&numoftix,&amount,&total); 
        } 


    -------------------------------------------------------------------------------- 

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Have you tried reading the warnings and errors that you're getting? They seem to be pretty informative. One of your biggest problems is not knowing when to use & in a function call and when not to as well as relying on C89's implicit int in functions. This works. Compare with what you have and try to figure out what I did differently:
    Code:
    #include <stdio.h> 
    
    float getfloat(float minimum, int calltype){ 
      float amount = 0.0f; 
    
      if (calltype == 1){                         
        printf("\nPlease enter the Amount for Raffle Ticket"); 
        scanf("%f",&amount); 
        while (amount < minimum){ 
          printf("\nInvalid Amount! Please Enter Amount greater than $%1f", minimum); 
          scanf("%f",amount); 
        } 
      } 
      if (calltype == 2){                         
        printf("\nPlease enter the Number of Raffle Ticket"); 
        scanf("%f",&amount); 
        while (amount < minimum){ 
          printf("\nInvalid Number! Please Enter Number greater than %1f", minimum); 
          scanf("%f",amount); 
        } 
      } 
    
      return amount; 
    }
    
    float total(float numoftix, float amount) { 
      float max = 500.0f;
      float total = numoftix*amount; 
      if(total>=max)    { 
        return max; 
      } else { 
        return total; 
      } 
    } 
    
    show(float numoftix, float amount, float total) { 
      printf("\nYou purchased %.0f tickets at %.0f each, for a total of $%.2f\n", numoftix, amount, total); 
    }     
    
    int main(void) { 
    
      float amount, numoftix, ftotal; 
    
      amount = getfloat(5.00,1); 
      numoftix = getfloat(0,2); 
      ftotal = total(numoftix,amount); 
      show(numoftix,amount, ftotal);
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    printf is lowercase not Printf
    for the second warning you didn't tell what your function is returning so it assumed int you need to say float functionname main needs to be set as an int
    Code:
    int main(void)
    scanf's are wrong
    Code:
    scanf("%f",& amount);
    you don't need to pass memory addresses for numbers only for strings in functions. Here is the corrected code(Note: Didn't test just looked at syntax(Your total function returns something but nothing picks it up. Plus to show only the last decimal place you use %.1f not just %1f but I think you want %.2f
    Code:
    #include <stdio.h>
    
    float
    getfloat (float minimum, int calltype)
    {
      float amount;
      if (calltype == 1)
        {
          printf ("\nPlease enter the Amount for Raffle Ticket");
          scanf ("%f", &amount);
          while (amount < minimum)
            {
              printf ("\nInvalid Amount! Please Enter Amount greater than $%.1f",
                      minimum);
              scanf ("%f", &amount);
            }
        }
      if (calltype == 2)
        {
          printf ("\nPlease enter the Number of Raffle Ticket");
          scanf ("%f", &amount);
          while (amount < minimum)
            {
              printf ("\nInvalid Number! Please Enter Number greater than %.1f",
                      minimum);
              scanf ("%f", &amount);
            }
        }
      return amount;
    }
    
    float total (float numoftix, float amount){
      int max;
      max = 500;
      float total = numoftix * amount;
      if (total >= max){
          return max;
        }
      else{
          return total;
        }
    }
    
    void
    show (float numoftix, float amount, float total){
      printf ("\nYou purchased %.1f tickets at %.1f each, for a total of $%.1f\n",
              numoftix, amount, total);
    }
    
    int
    main (void)
    {
      float amount, numoftix, total;
      amount = getfloat (5.00, 1);
      numoftix = getfloat (0, 2);
      total (numoftix, amount);
      show (numoftix, amount, total);
    }
    Just read the erros and see what is wrong.
    [edit]beat me to it Prelude [/edit]
    Last edited by linuxdude; 03-14-2004 at 05:03 PM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    AWESOME

    Thanks Prelude and Linuxdude, I used Prelude's suggestions and it runs without a problem. I really appreciate it. I wanted to ask about another problem I am having with a do while loop. The loop runs fine except it doesn't prompt to continue. It just skips to the beginning of the loop again. I have tried changing the while condition to (cont == 'y') and (cont != 'y') but nothing works. Any suggestions will be greatly appreciated.

    Code:
    
    void main(void) { 
      float amount, numoftix, ftotal;
      char cont;
    
       do{
         amount = getfloat(5.00,1); 
         numoftix = getfloat(0,2); 
         ftotal = total(numoftix,amount); 
         show(numoftix,amount,ftotal);
    
    	 printf("Do you wish to continue (y/n): \n");
          scanf("%c",&cont); } while (cont != 'y'); getch();
        
    }
    Thanks again.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Talking

    1) main returns an int. Always. Always. One more time, always.
    2) You probably have some input in your buffer still, which overrides (or rather, is used in) the scanf call. Modify it to:
    Code:
        while( fgetc( stdin ) != '\n' );
        /* now do a scanf */
        printf("Continue? y/n: ");
        scanf( "%c", &choice );
    } while( toupper( choice ) == 'Y' ); /* include ctype.h if you're using toupper */
    [edit]Don't drink and type.[/edit]

    Quzah.
    Last edited by quzah; 03-15-2004 at 12:43 AM.
    Hope is the first step on the road to disappointment.

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Code:
    void main(void) {   /*main SHOULD return an int  to determine clean\successful execution*/ 
      float amount, numoftix, ftotal;
      char cont;
    
       do{
         amount = getfloat(5.00,1); 
         numoftix = getfloat(0,2); 
         ftotal = total(numoftix,amount); 
         show(numoftix,amount,ftotal);
    
    	 printf("Do you wish to continue (y/n): \n");
          scanf("%c",&cont); } while (cont != 'y'); getch();
        
    }/* where is the while(<condition>);? */
    just thought of showing those two mistakes , didn't look at your whole program nor tested it to be honest.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Brain Cell
    /* where is the while(<condition>);? */
    Code:
      float amount, numoftix, ftotal;
      char cont;
    
       do{
         amount = getfloat(5.00,1); 
         numoftix = getfloat(0,2); 
         ftotal = total(numoftix,amount); 
         show(numoftix,amount,ftotal);
    
    	 printf("Do you wish to continue (y/n): \n");
          scanf("%c",&cont); } while (cont != 'y'); getch();
        
    }
    Chalk it up to bad indenting and formatting. I didn't see it either at first glance.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    sorry , not used to reading C programs that uses multiple functions in one line
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  2. gdb no debugging symbols found
    By Laserve in forum Linux Programming
    Replies: 8
    Last Post: 09-17-2006, 08:56 AM
  3. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM