简体   繁体   中英

Take multiple separate inputs from lines read in from file in C

Basically I have a few problems.

The problem I am trying to solve involves a "banking program" that gets its info from a text file. It reads the first five lines of code (which are all floats) and uses each as the starting balance.

Next, the following 1-5 lines of code each begin with a single character indicating the account that it affects. Next is a single letter indicating either withdraw or deposit, etc. Following that is the amount that will be used in the action. I have to use multiple functions.

Here's what I have so far, I know some spaces have been left in pseudocode while I'm working on it.

  float balance();
float withdraw();
float deposit();
float update();

#define INTEREST 3.5;
int main(){

//initializing variables
int count =0,count2=0,acctNum=1,acct1,acct2,acct3,acct4,acct5,i=0;
float orgBalance, balance;
char activity,B,W,U,D;

//opening files
FILE *input;
input = fopen("bankfile.txt","r");

//error checking that file opened correctly
if (input == NULL){
    fprintf(stderr, "Can't open input file!\n");
    exit(1);
}   
printf("Account          Balance\n");
printf("------------------------\n");
while(count<5){
    fscanf(input,"%f",&orgBalance);

    //printf("%d               %.2f\n",acctNum,orgBalance);
    //acctNum++;
    i++;
    count++;
    printf("%d balance is %.2f\n",i,orgBalance);
}
printf("------------------------\n");


while(fscanf(input, "%d ",&i)!=EOF){
//while(count2<5)
    fgetc(i);   


    switch (activity){
        case 'B':
            balance;
            ;
            break;
        case 'W':
            withdraw;
            ;
            break;
        case 'D':
            deposit;
            ;
            break;
        case'U':
            update;
            ;
            break;


    }
    count2++;
}


system("pause");
return 0;
}


 float balance(){
    float bal;
    return bal; 
  }
  float withdraw(){
    float bal1, bal2;
    if(bal1>bal2){
        return bal1 - bal2;
    }
    else{
        printf("Sorry, you can't withdraw that much");
        return bal1; 


}
}
float deposit(){
    float bal1,bal2;
    return bal1 += bal2;
}
float update(){
    float bal;
    return bal*= INTEREST;

}

these are wrong

switch (activity){
    case 'B':
        balance;
        ;

....

you mean

switch (activity){
    case 'B':
        balance(); <<<======
        ;

You can extract the accounts like this using an array:

float ogAcnts[5] = {0,0,0,0,0}; // original account balances initialized to zero
fscanf(input, "%f%f%f%f%f", &ogAcnts[0], &ogAcnts[1], &ogAcnts[2], &ogAcnts[3], &ogAcnts[4]);

You can have a second array with the new balances if you need to keep the original balances for future reference:

float newAcnts[5] = {0,0,0,0,0}; // initialized to zero, could also be replaced with the original balances

To display the original account balances:

// you can fill in the rest
for(int i=0; i<=4; i++)
{
    printf("%d balance is %.2f\n",i,ogAcnts[i]);
}  

You can skip the account balances in the file when scanning, like this:

fscanf(input+5, %d%c%f, &acountNum, &transactionType, &amount); // note these variables don't exist in your code but it's an example of how you could use them

You can then follow this same method to get the remaining transactions. Get the transaction parameters and act on them. Use the arrays to access the balances instead of the file. The first account will be element 0 and ends with account number 5 being in index 4 (zero based). So you will have to follow this pattern. I say that because of how the transactions are labeled with the account number (either starts with 0 or 1). if you are using zero based then it makes it easier to access that account balance in the array. Like this example "2 W 200" if account #2 has 400 in it and you are starting with the first account being account 0, you can pass the account number and amount to your transaction functions like this:

...
newAcnts[accountNum] = ogAcnts[accountNum]-amount; // withdraw example using the variables from above

Note: It would probably be better to just have one running balance for each account unless (like mentioned) you need to know what it was before you started. Because for any additional transactions on the same account you'd want to use newAcnts[accountNum] to have the correct balance of the account. Or before starting any transactions copy the original balances into the new accounts array and only do your transactions on the newAcnts array. Hopefully that makes sense :)

newAcnts[accountNum] = newAcnts[accountNum]-amount; // withdraw example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM