do while loop to continue program

This is a discussion on do while loop to continue program within the C Programming forums, part of the General Programming Boards category; The assignment is: Add a do-while loop so that the program will continue prompting the user to see if another ...

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

    do while loop to continue program

    The assignment is: Add a do-while loop so that the program will continue prompting the user to see if another calculation is to be done.
    I can't seem to figure out how to do the do-while loop. (where to put it) What I have gives runs but not 100% correct, so I'm assuming it is a minor detail. Please help.
    Thanks

    Code:
    #include <stdio.h>
    
    int main()
    {
    	// Variable Declarations
    	float num1, num2, result;
    	char sel, sel2, Y;
    	
    	do
    	{	
    	// Input: Getting 2 numbers
    	printf("Enter 2 numbers: ");
    	scanf("%f %f",&num1, &num2);
    	
    	//Displaying the menu
    	printf("\nMenu:\n");
    	printf("A ----- Addition\n");
    	printf("S ----- Subtraction\n");
    	printf("M ----- Multiplication\n");
    	printf("D ----- Division\n");
    	
    	// Input: Getting the user's selection
    	printf("\nEnter the letter corresponding to the calculation to be performed: ");
    	scanf(" %c",&sel);
    
    
    	
    		if(sel=='A' || sel=='a')
    		{
    			result = num1+num2;
    			printf("\nThe addition is: %.2f\n",result);
    		}
    		else if(sel=='S' || sel=='s')
    		{
    			result = num1-num2;
    			printf("\nThe subtraction is: %.2f\n",result);
    		}
    		else if(sel=='M' || sel=='m')
    		{
    			result = num1*num2;
    			printf("\nThe multiplication is: %.2f\n",result);
    		}
    		else if(sel=='D' || sel=='d')
    		{
    			result = num1/num2;
    			printf("\nThe division is: %.2f\n",result);
    		}
    		else
    		{
    			printf("\nInvalid Choice\n");
    		}
    		
    		printf("Would you like to perform another calculation?");
    		scanf(" %c", &sel2);
    	}while(sel2=Y);
    	
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    I forgot to mention that this must be in C.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you have a scanf() for a number, and then you have a scanf() for a char, you need to get rid of the newline char, in the keyboard buffer, before the scanf() for the char.

    So:

    scanf() //for the float or int

    getchar() //removes the newline char

    scanf() for the char

    Otherwise the scanf() for the char will be "skipped" (looks like it anyway), frequently.

    Welcome to the C forum! Not to worry, C is all we do here!

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    Thanks for the quick reply, however I have not learned the getchar() function in my class, therefore I can not use it. Is there another way?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    }while(sel2=Y);
    Assuming Y is not zero, this will always be true. You are also never initializing Y or even changing its value, so who knows how long this will loop. (Hint: The good money is on 'forever'.)


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    Ok I figured it out. I just changed }while(sel2=Y); to }while(sel2=='Y');

    Thanks guys, I'll be back again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how can I get the program to loop back into itself?
    By javalurnin in forum C Programming
    Replies: 5
    Last Post: 12-04-2009, 11:38 AM
  2. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 03:50 PM
  3. Loop issue in tax program
    By mistymoon1966 in forum C Programming
    Replies: 4
    Last Post: 10-31-2009, 02:04 PM
  4. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  5. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21