Error in my code...not sure what i did wrong

This is a discussion on Error in my code...not sure what i did wrong within the C Programming forums, part of the General Programming Boards category; Ok, so this is just a stupid little program i wrote for class...it will not compile here it is. Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    18

    Error in my code...not sure what i did wrong

    Ok, so this is just a stupid little program i wrote for class...it will not compile here it is.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
        int gallons;
        int gallons1;
        int gallons2;
        double upb;
        int charge = 35;
        int latefee = 2;
        double total = 0;
    
        
        
        
        
        
        
        
        while ( gallons != -1 ) {
        
        printf( "THANK YOU FOR CHOOSING PLEASANT WATER AUTHORITY\n\n" );
        printf( "Enter the current quarters readings:\n" );
        printf( "                    " ); 
        scanf( "%d", &gallons );
        printf("Enter last quarters readings:\n" );
        printf( "                    " );
        scanf( "%d", &gallons1 );
        printf( "Enter last years readings, this quarter:\n" );
        printf( "                    " );
        scanf( "%d", &gallons2 );
        printf( "Enter any unpaid balance from last quarter:\n" );
        printf( "                    " );
        scanf( "%.2f", &upb );
        
        if ( upb = 0 && gallons <= gallons2 * .95 ) {
             total = gallons * (1.10 / 1000) + latefee + charge;
             printf( "Your total due this quarter is:\t %.2f\n\n", total );
             printf( "Congratulations, you have been environmentally green this quarter!\n\n" );
    }
             
             
        if ( gallons > gallons2 * .95 && upb > 0 ) { 
                 total = gallons * (2.20 / 1000) + charge + upb + latefee;
    			 printf( "You have an unpaid balance of %.2f\n\n", upb );
                 printf( "Your total due this quarter is:\t %.2f\n\n", total );
                 printf( "You were not environmentally green this quarter.\n\n");
    }
    
    
        if ( gallons <= gallons2 * .95 && upb > 0 ) {
                 printf( "You have an unpaid balance of %.2f\n\n", upb );
    			 printf( "Congratulations, you have been environmentally green this quarter!\n\n" );
                 total = gallons * (1.10 / 1000) + latefee + upb + charge;
                 printf( "Your total due this quarter is:\t %.2f\n\n", total );
    }
    
    
        //here is the compilation error, the 'if' statement
             if ( gallons > gallons2 * .95 && upb = 0 ) {
                 total = gallons * (2.20 / 1000) + charge + upb;
    	printf( "Your total due this quarter is:\t %.2f\n\n", total );
    	printf( "You were not environmentally green this quarter.\n\n" );
    }
    
    }
         system("PAUSE");
         return 0;
         
    }

    any help would be appreciated. thank you

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Excuse me, i got it to compile and run, however...there is no output. it just instantly loops back up after the final input is put in and restarts.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What error messages is the compiler giving you?
    Most times it will tell you exactly what's wrong...

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    So now, i am trying to figure out what is wrong with my loop. i will post the new code and it will produce strange results.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
        int gallons;
        int gallons1;
        int gallons2;
        double upb;
        int charge = 35;
        int latefee = 2;
        double total = 0;
    
        
        
        
        
        
        
        
        while ( gallons != -1 ) {
        
        printf( "THANK YOU FOR CHOOSING PLEASANT WATER AUTHORITY\n\n" );
        printf( "Enter the current quarters readings:\n" );
        printf( "                    " ); 
        scanf( "%d", &gallons );
        printf("Enter last quarters readings:\n" );
        printf( "                    " );
        scanf( "%d", &gallons1 );
        printf( "Enter last years readings, this quarter:\n" );
        printf( "                    " );
        scanf( "%d", &gallons2 );
        printf( "Enter any unpaid balance from last quarter:\n" );
        printf( "                    " );
        scanf( "%.2f", &upb );
        
        if ( upb = 0 && gallons <= gallons2 * .95 ) {
             total = gallons * (1.10 / 1000) + latefee + charge;
             printf( "Your total due this quarter is:\t %.2f\n\n", total );
             printf( "Congratulations, you have been environmentally green this quarter!\n\n" );
    }
             
             
        if ( gallons > gallons2 * .95 && upb > 0 ) { 
                 total = gallons * (2.20 / 1000) + charge + upb + latefee;
    			 printf( "You have an unpaid balance of %.2f\n\n", upb );
                 printf( "Your total due this quarter is:\t %.2f\n\n", total );
                 printf( "You were not environmentally green this quarter.\n\n");
    }
    
    
        if ( gallons <= gallons2 * .95 && upb > 0 ) {
                 printf( "You have an unpaid balance of %.2f\n\n", upb );
    			 printf( "Congratulations, you have been environmentally green this quarter!\n\n" );
                 total = gallons * (1.10 / 1000) + latefee + upb + charge;
                 printf( "Your total due this quarter is:\t %.2f\n\n", total );
    }
    
    
                 if ( upb = 0 &&  gallons > gallons2 * .95  ) {
                 total = gallons * (2.20 / 1000) + charge + upb;
    	printf( "Your total due this quarter is:\t %.2f\n\n", total );
    	printf( "You were not environmentally green this quarter.\n\n" );
    }
    
    }
         system("PAUSE");
         return 0;
         
    }
    here it is

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I do hope you don't think we're sitting here compiling your code...

    Describe what you expect and what it's doing....
    What should it produce?
    What does it produce?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Well, it produces almost everything i need. Except for after the final input of unpaid balance it doesnt display any of the 'if' statements and it just loops back up to "THANK YOU FOR CHOOSING PLEASANT WATER AUTHORITY"

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
        if ( upb = 0 && gallons <= gallons2 * .95 ) {
    Edit: for ignorance, = is assignment operator, to test for equality, ==
    Last edited by Bayint Naung; 11-27-2010 at 08:59 PM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Bayint, I've reviewed it now, and I see not problems. I'm quite sure there is something wrong with that statement otherwise you would not have posted that line.

    The logic here seems to me that -- if the unpaid balance is 0, and this quarters gallons is less than 95% of last years, then display the appropriate results.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Well, that is suppose to be the logic, however, is that not exactly what I have written?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This part of your code (in at least two places), is incorrect:
    Code:
    if (upb = 0... etc.
    The single = should be ==, as was pointed out previously.

    When a program doesn't work correctly, we can assume quite comfortably, that the logic atm, is incorrect. Arguing that it is correct is --- illogical, surely.

    After this line of code:
    Code:
    scanf( "%d", &gallons );
    It would be good to add something to break out of the loop, if the amount entered was -1. Currently, the rest of the loop executes anyway, which makes no sense.

    Code:
    if(gallons == -1)
      break;
    Are you printing up the info on how the user should break out of the loop, so the user knows to enter -1 to quit?
    Last edited by Adak; 11-27-2010 at 09:35 PM.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Well, my teacher said it doesnt matter, she told us to use -1 to exit, and because she has not taught us better methods because we have not gotten that far yet in class, that there will be no need to add the line directing the user to enter the sentinel value. and yes, I suppose that would be a good idea O.o thank you. Im still trying to grasp some of the syntax and logic in programming, i appreciate all critiques.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Have you changed the single = sign to the correct == inside the if statements, yet?

    She has you using if statements in your code. You could use an if statement to stop the "wrong" loop code from executing, if the user entered -1:
    Code:
    if(gallons != -1) {
      //all the rest of the loop processing goes in here
      //including ALL your other if statements that are inside the while loop
    }
    Last edited by Adak; 11-27-2010 at 09:59 PM.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Yes, i have changed to == in both if statements. Currently I am working on formatting the ouput. I want the cursor to be right aligned when the user enters the initial values. working on that. also, one other thing i am working on is that when the user types in the upb ( unpaid balance), the out put is always 0 regardless of what value was actually typed in. finally, when the loop comes back after the program runs, and it restarts and asks the user to enter gallons, it skips that step and is automatically set on last quarters gallons to enter. those are 3 problems im currently working on, any help is appreciated.

  14. #14
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
        scanf( "%.2f", &upb );
    Just use scanf("%f",&upb);

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    alright, i took away the ".2" did not seem to have any effect, however I am curious as to why the .2 should have been taken away, Does the compiler automatically recognize a double having two decimal places? I am simply trying to learn, sorry if I am so full of skepticism and questions.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 12:52 PM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 01:51 AM

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