Find the answer to your Linux question:
Results 1 to 3 of 3
Hello all, something really astounding happened! ........... Thursday........ Jun 19th, ........... nearly midnight ............ My C program worked! It was an exercise question in a C book. To make a ...
  1. #1
    Linux User
    Join Date
    Jun 2007
    Posts
    458

    Red face My C Programs Begin to Work!

    Hello all, something really astounding happened!

    ...........

    Thursday........

    Jun 19th,

    ...........

    nearly midnight
    ............

    My C program worked!

    It was an exercise question in a C book. To make a calculator that works wit these:

    <number><space><sign>

    The signs can do these:
    S: set the value of the accumulator
    /: divide accumulator by number specified
    x: multiply accumulator by number
    +: you know what
    -: //ditto

    0 E would end the program:

    Code:
    #include <stdio.h>
    
    int main () {
    	float value, number;
    	char sign;
    	int tmp;
    
    	printf("Begin Calculations\n");
    	for ( tmp = 0; ++tmp; tmp > 0 ) {
    		scanf("%f %c", &number, &sign);
    
    		switch ( sign ) {
    		case 'S': {
    			value = number;
    			printf("= %f\n", value);
    			break;
    		}
    
    		case '+': {
    			value += number;
    			printf("= %f\n", value);
    			break;
    		}
    
    		case '-': {
    			value -= number;
    			printf("= %f\n", value);
    			break;
    		}
    
    		case 'x': {
    			value *= number;
    			printf("= %f\n", value);
    			break;
    		}
    
    		case '/': {
    			value /= number;
    			printf("= %f\n", value);
    			break;
    		}
    
    		case 'E': {
    			printf("= %f\n", value);
    			printf("End of Calculations\n");
    			return 0;
    		}
    		}
    	}
    	return 0;
    }
    Now is the return statement correct in last case i.e. E? How to use break there?
    "When you have nothing to say, say nothing."

  2. #2
    Linux Newbie
    Join Date
    Nov 2007
    Location
    Planet Earth
    Posts
    152
    Break is optional... in this case is not necessary because of return statement.

    Congrats... I remembered my first C program too... oh old good times hehehe...

    Hugo
    EOF

  3. #3
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    949
    It's not just a C thing. The first program I ever wrote was in basic; it converted centigrade to fahrenheit and backwards. And when I ran it and it actually worked, I knew how God felt on the seventh day. It was that sense of omnipotence: I had told a computer what to do and the computer had actually done it!
    "I'm just a little old lady; don't try to dazzle me with jargon!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •