Thread: do while loop got problem

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    20

    do while loop got problem

    Look at my code here, my do while loop got some sort of problem but i dont know how to fix it. The problem is when i loop again, it will not return my answer to zero first so that the answer previous will not affect the answer after the loop.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<stdlib.h>
    
    int main()
    {
    	char txt[200];
    	char temp[50][50];
    	char opt[200];
    	char par[200];
    	float num[200];
    	int keeploop = 0;
    	
    	do{
    	int i=0, j=0, k=0, n=0, m=0, xyz = 0;
            int yee = 0; 
            float ans = 0;
    	int asd = 0;
    	
    	printf("?\n");
    	gets(txt);
    	
    	i = strlen(txt);
    	
    	for(j=0;j<i;j++)
    	{               
            if(txt[j] == '(' || txt[j] == ')')
            {
                 par[xyz] = txt[j]; xyz++;    
            }
            else if(!(txt[j]=='+' || txt[j]=='-' || txt[j]=='*' || txt[j]=='/' || txt[j]=='^'))
          		{
            			 temp[m][k] = txt[j]; k++;
                }
                else
                {		
                   opt[n] = txt[j]; n++;
    			   num[m] = atof(temp[m]); m++;
    			   k=0;
                }          			
    	}
    	num[m] = atof(temp[m]); m++;
    	
    	asd = strlen(opt);
    	
    	for(j=0;j<asd;j++)
    	{
            if(opt[j]=='^')
    		{
    			ans = pow(num[j],num[j+1]);          
                for(yee=j;yee<asd+1;yee++)
                {
                    num[yee]=num[yee+1];
                }
                for(yee=j;yee<asd;yee++)
                {
                    opt[yee] = opt[yee+1];
                }        
                num[j] = ans; j--;
    		}              
         }	
    	
    	for(j=0;j<asd;j++)
    	{
    		if(opt[j]=='*')
    		{
    			ans = num[j] * num[j+1];          
                for(yee=j;yee<asd+1;yee++)
                {
                    num[yee]=num[yee+1];
                }
                for(yee=j;yee<asd;yee++)
                {
                    opt[yee] = opt[yee+1];
                }        
                num[j] = ans; j--;
    		}
    		if(opt[j]=='/')
    		{
    			ans = num[j] / num[j+1];
                for(yee=j;yee<asd+1;yee++)
                {
                    num[yee]=num[yee+1];
                }
                for(yee=j;yee<asd;yee++)
                {
                    opt[yee] = opt[yee+1];
                }        
                num[j] = ans; j--;			
    		}
    	}
    	
    	for(j=0;j<asd;j++)
    	{
    		if(opt[j]=='+')
    		{
    			ans = num[j] + num[j+1];          
                for(yee=j;yee<asd+1;yee++)
                {
                    num[yee]=num[yee+1];
                }
                for(yee=j;yee<asd;yee++)
                {
                    opt[yee] = opt[yee+1];
                }        
                num[j] = ans; j--;
    		}
    		if(opt[j]=='-')
    		{
    			ans = num[j] - num[j+1];
                for(yee=j;yee<asd+1;yee++)
                {
                    num[yee]=num[yee+1];
                }
                for(yee=j;yee<asd;yee++)
                {
                    opt[yee] = opt[yee+1];
                }        
                num[j] = ans; j--;			
    		}
    	}
    	
    
    	printf("%f\n\n",ans);
    	printf("%f\n",num[0]);
    	printf("%f\n",num[1]);
    	printf("%f\n",num[2]);
    	printf("%f\n",num[3]);
    	
    	printf("opt\n\n",ans);
    	printf("%c\n",opt[0]);
    	printf("%c\n",opt[1]);
    	printf("%c\n",opt[2]);
     }while(keeploop != 1);
    	getchar();
    }
    example first time loop i enter 123+456, then it return 579 as answer, then again i enter 5+6, it return different answer which is 1179. How do i fix this problem?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well for starts you need to move your variable definitions out of the loop.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    Quote Originally Posted by CommonTater View Post
    Well for starts you need to move your variable definitions out of the loop.
    After that? After i move my variable definitions out of my loop, the loop of operation are not working. Not operation starting 2nd loop

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your program shows the problem with using no functions except main().

    For a program of any complexity, that will twist your head around until it becomes like putty.

    You have a few functions that seem very natural. See if you agree:

    1) in main(), you need to get a string to be worked on, and set up some variables that you'll pass to other functions.

    2) you may want to remove spaces from the string, as the expression is shortened by your program.

    3) you need to get the next sub-string that your program will solve. Typically, you want the part inside the innermost parentheses.

    4) you need to have a function to pass the sub-string to, that will actually do the indicated arithmetic, inside it, and put the result back into the string, and set the sub-string back to "".

    #1, is no problem

    #2 you need to make this a separate function. I call my function for this "compress", and all it does is remove extra spaces. It is called right after the sub-string has been reduced.

    #3 are you finding the sub-string, OK? You should be printing it or watching it in a debugger, to be sure. You can't solve a sub-string without the sub-string being good, to start with.

    #4 the "heart" of the program. It needs the sub-string and string as input parameters, and most of these functions need the leftpar (left parenthesis), and the rightpar (right parenthesis), also.

    Please change your program over to take advantage of the power of separate functions. I can't work with your program as it is. The logic should be something like:

    Code:
    in main()
    
    while there are more operators (+-*/ whatever)
      get a sub string. Call your getSub() function
      pass it to the solver. Call your solver() function, which also changes the string, itself
      compress the string. Call your compress() function, the string will be shorter now
      set the sub-string to ""
      print the new string
    and loop back to the top of the while loop
    I've been re-writing a new program for this, and it is a bit tricky, imo. I know you aren't too comfortable with coding other functions, but they are a BIG benefit, and you will HAVE to learn how to do them, and get comfortable doing them. They are used in C programming ALL the time.

    So start in, and I'll jump in and assist - but you have to use these separate functions. It becomes a maze of code and logic, without it.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by yangss View Post
    After that? After i move my variable definitions out of my loop, the loop of operation are not working. Not operation starting 2nd loop
    The other advice you are receiving is excellent... but to loop back to this for a moment, putting variable definitions inside a loop means that, unless they are somehow disposed at the end of the loop, you are creating new copies of the variable every time through the loop.

    Code:
    While (x < 10)
       { int a = x + 20; 
          ++x; }
    Will give you the result you want because a is assigned an explicit value in the loop.

    However...
    Code:
    while (x < 10)
       { int a;
          a = a + (x + 20); 
          ++x; }
    Will produce erratic results because you are working on a new "a" every time through the loop. On each pass the "a" from the previous pass is lost and replaced with a new, uninitialized value that may not occupy the same memory space as the old one.

    Code:
    int a = 0;
    while (x < 10)
      { a = a + (x + 20);
         ++x; }
    Will produce reliable results.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple loop problem I cant seem to solve
    By LightYear in forum C Programming
    Replies: 8
    Last Post: 03-21-2010, 06:59 PM
  2. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 01:15 PM
  3. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  4. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  5. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM