使用数组保存和打印数据时出现问题

时间:2015-11-24 18:14:36

标签: c arrays multidimensional-array

将数据保存到数组中时遇到了一些麻烦。大多数代码都按要求工作,但我遇到的问题是将数据(例如存款,取款,转移)保存到数组中,以便最后打印出来。出现了两个主要问题 1.未输入任何内容(完全可以接受),然后输出随机数字。 2.如果在循环期间输入了某些内容,例如存款,然后用户决定在循环结束前输入另一个存款,则输出将只是最后一个条目(而我希望它是两个单独的输出) 。我似乎无法想出绕过这些问题的方法,所以感谢任何帮助!

#include <stdio.h>
#include <stdlib.h>
//global variable for the overdraft
float OVERDRAFT = -1000;


//function to determine interest rate for users deposit
//return the interest rate to be used in the total() function
float interest(float money)
{
float int_rate;

if (money < 0 && money >= -100)
{
    int_rate = 1.2f;
}
else if (money >= 0 && money < 100)
{
    int_rate = 1.1f;
}
else if (money >= 100 && money < 200)
{
    int_rate = 1.5f;
}
else if (money >= 200 && money < 500)
{
    int_rate = 1.8f;
}
else if (money >= 500)
{
    int_rate = 1.9f;
}
else if (money < -100 && money >= -500)
{
    int_rate = 1.4f;
}
else if (money < -500 && money >= -1000)
{
    int_rate = 1.8f;
}
return int_rate;
}


//function to calculate final amount with interest applied
//uses previous interest() function to calculate final amount by multiplying them together
//if the amount is less than -1000 (overdraft) then the OVERDRAFT value of -1000 is returned
//else, returns the normal amount
float total(float x)
{
float int_func;
int_func = interest(x);
float amount = x * int_func;
if (amount < OVERDRAFT)
{
    return OVERDRAFT;
}
else
{
    return amount;
}

}


//function that checks if user enters an amount greater than 0
//returns an integer dependent on this
//this is used in main function to determine if money can be added to the account (0 means yes, -1 means no)
float add_money(float *Account, float Amount)
{
if (Amount > 0)
{
    return 0;
}
else
{
    return -1;
}
}
float take_money(float *Account, float Amount)
{
if (Amount > 0)
{
    return 0;
}
else
{
    return -1;
}
}


//function to be used for the transfer section in the main function
//finds out if users input (multiplied by interest) is positive
//if it is, 0 is returned which can be used later
float transfer(float y)
{
float int_func;
int_func = interest(y);
float amount = y * int_func;
if (amount >= 0)
{
    return 0;
}
else
{
    return -1;
}

}


