Simple program that wont execute

This is a discussion on Simple program that wont execute within the C Programming forums, part of the General Programming Boards category; This is the code: Code: #include <stdio.h> int main() { int miles; int gallons; int mileage; printf("Enter the gallons used: ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    68

    Simple program that wont execute

    This is the code:

    Code:
     #include <stdio.h>
    
    int main()
    {
    
    	int miles;		
    	int gallons;	
    	int mileage;
    
    	printf("Enter the gallons used:  ");	
    	scanf("%d", &gallons);
    
    	printf("Enter the miles driven:  ");
    	scanf("%d", &miles);
    	
    	mileage = miles / gallons; /* Calculates the tank's mileage */
    	scanf("%d", &mileage);
    
    	printf("The miles per gallon for this tank was " , &mileage);
    
    	return 0;
    	}
    I cant for the life of me figure out why this wont execute, any help that anyone can offer is appreciated.
    Thank you very much.

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Learn how printf works and ponder the purpose of scanf("%d", &mileage);.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You are trying to pass the address of the variable mileage:

    Code:
    printf("The miles per gallon for this tank was " , &mileage);
    That is not what you want. Pass the variable as an argument, without the & operator. Also, you will want to have a format specifier in the call to printf(), which you did not supply.

    Code:
    printf("The miles per gallon for this tank was %d" , mileage);

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    68
    [QUOTE=kermit]You are trying to pass the address of the variable mileage:

    Code:
    printf("The miles per gallon for this tank was " , &mileage);
    That is not what you want. Pass the variable as an argument, without the & operator. Also, you will want to have a format specifier in the call to printf(), which you did not supply.

    Code:
    printf("The miles per gallon for this tank was %d" , mileage);
    Strange I'm still not getting that last line to printout
    Last edited by Extropian; 07-21-2005 at 07:55 PM.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Code:
        mileage = miles / gallons; /* Calculates the tank's mileage */
    
        scanf("%d", &mileage);   /* This would be your problem */ 
    
        printf("The miles per gallon for this tank was %d\n",  mileage);
    The program waits for you to enter a value into mileage, which, incidentally, was supposed to be calculated on the line above.
    Last edited by kermit; 07-21-2005 at 08:06 PM.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    68
    Quote Originally Posted by kermit
    Code:
        mileage = miles / gallons; /* Calculates the tank's mileage */
    
        scanf("%d", &mileage);   /* This would be your problem */ 
    
        printf("The miles per gallon for this tank was %d\n",  mileage);
    The program waits for you to enter a value into mileage, which, incidentally, was supposed to be calculated on the line above.
    lol, thanks Im suffering two problems:
    1. Im a programming noobie
    2. AND I swear my brain aint workin this evening hehe
    thanks

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    2
    whats that scanf for after the computation of mile age, yes please learn to use printf

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    68
    I've moved on to the next part of the program, involving averaging milage for whatever number of tanks calculated but the of statement isnt working properly to end the program:

    Code:
    
    #include <stdio.h>
    
    int main()
    {
    	int miles;		
    	int gallons;	
    	int mileage;	
    	int total;		
    	int counter;	
    	int average;	 
    						
    	total = 0;	
    	counter = 0;
    
    
    	while (gallons != -1){
    
    		printf("Enter the gallons used (-1 to end): ");
    		scanf("%d" , &gallons);
    	
    		printf("Enter the miles driven: ");
    		scanf("%d" , &miles);
    	
    		mileage = gallons / miles;
    
    		total = total + mileage;
    
    		counter = counter + 1;
    
    		printf("The tank's mileage was %d:\n " , mileage);
    	}
    
    	if(gallons = -1){
    
    		average = total / counter;
    
    			printf("The overall mileage was %d: " , average);
    
    	}
    	return 0;
    }
    Any thoughts on why the if statement isn't done properly would be appreciated.
    Thanks,
    extro

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if(gallons = -1){
    = is an assignment.
    == is a comparison.


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

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    68
    Still won't work, not sure what it could be.

    Thanks,
    extro
    Last edited by Extropian; 07-22-2005 at 08:50 AM.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    This may be completley off the subject, but if you wrote it in C++, you could ditch printf() then send the output to cout and endl using namespace.

    As for the IF statement, what is it doing worng, how is the output not correct?

    you could try using the opposite to what quaza said, which would be this:

    Code:
    if (gallons <=-1)  {
    //code
    }
    that would mean that is the condition was less than or equal to -1 it would action the last lines of your code accordingly. But I really need to know what error the IF statment is making before I can help you further.

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    68
    I do have to write it in c unfortunately and I think it's a logic error, the program runs but if I put a -1 at the gallons it continues to ask me for miles driven again.
    I looked it over and it all seems to make sense,
    Not sure what the problem is.
    Thanks,
    Extro

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course it asks you for it again. Follow the flow of your loop:
    Code:
    while (gallons != -1){ /* start this loop cycle if gallons isn't -1 */
    
    		printf("Enter the gallons used (-1 to end): "); /* ask for gallons */
    		scanf("%d" , &gallons); /* read gallons */
    	
    		printf("Enter the miles driven: "); /* ask for miles, regardless of
                                                            what gallons is, because you
                                                            have no check here */
    		scanf("%d" , &miles); /* read miles */
    	
    		mileage = gallons / miles; /* coversion */
    
    		total = total + mileage; /* more stuff */
    
    		counter = counter + 1; /* more stuff again */
    
    		printf("The tank's mileage was %d:\n " , mileage); /* display stuff */
    
                    /* jump to top of the loop */
    	}
    
            /* only check this once you've finally gotten outside of the loop */
    	if(gallons = -1){
    
    		average = total / counter;
    
    			printf("The overall mileage was %d: " , average);
    
    	}
    	return 0;
    }
    It does exactly what you told it to do.


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

  14. #14
    Registered User
    Join Date
    May 2004
    Posts
    68
    I tried moving the while and if statements around a bit and it still didnt work,
    I so lost.
    Thanks for all your help,
    Extro

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are a million ways to fix this. I figured you'd think up one, but since you haven't, try this:
    Code:
    while (gallons != -1){ /* start this loop cycle if gallons isn't -1 */
    
    		printf("Enter the gallons used (-1 to end): "); /* ask for gallons */
    		scanf("%d" , &gallons); /* read gallons */
    		if( gallons == -1 )
    				break;

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

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

Similar Threads

  1. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 03:42 PM
  2. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  3. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 03:46 PM
  4. Help with simple program
    By nik2007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2006, 09:54 AM
  5. Need help on code to execute the program
    By hibmem10 in forum C++ Programming
    Replies: 6
    Last Post: 12-24-2005, 01:42 PM

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