2

I've been working on a group program for a systems programming class which will run functions that someone at a bank would use. These functions include add bank customers, output balance of a customer, and so on. The problem lies with the add customer function.

This function will run in an infinite loop in terminal and Putty, but when I run it as a standalone program in XCode the loop quits. Is there anything wrong with the program that I, my group members AND our professor are overlooking?

#include <stdio.h>
#include <string.h>
#include "./bank.h"

void add(FILE *fp)
{
    int i=0;
    char firstName[20];
    char lastName[20];
    float camount;
    int prompt;
    float samount;
    float mamount;
    fseek(fp,0,2);
do

{
   printf("\nEnter first name: ");
   scanf("%s", firstName);
   strcpy(bank[i].firstName, firstName);

   printf("\nEnter last name: ");
   scanf("%s",lastName);
   strcpy(bank[i].lastName, lastName);

   printf("\nEnter checking account balance: ");
   scanf("%f", &camount);
   bank[i].checking = camount;

   printf("\nEnter savings account balance: ");
   scanf("%f", &samount);
   bank[i].savings = samount;

   printf("\nEnter money market account balance: ");
   scanf("%f", &mamount);
   bank[i].moneyMarket = mamount;

   fwrite(&bank[i],1, sizeof(struct BankInfo),fp);
   i++;

   printf("Enter 1 to enter another name and 0 to quit: ");
   scanf("%d", &prompt);
   printf("%d\n", prompt);

   } while(prompt == 1);

   fclose(fp);
   return;
}

Screenshot of output once Makefile is executed: screenshot

Sidenote: This function does write onto the file that we are passing which is great but we still need for it to break out of the loop.

EDIT: code for the main function: #include #include #include #include "bank.h" #include "definitions.h"

int main()
{
FILE *fp;
int selection;
fp=fopen("bankInfo.dat","ab+");

selection=menu();

while(selection !=6)
{
switch(selection)
{
  case 1:
       add(fp);
       break;

       case 2:
          //  outputBalance(fp);
            break;
      case 3:
            delete(fp);
            break;
      case 4:
            update(fp);
            break;
    case 5:
         // display(fp);
            break;
    case 6:
            exit(0);
            break;
    default:
            printf("Invalid selection\n");
            break;
    }
}
fclose(fp);
return 0;
}
mimiholli
  • 33
  • 7

1 Answers1

3

You are assigning selection only once. This should fix your issue:

while(true)
{
selection=menu();
switch(selection)
{
  case 1:
       add(fp);
       break;

       case 2:
          //  outputBalance(fp);
            break;
      case 3:
            delete(fp);
            break;
      case 4:
            update(fp);
            break;
    case 5:
         // display(fp);
            break;
    case 6:
            fclose(fp);
            exit(0);
            break;
    default:
            printf("Invalid selection\n");
            break;
    }
}
return 0;
Ari0nhh
  • 5,720
  • 3
  • 28
  • 33
  • Thank you! This has been driving us crazy all day! We asked 3 different professors and none of them could figure it out. – mimiholli Dec 09 '16 at 05:09
  • @ You are right of course. In fact, there is no need to check selection value at all in the loop condition. – Ari0nhh Dec 09 '16 at 05:16