int main()
{
    // stating my primivative data types and variables 
    float num1,result;
    char symbol, choice;
	
   
    printf("Calculator Is ON\n");
	
	do{  
		
    
		result=0.0; //assign 0.0 to result 
		
        printf("Result=%.1f\n", result); 
		
		scanf(" %c",&symbol); 
       

        
		while((symbol!='r') && (symbol!='R'))
        {
            
            scanf(" %f",&num1); 
            switch(symbol) 
            {
                case'+':
                
                    result=result+num1;
                    printf("Result = %.1f\n",result);
                    break;
                
                case'-':
                
                    result=result-num1;
                    printf("Result = %.1f\n",result);
                    break;
                
                case'*':
                
                    result=result*num1;
                    printf("Result = %.1f\n",result);
                    break;
                
                case'/':
                
                    result=result/num1;
                    printf("Result = %.1f\n",result);
                    break;
                
                default:'%';
                    printf(" %c is an unknown operation\nReenter, your last line:",symbol);
                    break;
                   
                    
            }
            
           scanf(" %c",&symbol);
            
        }
        printf("Final result=%.1f",result);
        printf("\nAgain [y/n]\n",choice);
		scanf(" %c",&choice);
}	
	while((choice!='n') && (choice!='N'));
        printf("End of Program");
    
    return 0 ;
}

My question is when i enter r or R kicks me out of the do-while loop and then ask me if i want to continue then i press yes or no. My question is the logic behind the second while loop. If i press yes or y does it bring you all the way back to the start of the do while?

Your indentation is horrid, of course. I believe your problem is that the scanf(), for a char especially, is troublesome, because scanf() always leaves the newline behind (when the user hits enter a newline is sent to the input buffer, as well as the keystroke he hits).

To remove the newline, add a getchar(), (or use any other means you want), to pull the newline off the input buffer. For multiple input problems, use the getchar() in a while loop, and pull off all the input char's until the char is a '\n' (a newline). That will be the last char, normally.

while((charVariable=getchar()) != '\n');

Note the semi-colon - this is a one line while loop.

Since the newline isn't seen, this always trips up beginners who rely on scanf() for user input. Another example of this can be seen where you have a do while(intVariable != 0), loop, which maybe shows a menu repeatedly. If you have no getchar(), and the user enters a single letter instead of a number -- well, it's not pretty. Try it. ;)

Edited 5 Years Ago by Adak: n/a

Your indentation is horrid

His indentation looks horrid, but notice that some of his lines are tabed and some are spaced. I have this problem too when copying code from an IDE where tabs are set to allocate an unconventional specific number of fixed width spaces. It makes copying terrible. On that kind of IDE, your better off using spaces exclusively or tabs exclusively.

On that kind of IDE, your better off using spaces exclusively or tabs exclusively.

Actually you're (note the spelling) better off using SPACEs. They are always consistent. TABs are not

Actually you're (note the spelling) better off using SPACEs. They are always consistent. TABs are not

If IDE A has a tab spacing length of 2 and IDE B has a tab spacing of 4, as long as you are consistent with the tabs, and as long as its only used to indent the code, that alone should be fine. Mixing tabs and spaces is the primary issue.

I have also seen IDE's where you press enter and it auto-inserts tabs for you rather than spaces, so being consistent requires looking at the IDE settings before you begin your programming. And as far as copying code from other sources, that needs to be checked for proper indentation as well.

If IDE A has a tab spacing length of 2 and IDE B has a tab spacing of 4, as long as you are consistent with the tabs, and as long as its only used to indent the code, that alone should be fine. Mixing tabs and spaces is the primary issue.

And when posting on a forum, all bets are off. TABs rarely line up the way you want and given proper (and deep) indentation the code is difficult to read. By converting TABs to a set number of spaces in your IDE, your code will always be readable.

Consider with SPACEs

int main()
{
    int i, j, k;
    printf("Listing: ");
    for (i=1; i< 10; i++)
    {
        for (j=1; j< 10; j++)
        {
            for (k=1; k< 10; k++)
            {
                printf("%d:%d:%d ", i,j,k);
            }
        }
    }
    return 0;
}

vs with TABs

int main()
{
	int i, j, k;
	printf("Listing: ");
	for (i=1; i< 10; i++)
	{
		for (j=1; j< 10; j++)
		{
			for (k=1; k< 10; k++)
			{
				printf("%d:%d:%d ", i,j,k);
			}
		}
	}
	return 0;
}

The second one doesn't look like my editor, and is harder to read.

And if just posting a portion of the code 8 levels deep:

for (k=1; k< 10; k++)
                                    {             
                                        printf("%d:%d:%d ", i,j,k);
                                    }

vs

for (k=1; k< 10; k++)
								{			 
									printf("%d:%d:%d ", i,j,k);
								}

So TABs are evil. So there. :icon_razz:

Another question is why is this do-while the do's while doesn't need a ; at the end ?

Comments
Use punctuation so we can understand your question. It's unintelligible as it is.
This question has already been answered. Start a new discussion instead.