C程序在xcode中编译,但在终端中运行无限循环

时间:2016-12-09 04:51:48

标签: c xcode macos terminal systems-programming

我一直在研究一个系统编程类的组程序,它将运行某个银行的人会使用的函数。这些功能包括添加银行客户,客户的输出余额等。问题在于添加客户功能。

此函数将在终端和Putty中的无限循环中运行,但是当我在XCode中作为独立程序运行时,循环退出。我,我的小组成员和我们的教授都忽略了该计划有什么问题吗?

#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;
}

执行Makefile后输出的屏幕截图: screenshot

旁注:这个函数会写入我们传递的文件,这很好但我们仍然需要它来摆脱循环。

编辑:主要功能的代码:     #包括     #包括     #包括     #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;
}

1 个答案:

答案 0 :(得分:3)

您只分配一次选择。这应该可以解决您的问题:

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;