Thread: A simple problem

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    71

    A simple problem

    Ok guys this one is a lot like the bank program i had to write. it even has the same statements, just new things are added. in this program i have to prompt the user what month it is (1-12). then state how many days are in that month. that i got down. then i have to prompt the user to enter a valid day (based on the month). this is where i am having a problem. the problem is i have to prompt the user if he enters an invalid day, and make him enter a valid one. the problem i am having with this is i dont know how to get the program to return if the day is invalid. then lets say i enter 50 for the day. the new day will be set at 50.... here is the code i haave so far

    Code:
    #include <stdio.h>
    #define ANNUM .035
    
    int main(){
        
        int month, days_in_month, day=1;
        int count_credit=0, count_debit=0;
        int option;
        float balance = 2000;
        float deposit, total_one, total_d;
        float withdraw, total_w, total_two;
        float min_bal=balance;
        float interest, final;
        
        printf("Enter the transaction month (1-12):\t");
        scanf("&#37;d", &month);
        
      
        switch(month){
                      case 1: 
                      case 3: 
                      case 5: 
                      case 7: 
                      case 8: 
                      case 10:
                      case 12:
                           days_in_month=31; 
                           printf("Number of days in this month:%d\t\n", days_in_month);
                                break;
                      case 4:
                      case 6:
                      case 9:
                      case 11:
                           days_in_month=30;
                           printf("Number of days in this month:%d\t\n", days_in_month);
                                break;
                      case 2:
                           days_in_month=28;
                           printf("Number of days in this month:%d\t\n", days_in_month);
                                break;
                      default:
                              printf("Invalid selection.\n"); 
                              break;
                              }
                      
                      printf("\nYour balance is: $%.2f\n\n", balance);
    
                      while(option!=4){
                              //the list of transactions
        printf("Transaction options: \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 \n");
        printf("Please indicate your option:\t");
        scanf("%d", &option);                      
                             //if option one was selected
           if(option==1){
                      printf("Please enter a valid date from %d to %d\t", day, days_in_month);
                      scanf("%d", &day);
                      
                      if(day<=days_in_month){                  
                      printf("\n How much do you want to deposit?\t");
                      scanf("%f", &deposit);
                      balance+=deposit;
                      printf("Your balance is:\t $%.2f\n\n", balance);
                      count_credit++; // counts credit transactions
                         
                         }
    
                      else
                      printf("\nPlease enter a valid date from %d to %d\t", day, days_in_month);            
                         }
                      //end of option one
    
            
            else if (option==2){  //if option 2 was selected
                      printf("Please enter a valid date from %d to %d\t\n", day, days_in_month);
                      scanf("%d", &day); 
                      
                      if(day<=days_in_month){
                      printf("\n How much do you want to withdraw?\t");
                      scanf("%f",&withdraw);
                      balance-=withdraw;                                              
                      printf("Your current balance is:\t $%.2f\n\n", balance);
                         if ( balance < min_bal ) {
                         min_bal = balance;
                                                  }        
    
                                            }
                           
                     
            count_debit++; //counts debit transactions                   
                               }//end of option two
                               
           else if (option==3){  //if option 3 was selected
                           printf("\n Current balance:\t $%.2f\n\n", balance);
                           printf("Minimum balance: \t $%.2f\n\n", min_bal);    
                               }//end of option three
    
                               }//end of while statement
              
             
             
        //option 4     
    
                                                         
                         interest=min_bal*(ANNUM/12); //computation of the interest
             final=interest+balance;
             
                        printf("\nStatement of Account:\n\n");
                               printf("Credit Transactions: %d\n", count_credit);
                               printf("Debit Transactions: %d\n", count_debit);
                               printf("Current Balance:\t $%.2f \n", balance);
                               printf("Minimum Balance:\t $%.2f \n", min_bal);
                               printf("Interest Computed:\t $%.2f \n", interest);
                               printf("Final Balance:\t $%.2f \n", final);
                               printf("\nGood Bye! \n");
                               
                               
        system ("pause");
        return 0;
    }
    Last edited by vidioholic; 10-12-2007 at 06:54 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use a while loop, like
    Code:
    do {
      printf("foo");
      scanf("&#37;d",&foo);
      if ( foo == 1 ) {
        inputIsValid = 1;
      }
    } while ( ! inputIsValid );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    if i do something like that, it gets stuck in the while loop. if i choose option one and day is invalid, i want it to go back to the top of option one.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I don't suppose you know how to write functions yet?
    Your main() is getting rather large, and your poor formatting isn't helping either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    i don't really know a lot about functions. ive only been programing for about a month or two. this is only the third program ive ever written, not including the typical "hello world".

    so what can i do to make this program go back to the begining of the if statement or print out the right command? im playing around with while loops right now.... i have to get this done now because this program is due today at midnight! jus give me an idea of what i can do.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    ok i actually figured some of it out. i can get the program to print out a statement if the date typed in is invalid
    Code:
     if(option==1){
           printf("Please enter a valid date from &#37;d to %d\t", day, days_in_month);
           scanf("%d", &newday); 
                      while(newday > days_in_month || newday < day){
                                   printf("Please enter a valid date from %d to %d\t", day, days_in_month);
                                   scanf("%d", &newday);
                                   }
                      
    
                      printf("\n How much do you want to deposit?\t");
                      scanf("%f", &deposit);
                      balance+=deposit;
                      printf("Your balance is:\t $%.2f\n\n", balance);
                      count_credit++; // counts credit transactions
                                                                
                          day=newday;              
                      }
    im narrowing it down right now. i also have to make the program print out a statement if the user withdraws too much money.
    Last edited by vidioholic; 10-13-2007 at 03:12 PM.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    hey you guys im getting stuck on something. if the user withdraws too much money in this program, i need it to prompt the user that they do not have enough money and their current balance is only such-and-such. then it has to prompt the user again of how much they want to withdraw. i cant figure out how to do this completely. this is the code i am working on

    Code:
     else if (option==2){  //if option 2 was selected
                      printf("Please enter a valid date from &#37;d to %d\t", day, days_in_month);
                      scanf("%d", &newday); 
                    
                       while(newday > days_in_month || newday <day){
                                   printf("Please enter a valid date from %d to %d\t", day, days_in_month);
                                   scanf("%d", &newday);
                                   }
                      
                      printf("\n How much do you want to withdraw?\t");
                      scanf("%f",&withdraw);
                      current_bal=balance;
                      current_bal-=withdraw;    
                      while(current_bal<0){
                                   printf("%f\n", current_bal);
                                   printf("Sorry, you do not have that much money.\n");
                                   printf("Your current balance is just:\t%.2f\n", balance);
                                   printf("\n How much do you want to withdraw?\t");
                                   scanf("%d", &withdraw);
                                   current_bal=balance;
                                   current_bal-=withdraw;
                                   printf("%f\n", current_bal);
                                   }
                                                             
                      printf("Your current balance is:\t $%.2f\n\n", balance);
                         if ( balance < min_bal ) {
                         min_bal = balance;
                                                  }        
                      count_debit++; //counts debit transactions                   
                      day=newday;        
                               
                               }//end of option two
    when the while loop gets to the statment current_bal=balance, the current_bal becomes possitive again and kicks out of the loop... what should i do?

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    ok nevermind... i did it :-)
    check out the program see if theres anything that i missed or sumthing. it runs nicely for me.

    Code:
    #include <stdio.h>
    #define ANNUM .035
    
    int main(){
        
        int month, days_in_month, day=1;
        int count_credit=0, count_debit=0;
        int newday;
        int option;
        float current_bal, new_balance;
        float balance = 2000;
        float deposit, total_one, total_d;
        float withdraw, total_w, total_two;
        float min_bal=balance;
        float interest, final;
        
            
        printf("Enter the transaction month (1-12):\t");
        scanf("&#37;d", &month);
        
                while(month>12 || month <1){
                printf("Please enter a valid month (1-12):\t");
                scanf("%d", &month);
                               } 
    
        switch(month){
                      case 1: 
                      case 3: 
                      case 5: 
                      case 7: 
                      case 8: 
                      case 10:
                      case 12:
                           days_in_month=31; 
                           printf("Number of days in this month:%d\t\n", days_in_month);
                                break;
                      case 4:
                      case 6:
                      case 9:
                      case 11:
                           days_in_month=30;
                           printf("Number of days in this month:%d\t\n", days_in_month);
                                break;
                      case 2:
                           days_in_month=28;
                           printf("Number of days in this month:%d\t\n", days_in_month);
                                break;
                                       
                              }
                      
                      printf("\nYour balance is: $%.2f\n\n", balance);
    
                      while(option!=4){
                              //the list of transactions
        printf("Transaction options: \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 \n");
        printf("Please indicate your option:\t");
        scanf("%d", &option);                      
                             //if option one was selected
              
           if(option==1){
           printf("Please enter a valid date from %d to %d\t", day, days_in_month);
           scanf("%d", &newday); 
                      while(newday > days_in_month || newday <day){
                                   printf("Please enter a valid date from %d to %d\t", day, days_in_month);
                                   scanf("%d", &newday);
                                   }
    
    
                      printf("\n How much do you want to deposit?\t");
                      scanf("%f", &deposit);
                      balance+=deposit;
                      printf("Your balance is:\t $%.2f\n\n", balance);
                      count_credit++; // counts credit transactions
                          
                      day=newday;
                                         
                      }
                      //end of option one
    
            
            else if (option==2){  //if option 2 was selected
                      printf("Please enter a valid date from %d to %d\t", day, days_in_month);
                      scanf("%d", &newday); 
                    
                       while(newday > days_in_month || newday <day){
                                   printf("Please enter a valid date from %d to %d\t", day, days_in_month);
                                   scanf("%d", &newday);
                                   }
                      
                      printf("\n How much do you want to withdraw?\t");
                      scanf("%f",&withdraw);
                      
                      current_bal=balance;
                      current_bal-=withdraw;    
                      while(current_bal<0){
                                   new_balance=balance;
                                   printf("Sorry, you do not have that much money.\n");
                                   printf("Your current balance is just:\t%.2f\n", balance);
                                   printf("\n How much do you want to withdraw?\t");
                                   scanf("%f",&withdraw);
                                   new_balance-=withdraw;  
                                   current_bal=new_balance;
                                   }
                                   balance=current_bal;                          
                      printf("Your current balance is:\t $%.2f\n\n", balance);
                         if ( balance < min_bal ) {
                         min_bal = balance;
                                                  }        
                      count_debit++; //counts debit transactions                   
                      day=newday;        
                               
                               }//end of option two
                               
           else if (option==3){  //if option 3 was selected
                          
                          printf("Please enter a valid date from %d to %d\t", day, days_in_month);
                          scanf("%d", &newday); 
                    
                          while(newday > days_in_month || newday <day){
                                   printf("Please enter a valid date from %d to %d\t", day, days_in_month);
                                   scanf("%d", &newday);
                                   }
                          
                           printf("\n Current balance:\t $%.2f\n\n", balance);
                           printf("Minimum balance: \t $%.2f\n\n", min_bal);    
                           day=newday;    
                               
                               }//end of option three
    
                               }//end of while statement
              
             
             
        //option 4     
    
                                                         
                        interest=min_bal*(ANNUM/12); //computation of the interest
                        final=interest+balance;
             
                        printf("\nStatement of Account:\n\n");
                               printf("Credit Transactions: %d\n", count_credit);
                               printf("Debit Transactions: %d\n", count_debit);
                               printf("Current Balance:\t $%.2f \n", balance);
                               printf("Minimum Balance:\t $%.2f \n", min_bal);
                               printf("Interest Computed:\t $%.2f \n", interest);
                               printf("Final Balance:\t\t $%.2f \n", final);
                               printf("\nGood Bye! \n");
                               
                               
        system ("pause");
        return 0;
    }

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You might want to work on your indentation -- it's not very easy for me to read at the moment.

    Consider turning up your compiler warnings.
    Code:
    $ gcc -W -Wall -c vidioholic.c
    vidioholic.c: In function ‘main’:
    vidioholic.c:154: warning: implicit declaration of function ‘system’
    vidioholic.c:13: warning: unused variable ‘total_two’
    vidioholic.c:13: warning: unused variable ‘total_w’
    vidioholic.c:12: warning: unused variable ‘total_d’
    vidioholic.c:12: warning: unused variable ‘total_one’
    $
    You can get rid of those unused variables, and you should be including <stdlib.h> to use system() -- which I wouldn't advise, in fact. system() is unportable, unsafe, and slow. See the FAQ for alternatives.

    Code:
                      case 2:
                           days_in_month=28;
                           printf("Number of days in this month:&#37;d\t\n", days_in_month);
                                break;
    What about leap years?

    Also, you don't need those identical printf()s in each and every selection of case statements in that switch. You could just put the printf() afterwards.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    well this is a beginers c programing class. it isnt into depth such as leap year. its based on the year 2007. the print statements was based on what the prof said to do. i guess i didnt realize that i added extra int variables. i was messin with the program a lot so i added a lot of things. thanx for showin me that though! it really helped i deleted it. the program is due at 3 this morning. so i caught it just in time
    Last edited by vidioholic; 10-14-2007 at 12:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM