Little help with this please...

This is a discussion on Little help with this please... within the C Programming forums, part of the General Programming Boards category; Ok, this is for a class. We were required to write a program to calculate mileage reimbursement for a salesperson ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Iowa
    Posts
    14

    Little help with this please...

    Ok, this is for a class. We were required to write a program to calculate mileage reimbursement for a salesperson at a rate of $0.45 per mile. The program has to take the starting and ending mileage as inputs and then display distance traveled and total reimbursement. I have writen this and it compiles fine, but when I run it it quits out half way through. Bare with me, I'm pretty new at this so I might have made some stupid mistake, but can anyone help me out and maybe show me what I have wrong?



    Code:
    #include "stdafx.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	float miles, odometer1, odometer2, reimbursement, rate;
    	printf("MILEAGE REIMBURSEMENT CALCULATOR.\n");
    	printf("Enter the beginning odometer reading:>");
    	scanf("%f", &odometer1);
    	printf("\nEnter the ending odometer reading:>");
    	scanf("%f", &odometer2);
    	miles = odometer2 - odometer1;
    	printf("\nSalesperson traveled %6.2f", miles);
    	rate = 0.45;
    	reimbursement = miles * rate;
    	printf("\nTotal owed reimbursement is %6.2f", reimbursement);
    
    	getchar();
    	return 0;
    }
    Last edited by Salem; 09-11-2006 at 06:38 AM. Reason: Added code tags - learn to use them

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    First, of all, learn to use code tags.

    That'll help you get help much, MUCH faster.

    And you say it quits midway through. What is that last thing you see ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    By that do you mean, after you enter your input? You're getchar, which you seem to expect will hold open the console blocking for your input, will probably have picked up trash in the input buffer. Search the forums or the internet for ways to

    - Keep console open
    - Clear input buffer
    - Avoid fflush(stdin)

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    Iowa
    Posts
    14
    Ok... the first 2 printf commands come up and I enter the beginning odometer reading. Then the next printf comes up and i enter the ending odometer reading. Then it quits out, just as if the program is over and it skips the rest.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Add another getchar() at the end. It's just closing cause the first getchar() is taking the '\n' left in your stdin. The second will wait for input.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    Iowa
    Posts
    14
    Thank you very much. It worked. Like I said I just started with all this so hopefully I'll get better fast. Thanks again.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could add a "\n" to the end of scanf() format strings to consume additional whitespace. Or use fgets().
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

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