Page 5 of 10 FirstFirst ... 34567 ... LastLast
Results 41 to 50 of 95

Thread: need a bit help in C

  1. #41
    You can ignore the warnings for now, it is just Visual Studio complaining, other C compilers don't do it, to supress it though use #define _CRT...INGS.

    One thing I can see is you haven't escpaed your quotation marks in the printf string, that should clear up all the errors on line 37. Visual studio should change the colour for %d on that line showing that it isn't within the string that is displayed.

    The other errors, have a read of them. There is one error on line 13, not the 3 the errors imply. To solve this and the other errors I suggest going through all your open and closed brackets or braces and ensure they pare where you think they should (ie put the cursor next to a { or ( and it will highlight, its pair will highlight as well)
    E-Peen: AMD Phenom II x4 965 (stock) | MSI 790FX-GD70 | ATI HD 5970 (stock) | Kingston HyperX DDR3 1333 CL7 2x2GB | Seagate 7200.12 4x1TB | Ikonic RaX10 Liquid

  2. #42
    yeah, now I see it, remove the quotations marks in the middle from the last printf. And I think the the cases inside switch don't need any quotations marks.

  3. #43
    Those ones are fine, in those specific instances you are referring to the character * so denote it '*' in C source code. This way if you wanted the ascii character for the numeral 1 then you would use '1' but if you wanted the numeric value you would just use 1.

    Did you manage to sort out the other issues with the brackets and braces?
    E-Peen: AMD Phenom II x4 965 (stock) | MSI 790FX-GD70 | ATI HD 5970 (stock) | Kingston HyperX DDR3 1333 CL7 2x2GB | Seagate 7200.12 4x1TB | Ikonic RaX10 Liquid

  4. #44
    8-bit overflow
    Join Date
    Mar 2011
    Posts
    544
    the qoutation marks were superfluous at all.
    additionally there was an erroneous brace in place of bracket after printf. It allowed to build the program

    I had a problem that i couldnt in put operator at first attemt. But it seems that isolved it by adding fflush(stdin) to the very beginning of the loop.

    i ll try to optimize it further by getting rid of z
    IMHO
    Core i7-4770k with Thermalright True Spirit 140, ASRock Z87 Extreme 4 Mobo, 4x4 GB TeamGroup DDR3-2133, Sapphire Radeon HD 6950 2GB, 2xSamsung 840 Pro 256 GB in RAID 0, WD Caviar Black 1TB SATA 3 HDD, Corsair RM750x PSU, LG BH10LS30 BD-RE, Ultron Cardreader, Windows 7 Ultimate x64, LG 23MP68VQ FullHD monitor

  5. #45
    8-bit overflow
    Join Date
    Jul 2009
    Posts
    397
    Quote Originally Posted by Spartak View Post
    i ll try to optimize it further by getting rid of z
    Don't get rid of it - but look at:
    a) making it a bool
    b) giving it a more meaningful name (bool invalidInput)
    c) Giving it an initial setting that means you only need set it to something different if something goes wrong (and can therefore remove it from all the other case options.)

    You can replace it by moving the loop into a boolean return function, but I see no advantage of doing such.

    I'd also check what happens in that case statement if you enter the first operator - you are missing something.
    dd if=/dev/urandom of=/dev/kmem bs=1 count=1 seek=$RANDOM

  6. #46
    edit: ill remove some stuff so i dont get you confused.
    -V
    My views and opinions do not represent those of my employer

  7. #47
    Alvter either is fine for numeric work so long as you understand the limitations on them, ie with char you can't go past 256 and int takes up much more room if you are using an embedded system.
    E-Peen: AMD Phenom II x4 965 (stock) | MSI 790FX-GD70 | ATI HD 5970 (stock) | Kingston HyperX DDR3 1333 CL7 2x2GB | Seagate 7200.12 4x1TB | Ikonic RaX10 Liquid

  8. #48
    Quote Originally Posted by rambaldi View Post
    Alvter either is fine for numeric work so long as you understand the limitations on them, ie with char you can't go past 256 and int takes up much more room if you are using an embedded system.
    Then I take it that I'm just rusty.... Yes, a Char only has a 256 limit, however I wasn't sure it its a Numeric OR litterally an ASCII...


    Example:
    Defined as ascii
    Code:
    			case '*':
    			case '/':
    			case '+':
    			case '-':
    defined as a numeric
    Code:
    			z=1;
    			}while(z==0)
    -V
    My views and opinions do not represent those of my employer

  9. #49
    Lol, probably about as rusty as I am. I actually used some C earlier in there year but only so i could get at its gooey assembly centre.
    E-Peen: AMD Phenom II x4 965 (stock) | MSI 790FX-GD70 | ATI HD 5970 (stock) | Kingston HyperX DDR3 1333 CL7 2x2GB | Seagate 7200.12 4x1TB | Ikonic RaX10 Liquid

  10. #50
    Good ol' notepad and a free C compiler !

    Try the below... You were close! You had some syntax errors which I fixed. And not being able to use the "if" statement was annoying. I didn't program around all the errors, such as someone not inputting a numeric value in beginning, and multiple character responses.


    Code:
    #include <stdio.h>
    
    void main(void) {
    
    		char sign, operand1, operand2, result, z;
    		
    		printf("Enter the first number: ");
    		scanf("%d", &operand1);
    		printf("Enter the second number: ");
    		scanf("%d", &operand2);
    		
    		
    		z=0;
    		
    		while(z!=99) {
    
    			switch(z){
    				case 0:
    					printf("Please enter the operator (+,-,/,*):");
    					z=1;
    					
    				case 1:
    				break;
    				
    				case 2:
    					printf("Bad input. Please enter the operator (+,-,/,*):");
    					z=3;
    				break;
    				
    				default:
    				break;
    				
    			}//switch
    		
    			
    		
    		scanf("%c", &sign);
    			
    			
    			switch(sign) {
    				case 42: // *
    					result=operand1*operand2;
    					z=99;
    					
    				case 47:// /
    					result=operand1/operand2;
    					z=99;
    				break;
    					
    				case 43: // +
    					result=operand1+operand2;
    					z=99;
    				break;
    				
    				case 45: // -
    					result=operand1-operand2;
    					z=99;
    				break;
    				
    				case 10:
    					z=1;
    				break;
    				
    				default:
    					z=2;
    				break;
    					
    			} //switch
    			
    		}//while
    		
    		printf("The result is:  ");
    		printf("%d\n",result);
    
    }//main
    -V
    My views and opinions do not represent those of my employer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
WordPress Appliance - Powered by TurnKey Linux