Thread: c problem

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Unhappy c problem

    we can only use getchar, putchar, no arrays, no scanf for the calculations. printf is ok.


    so this program continously loops around the first if statement never processing the rest of the information.. how do i stop it and allow it to run the actualy calculation.

    the user is supposed to put in " 3 + 5" and then the program outputs "3 + 5 = 8"
    I am really stuck on it.
    That is what I got.


    Code:
    #include<stdio.h>
    #include<ctype.h>
    int addition(int,int);
    int subtraction(int,int);
    int multiplication(int ,int);
    double division(int,int);
    int module(int,int);
    int convert(int);
    int main()
    {
    	int num1,num2,ch,test=0,operation;
    	char answer;
    	do
    	{
    		printf(" Please enter your expresion\n");
    	    while((ch=getchar())!='+' && ch!='-' && ch!='*' && ch!='/' && ch!='%') 
    			{
    				if(test==1)
    				{
    					if(ch!=' ' && ch!='\t')
    					{
    						putchar(ch);
    					    
    						test=0;
    					}
    				}
    	
    			    else
    			    {
    				    if(ch==' ' || ch=='\t')
    					   test=1;
    			    
    				   else
    
    				   {
    					  putchar(ch);
    					 
    					  test=1;
    				   }
    				}
    
    				if(ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='%')
    
    					operation=ch;
    			}
    		     putchar(operation);
    		
    		
    
    	
    
    
        putchar('\n');
        printf("Would you lie to continue?(Y/N) ");
        scanf("%c",&answer);
        getchar();
       
    	}  while(answer=='y');
    	
    	return 0;
    }
    
    int addition(int num1,int num2)
    {
    	return num1+num2;
    
    }
    
    int subtraction(int num1,int num2)
    {
    	return num1-num2;
    }
    
    int multiplication(int num1,int num2)
    {
    	return num1*num2;
    }
    
    double division(int num1, int num2)
    {
    	
    
    	return num1/num2;
    
    }
    
    int module(int num1,int num2)
    {
    	return num1%num2;
    }
    
    int convert(int ch)
    {
    
        int sum;
    	sum=0;
    	while(((ch=getchar())!=' ') && ch!='\n')
    	{
    		if(!isdigit(ch))
    		
    			break;
    		sum=sum*10 + ch-'0';
    	}
    	return sum;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Wow! A morals quiz, in a programming exercise!

    Oh that's sneaky!
    printf("Would you lie to continue?(Y/N) ");
    scanf("%c",&answer);
    I can't get my head around this program. Where do you call the arithmetic operator functions?
    Last edited by Adak; 03-19-2011 at 11:17 PM.

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