One Last Question....I swear (its due at midnight!)

This is a discussion on One Last Question....I swear (its due at midnight!) within the C Programming forums, part of the General Programming Boards category; My program seems to work now byt all of the answers come out to zero - any ideas???? Code: /* ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    7

    Question One Last Question....I swear (its due at midnight!)

    My program seems to work now byt all of the answers come out to zero - any ideas????

    Code:
    /* Assignment 1 */
    
    #include <stdio.h>
    
    main()
    {
    	
    	int i;
    	int j;
    	float f;
    	f=1.23;
        	
    		
    	{
    		
    		/* Menu of Choices */
    		printf("1: cm to in\n");
    		printf("2: cm to ft\n");
    		printf("3: cm to mi\n");
    		printf("4: in to cm\n");
    		printf("5: in to ft\n");
    		printf("6: in to mi\n");
    		printf("7: ft to cm\n");
    		printf("8: ft to in\n");
    		printf("9: ft to mi\n");
    		printf("10: mi to cm\n");
    		printf("11: mi to in\n");
    		printf("12: mi to ft\n");
    		printf(" Enter your selection: \n");
    		scanf("%d", &i);
    		switch (i)
    
    		{
    			case 1: /* cm to inches */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);
    			j=i/  ( float ) 2;
    			printf("There are %f inches\n", j);
    			break;
    
    			case 2: /* cm to feet */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);
    			j=i/30.5;
    			printf("There are %f ft\n", j);
    			break;
    
    			case 3: /* cm to mi*/
    			printf("Enter quantity: \n");
    			scanf("%f", &i);
    			j=i/160934.4;
    			printf("There are %f mi\n", j);
    			break;
    
    			case 4: /* inches to cm */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);
    			j=i*2.4;
    			printf("There are %f cm\n", j);
    			break;	
    
    
    			case 5: /* inches to feet */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i/12;
    			printf("There are %f ft\n", j);
    			break;
    
    			case 6: /* inches to miles */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);
    			j=i/63360;
    			printf("There are %f mi\n", j);
    			break;
    
    			case 7: /* feet to cm */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);
    			j=i*30.5;
    			printf("There are %f cm\n", j);
    			break;
    
    			case 8:  /* feet to inches */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i*12;
    			printf(" There are %f in\n", j);
    			break;
    
    			case 9: /* feet to miles */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i/5280;
    			printf("There are %f mi\n", j);
    			break;
    
    			case 10: /* miles to cm */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i*160934.4;
    			printf("There are %f cm\n", j);
    			break;
    
    			case 11: /* miles to inches */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i*63360;
    			printf("There are %f in\n", j);
    			break;
    
    			case 12: /* miles to feet */
    			printf("Enter quantity: \n");
    			scanf("%f", &i);	
    			j=i*5280;
    			printf("There are %f ft\n", j);
    			break;
    		}
    	}		
    return 0;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    scanf("%f", &i);
    i is an int not a float
    Code:
    j=i/12;
    will result in an integer always

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    7
    ok...then I am seriously lost as to what to do...I tried the following:

    Code:
    case 1: /* cm to inches */
                printf("Enter quantity: \n");
                scanf("%f", &f);
                j=f/  ( float ) 2.4;
                printf("There are %f inches\n", j);
                break;
    This is still resulting in a zero answer ( actually the answer is coing out 0.00000)

    Any hep would be EXTREMELY appreciated!

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    You're still trying to use type int as floating point numbers.
    Code:
        int i;  /* don't reuse this variable in your switch statements */
        int j;  /* should be type float */
                /* <--------- Should add another variable of type float for use inside case functions*/
        float f;
        f=1.23;
    .
    .
    .
                case 1: /* cm to inches */
                printf("Enter quantity: \n");
                scanf("%f", &i); /* i is declared as int, the statement requires a float 
                                    declare and use a new variable type float as mentioned above */ 
                j=i/  ( float ) 2;
                printf("There are %f inches\n", j);
                break;
    Last edited by Scribbler; 02-02-2005 at 08:39 PM.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Code:
    /* <--------- Should add another variable of type int for switch cases */

  6. #6
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by jverkoey
    Code:
    /* <--------- Should add another variable of type int for switch cases */
    gack!

    Actually that's a bad case of wording. I meant it to mean declare a new variable type float for use in the functions inside each switch/case. I edited it to reflect proper wording .
    Last edited by Scribbler; 02-02-2005 at 08:40 PM.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    7
    Thank you all so much! You have been wonderful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Math Question, hard
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 12-08-2001, 11:58 AM

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