Thread: Need help

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    8

    Need help

    I need help figuring out how to get this to calculate and print the number of times the investment and donate run. I've gotten everything else to run right just hit a road block with that one little part.

    Code:
    //Pre-processor
     #include <stdio.h>
    
      //Main Function
       int main(){
           //Variable declaration
           int type,num_donate , num_invest;
           float total, initial_balance, donate, invest, new_balance,t1,t2;
           printf("Welcome!\n");
           //user input
            printf("What is the initial balance of the fund?\n");
            new_balance += initial_balance;
            scanf("%f", &initial_balance);
    
           //print menu for user
           printf("What would you like to do?:\n");
           printf("\t1 - Make a donation\n");
           printf("\t2 - Make an investment\n");
           printf("\t3 - Print balance of fund\n");
           printf("\t4 - Quit\n");
           printf("Which would you like to do?\n");
           scanf("%d", &type);
    
    
           // continue until user inputs 4 to quit
           while(type != 4){
              switch (type){
                 //Donation case
                 case 1:
                    printf("How much would you like to donate?\n");
                    scanf("%f", &donate);
                    t1=donate + t1;
                    break;
    
                //Investment case
                 case 2:
                    printf("How much would you like to invest?\n");
                    scanf("%f", &invest);
                    if(invest > donate)
                    printf("You cannot make an investment of that amount\n");
                    else
                    t2=invest - 2;
                    break;
    
                //Balance case
                case 3:
                    printf("The current balance is %.2f\n", t1 + initial_balance - t2);
                    printf("There have been %d donations and %d investments\n", num_donate, num_invest);
                    total+= t1;
    
                case 4:
                       break;
                    default:
                    printf("Sorry that was not a vaild option\n");
                    break;
                }//end switch
    
                //reprint menu
                 printf("What would you like to do?:\n");
                 printf("\t1 - Make a donation\n");
                 printf("\t2 - Make an investment\n");
                 printf("\t3 - Print balance of fund\n");
                 printf("\t4 - Quit\n");
                 printf("Which would you like to do?\n");
    
    
                 scanf("%d", &type);
           }
    
    
                 //end loop
                 printf("The final balance is %.2f\n",total=t1 + initial_balance - t2);
                 printf("There were %d donations and %d investments\n",num_donate, num_invest);
    
                //end main function
                   return 0;
           }

  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
    First, you need to improve your indentation.
    Indent style - Wikipedia, the free encyclopedia

    > new_balance += initial_balance;
    > scanf("%f", &initial_balance);
    1. You need to initialise your variables.
    2. You need to input things before doing calculations on them.

    t1 and t2 are meaningless names, can you think of anything better?
    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.

Popular pages Recent additions subscribe to a feed

Tags for this Thread