Thread: Do While Loop Need Help Fast!!!

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    11

    Do While Loop Need Help Fast!!!

    Im trying to set this do while loop in my code and it keeps giving me an error .
    Code:
    #include <stdio.h>
    #define interest = .02	
    int main ()
    
    {
        int date, choice, option1, option2;
    	double amt, balance= 2000.00;
    
    	do{
    			
    			printf(" Welcome to Moon Trust Bank!\n\n");
    			printf(" Please Choose From The Following Options.\n\n");
    			printf("   1)  Deposit Funds (Credit Transaction)\n");
    			printf("   2) Withdraw Funds ( Debit Transaction)\n");
    			printf("   3) Print Statement of Account\n"); 
    			printf("   4) Compute Interest and Exit the Program\n");
    	
    			scanf("%d", &choice);
    	
    	if (choice == 1)
    	
    			{
    				printf("Your current balance is $%.2f.\n", balance);
    				printf("Please enter the date.\n");
    				scanf("%d", &date);
    				
    				printf("Please enter the amount you wish to deposit into your account.\n");
    				scanf("%lf", &amt);
    				
    				balance = balance + amt;
    				
    				printf("Your current balance is $%.2f.\n\n", balance);		
    
    			}
    	
    	else if (choice == 2)
    	
    			{
    				printf("Your current balance is $%.2f.\n", balance);
    				printf("Please enter the date.\n");
    				scanf("%d", &date);
    				
    				printf("Please enter the amount you wish to withdraw from your account.\n");
    				scanf("%lf", &amt);
    					
    						if (amt > balance)
    								{
    									printf(" Please enter a smaller amount, funds not sufficient!\n");
    									scanf("%lf", &amt);
    								}
    				
    				balance = balance - amt;
    				
    				printf("Your current balance is $%.2f.\n\n", balance);
    				}
    	
    	else if (choice == 3)
    	
    			{
    				printf("Your current balance is $%.2f.\n", balance);
    				printf("Please enter the date.\n");
    				scanf("%d\n\n", &date);
    				
    				printf("So far this month:\n");
    				printf("Your current balance is $%.2f.\n", balance);				
    				printf("Your total number of credit transactions are %d\n", option1);
    				printf("Your total number of debit transactions are %d\n", option2);
    			}
    	
    	
    		}
    	
    	
    	while (choice == 4)
    		{
    			printf("Statement of Account for October, 2004\n\n");
    			printf("Your total number of credit transactions are %d\n", option1);
    			printf("Your total number of debit transactions are %d\n", option2);
    			printf("Interest Computed: $%.2lf\n", interest);
    			printf("Final Balance: $%.2lf\n\n", balance);
    			printf("Good Bye!");
                      return 0;
                          }
    it says i have a parse error before "{" its the one right up under where the "while" loop starts. Im not done with my equations (ex. intrest) so you dont have to pay that any attention i just need to have the do while loop format right.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, you started a do-while loop up top. How about finishing that one some place? That'd be a good start. Also, count your { } pairs and you'll see you've got some other problems. Good indentation helps. Yours is what we call "not good".

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

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    11
    What about my indentations now? Can you see what the problem is?

    Code:
    #include <stdio.h>
    #define interest = .02	
    int main ()
    
    {
        int date, choice, option1, option2;
    	double amt, balance= 2000.00;
    
    	do
    			{
    					printf(" Welcome to Moon Trust Bank!\n\n");
    					printf(" Please Choose From The Following Options.\n\n");
    					printf("   1)  Deposit Funds (Credit Transaction)\n");
    					printf("   2) Withdraw Funds ( Debit Transaction)\n");
    					printf("   3) Print Statement of Account\n"); 
    					printf("   4) Compute Interest and Exit the Program\n");
    					scanf("%d", &choice);
    	
    										if (choice == 1)
    	
    												{
    														printf("Your current balance is $%.2f.\n", balance);
    														printf("Please enter the date.\n");
    														scanf("%d", &date);
    				
    														printf("Please enter the amount you wish to deposit into your account.\n");
    														scanf("%lf", &amt);
    				
    														balance = balance + amt;
    				
    														printf("Your current balance is $%.2f.\n\n", balance);		
    
    												}
    	
    										
    										else if (choice == 2)
    	
    												{
    														printf("Your current balance is $%.2f.\n", balance);
    														printf("Please enter the date.\n");
    														scanf("%d", &date);
    				
    														printf("Please enter the amount you wish to withdraw from your account.\n");
    														scanf("%lf", &amt);
    					
    															if (amt > balance)
    																{
    																	printf(" Please enter a smaller amount, funds not sufficient!\n");
    																	scanf("%lf", &amt);
    																}
    				
    														balance = balance - amt;
    				
    														printf("Your current balance is $%.2f.\n\n", balance);
    												}
    	
    										else if (choice == 3)
    	
    												{
    														printf("Your current balance is $%.2f.\n", balance);
    														printf("Please enter the date.\n");
    														scanf("%d\n\n", &date);
    				
    														printf("So far this month:\n");
    														printf("Your current balance is $%.2f.\n", balance);				
    														printf("Your total number of credit transactions are %d\n", option1);
    														printf("Your total number of debit transactions are %d\n", option2);
    												}
    	
    	
    			}
    	
    			
    									while (choice == 4)
    												{
    														printf("Statement of Account for October, 2004\n\n");/*
    														printf("Your total number of credit transactions are %d\n", option1);
    														printf("Your total number of debit transactions are %d\n", option2);
    														printf("Interest Computed: $%.2lf\n", interest);
    														printf("Final Balance: $%.2lf\n\n", balance);
    														printf("Good Bye!");*/
    			
    														return 0;
    												}
    			
    			
    }

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Identation is still not good...After a conditional (if, while) the code should be tabbed over ONCE. The brackets should be the same indentation as the conditional...like so

    Code:
    while(true)
    {
        do some stuff
        do some more stuff
        if (more stuff)
        {
            more more more
        }
    }
    I am not scrolling horizontally to read anything

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For forum posting, replace each tab with four spaces. That seems to work well.

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

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    11
    HOW ABOUT THIS?

    Code:
    #include <stdio.h>
    #define interest = .02	
    int main ()
    
    {
        int date, choice, option1, option2;
    	double amt, balance= 2000.00;
    
    	do{
    	
    		printf(" Welcome to Moon Trust Bank!\n\n");
    		printf(" Please Choose From The Following Options.\n\n");
    		printf("   1)  Deposit Funds (Credit Transaction)\n");
    		printf("   2) Withdraw Funds ( Debit Transaction)\n");
    		printf("   3) Print Statement of Account\n"); 
    		printf("   4) Compute Interest and Exit the Program\n");
    		scanf("%d", &choice);
    	
    			if (choice == 1)
                	{
    				printf("Your current balance is $%.2f.\n", balance);
    				printf("Please enter the date.\n");
    				scanf("%d", &date);
    				printf("Please enter the amount you wish to deposit into your account.\n");
    				scanf("%lf", &amt);
    				
    				balance = balance + amt;
    				
    				printf("Your current balance is $%.2f.\n\n", balance);		
    
    				}
    	
    										
    			else if (choice == 2)	
    				{
    				printf("Your current balance is $%.2f.\n", balance);
    				printf("Please enter the date.\n");
    				scanf("%d", &date);
    				
    				printf("Please enter the amount you wish to withdraw from your account.\n");
    				scanf("%lf", &amt);
    					
    				if (amt > balance)
    					{
    					printf(" Please enter a smaller amount, funds not sufficient!\n");
    					scanf("%lf", &amt);
    					}
    				
    				balance = balance - amt;
    				
    				printf("Your current balance is $%.2f.\n\n", balance);
    					}
    	
    			else if (choice == 3)
    					{
    					printf("Your current balance is $%.2f.\n", balance);
    					printf("Please enter the date.\n");
    					scanf("%d\n\n", &date);
    					printf("So far this month:\n");
    					printf("Your current balance is $%.2f.\n", balance);				
    					printf("Your total number of credit transactions are %d\n", option1);
    					printf("Your total number of debit transactions are %d\n", option2);
    					}
    	
    	
    			}	
    			
    			while (choice == 4)
    					{
    					printf("Statement of Account for October, 2004\n\n");/*
    					printf("Your total number of credit transactions are %d\n", option1);
    					printf("Your total number of debit transactions are %d\n", option2);
    					printf("Interest Computed: $%.2lf\n", interest);
    					printf("Final Balance: $%.2lf\n\n", balance);
    					printf("Good Bye!");*/
    					return 0;
    					}
    			
    			
    }

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    You're still not getting it...
    Code:
    #include <stdio.h>
    #define interest = .02
    int main ()
    {
        int date, choice, option1, option2;
        double amt, balance= 2000.00;
    
        do{
            printf(" Welcome to Moon Trust Bank!\n\n");
            printf(" Please Choose From The Following Options.\n\n");
            printf("   1)  Deposit Funds (Credit Transaction)\n");
            printf("   2) Withdraw Funds ( Debit Transaction)\n");
            printf("   3) Print Statement of Account\n"); 
            printf("   4) Compute Interest and Exit the Program\n");
            scanf("%d", &choice);
            if (choice == 1){
                printf("Your current balance is $%.2f.\n", balance);
                printf("Please enter the date.\n");
                scanf("%d", &date);
                printf("Please enter the amount you wish to deposit into your account.\n");
                scanf("%lf", &amt);
                balance = balance + amt;
                printf("Your current balance is $%.2f.\n\n", balance);
            }
            else if (choice == 2){
                printf("Your current balance is $%.2f.\n", balance);
                printf("Please enter the date.\n");
                scanf("%d", &date);
                printf("Please enter the amount you wish to withdraw from your account.\n");
                scanf("%lf", &amt);
                if (amt > balance){
                    printf(" Please enter a smaller amount, funds not sufficient!\n");
                    scanf("%lf", &amt);
                }
                balance = balance - amt;
                printf("Your current balance is $%.2f.\n\n", balance);
            }
            else if (choice == 3){
                printf("Your current balance is $%.2f.\n", balance);
                printf("Please enter the date.\n");
                scanf("%d\n\n", &date);
                printf("So far this month:\n");
                printf("Your current balance is $%.2f.\n", balance);
                printf("Your total number of credit transactions are %d\n", option1);
                printf("Your total number of debit transactions are %d\n", option2);
            }
        }
        while (choice == 4)//here's your problem... does the while belong to the do-while, or it's alone??
        {
            printf("Statement of Account for October, 2004\n\n");
            printf("Your total number of credit transactions are %d\n", option1);
            printf("Your total number of debit transactions are %d\n", option2);
            printf("Interest Computed: $%.2lf\n", interest);
            printf("Final Balance: $%.2lf\n\n", balance);
            printf("Good Bye!");
            return 0;
        }
    //end bracket missing
    Now that the code IS correctly idented try to understand it

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    the 'do' does not have a 'while'

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    11
    "xErath" The While does belong to the do-while.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    11
    Someone Please Help Me!!!!!

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    2 spaces works better. I dislike the default 4 spaces in MSVC and MSVC.net. Also 4 spaces on the board tends to make it into like 8 spaces, even with tabs not being inserted.

    Make sure in your options you have your compiler set to actually insert spaces instead of tabs and that should fix your posting problem, although not all of them. Best way to transfer code from the dev environment to the board is to copy the code, paste into notepad, manually touch it up, copy, and paste in reply window on the board. Seems to work just fine.

    As for your other problems...well your main function is just too doggone big. Try to follow this rule. One function=one task. One function should do one task and do it well. When you have one function doing a billion things it quickly becomes impossible to debug your code because you will be lost and, as evidenced by this thread, others will too.
    Last edited by VirtualAce; 10-06-2004 at 08:31 AM.

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Ok, Mack, what your missing is that when you place a do...while(), the while at the end of the do ends the loop. What you have is a while inside a do..while, and the do...while doesn't have a while.

    a do...while looks like this...
    Code:
    do {
        some suff
        some more stuff
    } while (true);
    What you have is
    Code:
    do {
        some stuff
        some stuff
        while (choice == 4) {
            some stuff
        }
    } while(true);
    Instead, you have a while, which itself has a block of code. This means that your do does not have a closing while.

    Your missing whats in red.

    I hope thats clearer...

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by xErath
    You're still not getting it...
    Code:
    int main ()
    {
        int date, choice, option1, option2;
        double amt, balance= 2000.00;
    
        do{
            printf(" Welcome to Moon Trust Bank!\n\n");
            ...snip snip...
        }
        while (choice == 4)//here's your problem... does the while belong to the do-while, or it's alone??
        {
            printf("Statement of Account for October, 2004\n\n");
            ...snip snip...
            return 0;
        }
    //end bracket missing
    Now that the code IS correctly idented try to understand it
    SEE!? It's as I said:
    1) COUNT YOUR { } PAIRS! If you had actually listened to me, you'd be done by now.
    2) Look at your do-while loop.
    a) You either have a correct do-while loop, in which case, the last pair of braces is pointless.
    b) You are missing a while loop.

    It's one or the other. Make up your mind. Also, try listening to what people have been telling you. What's the point of asking for help if you aren't going to pay attention to it?

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

  14. #14
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Mackology101
    "xErath" The While does belong to the do-while.
    ...
    Don't mock me. I know that well. You seem not to know what a while is.
    EDIT:
    The choice == 4 statment should be in another if inside the do-while, and it you be easier to have a while(1){}.
    Last edited by xErath; 10-06-2004 at 01:48 PM.

  15. #15
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by quzah
    SEE!? It's as I said:
    1) COUNT YOUR { } PAIRS! If you had actually listened to me, you'd be done by now.
    2) Look at your do-while loop.
    (....................)
    5 seconds were enough to spare us all time...
    Mackology next time please make some effort trying to understand what you're doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. For loop takes time
    By Shakti in forum C++ Programming
    Replies: 12
    Last Post: 10-14-2004, 08:25 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM