Thread: Dumb Problem

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Dumb Problem

    My friend just sent me this code block and I can't find out what is wrong, the idea is the following, it scans for both army numbers, and it must be done in the same scan, same line. After it comepares the numbers, if hamashs army is smaller it should prints fight and the difference between the 2 armys, else it prints runs and the difference.

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    int main (){
        int hmsharmy, earmy, dif;
        
    	hmsharmy = earmy = dif = 0;
    	
        printf("Should Hamash Fight or Run!?:");
        printf("\nPlease inform the number of soldier in Hamash's and in the enemy army is THIS ORDER:");
         scanf("%d",&hmsharmy,&earmy);
    	 //scanf("%d",&earmy);
    	  dif=&hmsharmy-&earmy;   
    	if (dif < 0) {
            printf("\nWe shall fight!\n");
    	    printf("%d\n",&dif); } 
    		   else {printf("\nWe shall run!\n");
    			 printf("%d\n",&dif);}
    	
        return 0;      
    }
    By the moment it returns they should allways run, and doesn't print the right number on the difference.

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    int main (){
        int hmsharmy, earmy, dif;
        
    	hmsharmy = earmy = dif = 0;
    	
        printf("Should Hamash Fight or Run!?:");
        printf("\nPlease inform the number of soldier in Hamash's and in the enemy army is THIS ORDER:");
         scanf("%d %d",&hmsharmy,&earmy);
    	 //scanf("%d",&earmy);
    	  dif=hmsharmy-earmy;   
    	if (dif < 0) 
    	{
            printf("\nWe shall fight!\n");
    	    printf("%d\n",dif); 
    	} 
    	else 
    	{
    		printf("\nWe shall run!\n");
    		printf("%d\n",dif);
    	}
    	
        return 0;      
    }
    Try this!

    First you enter only number of hmsharmy and not earmy.
    Second you calcutae difference between addresses of these variables in memory, and you shouldn't do this. Third in printf use name of variable not address.
    So pay attention on scanf,printf and dif
    Last edited by Micko; 07-22-2004 at 02:13 PM.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Thx a lot now I figured out, I was using scanf unproperly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM