Thread: Learning C

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    Learning C

    I'm just starting out on C and am trying to write a simple calculator program. The code looks good to me, but it always spits out nonsense answers like 2.11992e-314. They may be mathematically correct, but I'm not sure why they are formatted this way.

    Code:
    #include <stdio.h>
    
    int main () {
        float ValueOne = 0;
    	float ValueTwo = 0;
    	int defOp;
    	float Addition = ValueOne + ValueTwo;
    	float Subtraction = ValueOne - ValueTwo;
    	float Multiplication = ValueOne * ValueTwo;
    	float Division = ValueOne / ValueTwo;
    	printf("Enter the first value");
    	scanf("%f", &ValueOne);
    	printf("Enter the second value");
    	scanf("%f", &ValueTwo);
        printf("Enter the desired operation, 1 for Addition, 2 for Subtraction, 3 for Multiplication, 4 for Division");
        scanf("%d", &defOp);
    	if(defOp = 1) {
    		printf("%g", &Addition);
    	} else if(defOp = 2) {
    		printf("%f", &Subtraction);
    	} else if (defOp = 3) {
    		printf("%f", &Multiplication);
    	} else if (defOp = 4) {
    		printf("%f", &Division);
    	} else {
    		printf("Invalid input");
    	}
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,825
    Code:
    if(defOp = 1) {
    "=" is assignment!
    "==" is comparison(is equal).

    C is not magic; the instructions are done in top to bottom order unless special instructions are use like loops or other order changing instructions.

    Code:
    a = 0;
    b = 0;
    answer = a + b; // set answer equal to 0 + 0
    a = 1;
    b = 2;
    // answer is still 0 at this location
    Tim S.
    Last edited by stahta01; 12-23-2011 at 04:49 PM.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Your printfs are wrong. You don't need the "&", and "%g" is incorrect.

    Also, a word of warning, don't ever use "magic numbers." The numbers that you associate with the operations are random, and mean nothing to someone who reads your code. While it's preferable to enter one statement "2+2", you could also scan for the operator itself (+, -, , *). But if you must use numbers, at least use #defines so it makes the code readable.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    Quote Originally Posted by stahta01 View Post
    Code:
    if(defOp = 1) {
    "=" is assignment!
    "==" is comparison(is equal).

    C is not magic; the instructions are done in top to bottom order unless special instructions are use like loops or other order changing instructions.

    Code:
    a = 0;
    b = 0;
    answer = a + b; // set answer equal to 0 + 0
    a = 1;
    b = 2;
    // answer is still 0 at this location
    Tim S.
    Thanks, you helped to better understand how C works. I was expecting it to behave more like java.
    Also, a word of warning, don't ever use "magic numbers." The numbers that you associate with the operations are random, and mean nothing to someone who reads your code. While it's preferable to enter one statement "2+2", you could also scan for the operator itself (+, -, , *). But if you must use numbers, at least use #defines so it makes the code readable.
    I'll look into it. I don't totally understand #defines yet though.

    Also, for anyone who would like to learn from my mistakes, this is the final code. It works correctly.
    Code:
    #include <stdio.h>//Written by TheNumber
    int main () {
        float ValueOne;
    	float ValueTwo;
    	int defOp = 0;
    	printf("Enter the first value\n");
    	scanf("%f", &ValueOne);
    	printf("Enter the second value\n");
    	scanf("%f", &ValueTwo);
        printf("Enter the desired operation, 1 for Addition, 2 for Subtraction, 3 for Multiplication, 4 for Division\n");
        scanf("%d", &defOp);
    	if(defOp == 1) {
    		float Addition = ValueOne + ValueTwo;
    		printf("%f", Addition);
    	} else if(defOp == 2) {
    		float Subtraction = ValueOne - ValueTwo;
    		printf("%f", Subtraction);
    	} else if (defOp == 3) {
    		float Multiplication = ValueOne * ValueTwo;
    		printf("%f", Multiplication);
    	} else if (defOp == 4) {
    		float Division = ValueOne / ValueTwo;
    		printf("%f", Division);
    	} else {
    		printf("Invalid input");
    	}
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Just to make a note, there's nothing wrong with passing a double to %g in printf(), which of course means it's OK to pass a float to it. Whether one desires the output of %g over %f is another matter, but there's nothing inherently invalid about %g.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by TheNumber View Post
    Thanks, you helped to better understand how C works. I was expecting it to behave more like java.
    You might also want to work on developing your understanding of Java then. The errors you made with comparison versus assignment, and with the order of instructions, are errors in Java as well as in C.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Dos and learning Windows
    By blankstare77 in forum C++ Programming
    Replies: 8
    Last Post: 07-31-2005, 03:48 PM
  2. Learning C# vs C++
    By 747ken in forum C# Programming
    Replies: 1
    Last Post: 11-10-2003, 01:19 AM
  3. Learning COM
    By minesweeper in forum Windows Programming
    Replies: 5
    Last Post: 03-03-2003, 03:30 AM
  4. learning C++ from C
    By gratyklz in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 02:09 PM