为什么相同的循环逻辑(标红)在showcode最后可以用,但是openaccount不可以
本帖最后由 空羊羊 于 2020-12-22 19:02 编辑#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
void showcode(void);
void openaccount(int sequence);
struct Account{
char name;
char id;
char address;
char ccode;
char bcode;
char Dob;
int balance;
struct tm *Doo;
char accountnumber;
};
void main(void){
char option='\0', ch='\0', in='\0';
int count=0;
int sequence;
FILE *fp;
if((fp=fopen("Sequence.txt","r"))==NULL){
if((fp=fopen("Sequence.txt","w"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
fwrite(sequence, 150*sizeof(int), 1, fp);
}
else{
fread(sequence, 150*sizeof(int), 1, fp);
}
fclose(fp);
do{
printf("1) Show city code and branch code\n");
printf("2) Open an account\n");
printf("3) Show account details\n");
printf("4) Show list of accounts\n");
printf("5) Deposit in an account\n");
printf("6) Withdraw from an account\n");
printf("7) Transfer money\n");
printf("8) Transaction details\n");
printf("9) Close an account\n");
printf("10)Quit\n");
printf("\n");
printf("Option: ");
count=0;
option='\0', ch='\0', in='\0';
while((in=getchar())!='\n'){
count++;
option=ch;
ch=in;
}
if((count==1)&&(ch=='0')){
ch='a';
}
if(count>2){
ch='a';
}
if(count==2){
if((option=='1')&&(ch=='0')){
ch='0';
}
else{
ch='a';
}
}
printf("\n");
switch(ch){
case '1': showcode(); break;
case '2': openaccount(sequence); break;
case '3': printf("3\n"); break;
case '4': printf("4\n"); break;
case '5': printf("5\n"); break;
case '6': printf("6\n");break;
case '7': printf("7\n");break;
case '8': printf("8\n");break;
case '9': printf("9\n");break;
case '0': printf("break\n");break;
default: printf("Unknown option!\n");
printf("\n");
}
}while(ch!='0');
exit(0);
}
void showcode(void){
FILE *fp;
char name1, name2, name3;
char code1, code2, code3;
char buffer, citycode, choice;
char *name, *code;
int len, rmd;
if((fp=fopen("CityBranchCodes_Leyang_Hu_20215416.txt", "r"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
printf("City\t\t\tcode\t\t\tCity\t\t\tcode\t\t\tCity\t\t\tcode\n");
printf("_____________________________________________________________________________________________________________________________\n");
while(fgets(buffer, 20, fp)!=NULL){
len=strlen(buffer);
buffer='\0';
code=strtok(buffer, "\t");
name=strtok(NULL, "\t");
if(strlen(code)==3){
rmd=(atoi(code))%3;
switch(rmd){
case 1:
strcpy(name1, name);
strcpy(code1, code);
break;
case 2:
strcpy(name2, name);
strcpy(code2, code);
break;
case 0: strcpy(name3, name);
strcpy(code3, code);
printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2, name3, code3);
break;
}
}
}
printf("%-10s\t\t%-10s\n", name1, code1);
printf("\n");
fseek(fp, 0, SEEK_SET);
printf("Enter the city code: ");
scanf("%s", citycode);
getchar();
printf("\n");
if((fp=fopen("CityBranchCodes_Leyang_Hu_20215416.txt", "r"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
while(fgets(buffer, 20, fp)!=NULL){
len=strlen(buffer);
buffer='\0';
code=strtok(buffer, "\t");
name=strtok(NULL, "\t");
if((strcmp(code, citycode))==0){
break;
}
}
printf("Branch name\t\tcode\t\t\tBranch name\t\tcode\t\t\tBranch name\t\tcode\n");
printf("_____________________________________________________________________________________________________________________________\n");
while(fgets(buffer, 20, fp)!=NULL){
len=strlen(buffer);
buffer='\0';
code=strtok(buffer, "\t");
name=strtok(NULL, "\t");
if(strlen(code)==3){
break;
}
rmd=(atoi(code))%3;
switch(rmd){
case 1:
strcpy(name1, name);
strcpy(code1, code);
break;
case 2:
strcpy(name2, name);
strcpy(code2, code);
break;
case 0: strcpy(name3, name);
strcpy(code3, code);
printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2, name3, code3);
break;
}
}
switch(rmd){
case 1:
printf("%-10s\t\t%-10s\n", name1, code1);
break;
case 2:
printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2);
break;
case 0:
break;
}
fclose(fp);
printf("\n");
printf("Do you want to see more city and branch codes (Y/N): ");
scanf("%c", &choice);
getchar();
if(choice=='Y'){
showcode();
}
printf("\n");
}
void openaccount(int sequence){
struct Account *account=(struct Account*)malloc(sizeof(struct Account));
int blc,i,j,number,len;
char ch, orig, seq;
FILE *fp, *ptr;
time_t t;
printf("Enter the name of the account holder: ");
while((ch=getchar())!='\n'){
account->name=ch;
i++;
}
printf("Enter the Id number of the account holder: ");
scanf("%s", account->id);
getchar();
printf("Enter the address of the account holder: ");
i=0;
while((ch=getchar())!='\n'){
account->address=ch;
i++;
}
printf("Enter city code: ");
scanf("%s", account->ccode);
getchar();
printf("Enter branch code: ");
scanf("%s", account->bcode);
getchar();
printf("Enter the Dob of the account holder: ");
scanf("%s", account->Dob);
getchar();
while(blc<1000){
printf("Enter the initial balance of the account (not less that 1000 RMB): ");
scanf("%d", &blc);
if(blc<1000){
printf("Initial deposit can't be less than 1000 RMB.\n");
}
}
account->balance=blc;
time(&t);
account->Doo=localtime(&t);
if((fp=fopen("Account.txt", "a"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
i=atoi(account->ccode);
j=atoi(account->bcode);
number=(sequence)+1;
sequence+=1;
if((ptr=fopen("Sequence.txt", "w"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
fwrite(sequence, 150*sizeof(int), 1, ptr);
fclose(ptr);
sprintf(orig, "%d", number);
len=strlen(orig);
i=0;
for(j=0;j<5;j++){
if(j>=(5-len)){
seq=orig;
i++;
}
else{
seq='0';
}
}
strcpy(account->accountnumber, account->ccode);
strcat(account->accountnumber, account->bcode);
strcat(account->accountnumber, seq);
account->accountnumber='\0';
printf("The account number is: %s\n", account->accountnumber);
fwrite(account, sizeof(struct Account), 1, fp);
free(account);
fclose(fp);
printf("\n");
printf("Do you want to open more accounts?(Y/N): ");
scanf("%c", &ch);
getchar();
if(ch=='Y'){
printf("\n");
openaccount(sequence);
}
printf("\n");
}
就是我每次在每个功能进行一次后在结尾询问是否要继续后, showcode的可以正确执行,但是openaccount的就不可以 解决了,就是直接在主程序菜单的switch的case 2里写一个do...while来控制是否进行循环 真的很疑惑,求助各位大佬! 本帖最后由 jackz007 于 2020-12-22 18:06 编辑
楼主,把你的帖子编辑一下,一定要把代码贴进代码框,要不然,你贴出来的代码会缺很多东西,看看你的代码,是不是在某个位置起变成斜体了,因为你的部分代码被作为字体及其它版面描述符而被吸收掉了。 jackz007 发表于 2020-12-22 18:04
楼主,把你的帖子编辑一下,一定要把代码贴进代码框,要不然,你贴出来的代码会缺很多东西,看看你的 ...
是因为我设置了红色才会这样的吗?之前好像都没事的 空羊羊 发表于 2020-12-22 18:12
是因为我设置了红色才会这样的吗?之前好像都没事的
一定有事的,不信你发一下
x
看一下效果。 x jackz007 发表于 2020-12-22 18:04
楼主,把你的帖子编辑一下,一定要把代码贴进代码框,要不然,你贴出来的代码会缺很多东西,看看你的 ...
代码框不是那个发帖的时候下面那个大框吗 空羊羊 发表于 2020-12-22 18:20
代码框不是那个发帖的时候下面那个大框吗
不是的,是这个框子的上边框有一个标识有 "<>" 的按钮,点一下就会出现代码框。 jackz007 发表于 2020-12-22 18:27
不是的,是这个框子的上边框有一个标识有 "" 的按钮,点一下就会出现代码框。
okok
不懂帮顶·{:10_261:} 解决了就把帖子改成已解决吧{:10_256:}
页:
[1]