//creates a sentinel while loop so that user can exit it if they press 0, or reloops if they enter any other number
float main(void)
{
float sentinel = 1;
while (sentinel != 0)
{
    //asks the user to enter amount they wish to deposit in both main and savings accounts
    //asks the user how many months they want to simulate (3 months with a starting deposit of £50 will result in £55, £60.50, £66.55)
    float deposit, add, take, savings, tran;
    int count, amount, answer;
    float x, dep[51], withd[51];
    float m_to_s[51], s_to_m[51];

    printf("Initial deposit for your main account:\n");
    scanf_s("%f", &deposit);
    while (deposit < OVERDRAFT)
    {
        //while loop to make sure user enters positive number for main account
        printf("ERROR: You have an overdraft limit of \x9C%.0f.\nPlease enter a greater initial deposit:\n", (OVERDRAFT* -1));
        scanf_s("%f", &deposit);
    }
    printf("Initial deposit for your savings account:\n");
    scanf_s("%f", &savings);
    while (savings < 0)
    {
        //while loop to make sure user enters positive number for savings account
        printf("ERROR: Please enter a positive value:\n");
        scanf_s("%f", &savings);
    }
    printf("Amount of months to simulate:\n");
    scanf_s("%d", &amount);
    while (amount <= 0)
    {
        printf("ERROR: Please enter a value greater than zero:\n");
        scanf_s("%d", &amount);
    }

    //for loop with a switch statement to allow user to choose option from main menu
    //user can deposit money, withdraw money, transfer money, and move to the next month with interest applied from the menu
    for (count = 1; count <= amount; count++)
    {
        printf("\nPlease choose one of the below options:\n");
        printf("1: Deposit money (Main account only)\t");
        printf("2: Withdraw money (Main account only)\n");
        printf("3: Transfer money\t\t\t");
        printf("4: Balance next month (with interest)\n");
        scanf_s("%d", &answer);
        while (answer <= 0 || answer >= 5)
        {
            //while loop to ensure user chooses one of the 4 options
            printf("\nPlease choose one of the below options:\n");
            printf("1: Deposit money (Main account only)\t");
            printf("2: Withdraw money (Main account only)\n");
            printf("3: Transfer money\t\t\t");
            printf("4: Balance next month (with interest)\n");
            scanf_s("%d", &answer);
        }

        //main menu
        switch (answer)
        {
            //option 1 to deposit more money
            //adds money to the account if 0 was returned in add_money() function
            //errors given if -1 was returned in add_money() function
        case 1: printf("\nYou chose to make a deposit\n");
            printf("Amount to deposit:\n");
            scanf_s("%f", &dep[count]);
            add = add_money(&x, dep[count]);
            while (dep[count] <= 0)
            {
                //while loop to ensure user enters a value greater than 0
                printf("ERROR: Please enter a value greater than zero:\n");
                scanf_s("%f", &dep[count]);
            }
            if (dep[count] > 0)
            {

                deposit = deposit + dep[count];
                printf("Main account balance: \x9C%.2f.\n", deposit);
                //reduces count by 1 so that it outputs correctly at end
                count -= 1;
            }
            else
            {
                printf("ERROR: You entered an invalid amount.\n");
                count -= 1;
            }break;



            //option 2 to withdraw money
            //reduces count by 1 so that the program doesn't end sooner than the user wanted
        case 2: printf("\nYou chose to make a withdrawal\n");
            printf("Amount to withdraw:\n");
            scanf_s("%f", &withd[count]);
            take = take_money(&x, withd[count]);
            while (withd[count] <= 0)
            {
                //while loop to ensure user enters value greater than 0
                printf("ERROR: Please enter a value greater than zero:\n");
                scanf_s("%f", &withd[count]);
            }

            if (withd[count] > 0)
            {
                //sets deposit variable to a new value according to the users input (may be reverted back in the if statement if it goes over overdraft)
                deposit = deposit - withd[count];
                if (deposit < OVERDRAFT)
                {
                    //reverts the recentl deposit variable as it will not be possible as the user will go over the overdraft
                    printf("ERROR: You can't withdraw that much as you will go over your overdraft limit\n");
                    deposit = deposit + withd[count];
                    count -= 1;
                    break;
                }
                else if (deposit > OVERDRAFT)
                {
                    printf("Main account balance: \x9C%.2f.\n", deposit);
                    count -= 1;
                    break;
                }
            }
            else
            {
                printf("ERROR: You entered an invalid amount\n");
                count -= 1;
            }break;

            //option 3 to transfer money from one account to another
        case 3: printf("\nYou chose to transfer money\n");
            printf("Please choose an option:\n");
            printf("1: Transfer from Main account to Savings account\n");
            printf("2: Transfer from Savings account to Main account\n");
            int option;
            scanf_s("%d", &option);
            while (option < 1 || option > 2)
            {
                //while loop to ensure user chooses one of the two options
                printf("Please choose an option:\n");
                printf("1: Transfer from Main account to Savings account\n");
                printf("2: Transfer from Savings account to Main account\n");
                scanf_s("%d", &option);
            }
            //switch inside a switch
            switch (option)
            {
            case 1: printf("\nYou chose to transfer money from your Main account to your Savings account\n");
                printf("Please enter how much to transfer:\n");
                scanf_s("%f", &m_to_s[count]);
                tran = transfer(m_to_s[count]);
                while (m_to_s[count] <= 0)
                {
                    //while loop to ensure user enters a value greater than 0
                    printf("ERROR: Please enter a value greater than zero:\n");
                    scanf_s("%f", &m_to_s[count]);
                }

                //if statement that will be run if user enters value greater than 0
                if (m_to_s[count] > 0)
                {
                    savings = savings + m_to_s[count];
                    deposit = deposit - m_to_s[count];
                    if (deposit >= OVERDRAFT)
                    {
                        //prints out both balances of accounts after the transfer
                        printf("\nMain account balance %.2f\n", deposit);
                        printf("Savings account balance: %.2f\n", savings);
                        count -= 1;
                    }
                    else
                    {
                        printf("ERROR: You can't transfer that much as you don't have a large enough overdraft on your Main account\n\n");
                        //if user enters an incorrect amount (because overdraft is reached), variables are restored so that end output is correct
                        savings = savings - m_to_s[count];
                        deposit = deposit + m_to_s[count];
                        count -= 1;
                    }

                }break;

            //transfer money from savings to main
            case 2: printf("\nYou chose to transfer money from your Savings Account to your Main account\n");
                printf("Please enter how much to transfer:\n");

                scanf_s("%f", &s_to_m[count]);
                tran = transfer(s_to_m[count]);
                while (s_to_m[count] <= 0)
                {
                    //while loop so ensure user enters a value greater than 0
                    printf("ERROR: Please enter a value greater than zero:\n");
                    scanf_s("%f", &s_to_m[count]);
                }

                //if statement that will be run if user enters a value greater than 0
                if (s_to_m[count] > 0)
                {
                    //adjusts balances using users entered value (these variables may be reverted in the else statement if the savings go lower than 0 as there is no overdraft)
                    savings = savings - s_to_m[count];
                    deposit = deposit + s_to_m[count];
                    if (savings >= 0)
                    {
                        //prints out current balances
                        printf("\nMain account balance: %.2f\n", deposit);
                        printf("Savings account balance: %.2f\n", savings);
                        count -= 1;

                    }
                    else
                    {
                        //error if user tries to transfer too much money out of their savings account because there is no overdraft
                        printf("ERROR: You can't transfer that much as you don't have an overdraft on your Savings account\n\n");
                        savings = savings + s_to_m[count];
                        deposit = deposit - s_to_m[count];
                        count -= 1;
                    }break;
                }
                else
                {
                    printf("ERROR\n\n");
                    count -= 1;
                }break;

            }break;

            //option 4 to move onto the next month (with interest applied to both accounts)
        case 4:
            deposit = total(deposit);
            savings = total(savings);
            printf("\nYou chose to show account balances (with interest) for the next month\n");
            printf("Month %d Main account balance: \x9C%.2f.\n", count, deposit);
            printf("Month %d Savings account balance: \x9C%.2f\n", count, savings);

        }
    }

    for (count = 1; count <= amount; count++)
    {
        //sting array
        //prints out transactions
        //NOT WORKING CORRECTLY

        printf("\nDeposit %d: %.2f\n", count, dep[count]);
        printf("Withdrawal %d: %.2f\n", count, withd[count]);
        printf("Transfer Main to Savings %d: %.2f\n", count, m_to_s[count]);
        printf("Transfer Savings to Main %d: %.2f\n", count, s_to_m[count]);
    }


    //if user enters 0, while loop is broken
    //if user enter anything other than 0, loop reloops
    printf("\nTo terminate program, enter '0'.\n");
    printf("To run program again, enter any other number.\n");
    scanf_s("%f", &sentinel);
}
return 0;
}

1 个答案:

答案 0 :(得分:1)

代码很长,很难通过,但如果我确实理解它,你需要:

  1. 初始化所有变量。将它们设置为零或适合您的东西。
  2. 您正在使用dep[count],但之后您总是count -= 1;。试着看一下。