Thread: Getting float result to appear

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    42

    Getting float result to appear

    Hi guys,

    I am trying to program the following type calculator:

    Welcome to <John Doe’>s Handy Calculator

    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division
    5. Exit

    What would you like to do? 3

    Please enter two numbers to multiply separate by a space: 24 6
    Result of multiplying 24.00 and 6.00 is 96.00.
    Press any key to continue …
    ..........................................

    I am stuck on the "Result....." line. First off I can't get the result to appear as a float (or at all for that matter). I am sure it is something basic I am doing incorrectly. Also I can't figure out how to place the "and" between the 2 numbers in the printf() statement.
    I am currently testing it under Case 1 in the code as you can see.

    You don't have to do it for me, just a push in the right direction would be appreciated Thanks

    Code:
    #include <stdio.h>
    
    int main()
    
    {
        int value;
    	int num1,num2;
    	float result;
    	
    	num1=0;
    	num2=0;
    	result=0;
    	
    	printf("Welcome to xxxxxxx's handy calculator\n");
    	printf("      1. Addition\n");
    	printf("      2. Subtraction\n");
    	printf("      3. Multiplication\n");
    	printf("      4. Division\n");
    	printf("      5. Exit\n");
    	printf("What would you like to do?");
    	scanf("%d",&value);
    
    	if(value>5 || value<1)
    	{
    		printf("You have entered an incorrect number");
    	}
    	
    	switch (value)
    	{
    	case 1:
    		printf("Please enter two numbers to add separate by a space: ");
    		scanf("%d %d",&num1,&num2);
    		printf("Result of adding %d+%d is: %2.2f",num1, num2, result);
    		break;
    	case 2:
    		printf("Please enter two numbers to subtract separate by a space: ");
    		break;
    	case 3:
    		printf("Please enter two numbers to multiply separate by a space: ");
    		break;
    	case 4:
    		printf("Please enter two numbers to divide separate by a space: ");
    		break;
    	
    	
    	}
    	
    	
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    after looking over the assignment again, it appears the scanf() input should allow the use of int or float type numbers. Hmmm, looks like I need to clean that part up as well....

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    float num1;
    float num2;
    
    	case 1:
    		printf("Please enter two numbers to add separate by a space: ");
    		scanf("%f %f",&num1,&num2);
    		printf("Result of adding %6.2f and %6.2f is: %7.2f",num1, num2, num1 + num2);
    		break;

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Thank you Tater,

    I actually figured it out before I saw your response, though I did it slightly different but it appears to compile fine. I realized I needed to assign my value as Int, but my num1 and num2 as float....

    Code:
    float num1,num2;
    
    case 1:
    		printf("Please enter two numbers to add separate by a space: ");
    		scanf("%f %f",&num1,&num2);
    		printf("The result is %.2f\n", num1+num2); 
    		break;

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    It's not a huge issue in this program, but it would be a good idea to get into the habit of explicitly defining all your cases as well as a default case to catch error. When you start doing more complicated programs it will make it easier for you to debug, and for other people to understand.

    Code:
    case 5: 
                break;      //explicit do-nothing case for the "exit" option
    default:
                printf( "You're not supposed to be able to get here!!\n";
                break;

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I advise against that in this instance. Only add a default case either when you actually need it, OR when it would be catastrophic if it were to ever not hit one of the other cases. In the above program nothing bad would happen as a result of not being equal to one of the provided cases.
    If you were to have something like this:
    Code:
    Foo *foo; // may or may not initialise this...
    switch (bar)
    {
       case 1: foo = new foobar(); break;
       case 2: foo = new barfoo(); break;
    }
    foo->doMyFoo(); // Boom!
    Then a default case which asserts (and in the case of C++ perhaps also throws) would be a good idea, and is sufficient.

    Every line of code you have has the potential to contain one or more bugs. The less unnecessary code you have, the more important code you can see at once on your screen which is more helpful to comprehend the code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Trying to input number only
    By Jack_Zepp in forum C Programming
    Replies: 5
    Last Post: 07-20-2010, 01:46 PM
  2. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  3. Float Function Issues
    By GCNDoug in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2007, 03:25 PM
  4. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM
  5. ~ Array Help ~
    By Halo in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 04:19 PM