鱼C论坛

 找回密码
 立即注册
查看: 2548|回复: 11

为什么相同的循环逻辑(标红)在showcode最后可以用,但是openaccount不可以

[复制链接]
发表于 2020-12-22 17:40:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 空羊羊 于 2020-12-22 19:02 编辑
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<time.h>


  5. void showcode(void);
  6. void openaccount(int sequence[10][15]);


  7. struct Account{
  8.         char name[100];
  9.         char id[15];
  10.         char address[200];
  11.         char ccode[4];
  12.         char bcode[3];
  13.         char Dob[10];
  14.         int balance;
  15.         struct tm *Doo;
  16.         char accountnumber[11];
  17. };


  18. void main(void){
  19.         char option='\0', ch='\0', in='\0';       
  20.         int count=0;
  21.         int sequence[10][15];
  22.         FILE *fp;
  23.         if((fp=fopen("Sequence.txt","r"))==NULL){
  24.                 if((fp=fopen("Sequence.txt","w"))==NULL){
  25.                         printf("Fail to open the file!\n");
  26.                         exit(EXIT_FAILURE);
  27.                 }
  28.                 fwrite(sequence, 150*sizeof(int), 1, fp);
  29.         }       
  30.         else{
  31.                 fread(sequence, 150*sizeof(int), 1, fp);
  32.         }
  33.         fclose(fp);
  34.         do{       
  35.                
  36.                 printf("1) Show city code and branch code\n");
  37.                 printf("2) Open an account\n");
  38.                 printf("3) Show account details\n");
  39.                 printf("4) Show list of accounts\n");
  40.                 printf("5) Deposit in an account\n");
  41.                 printf("6) Withdraw from an account\n");
  42.                 printf("7) Transfer money\n");
  43.                 printf("8) Transaction details\n");
  44.                 printf("9) Close an account\n");
  45.                 printf("10)Quit\n");
  46.                 printf("\n");
  47.                 printf("Option: ");
  48.                 count=0;
  49.                 option='\0', ch='\0', in='\0';
  50.                 while((in=getchar())!='\n'){
  51.                         count++;
  52.                         option=ch;
  53.                         ch=in;
  54.                 }
  55.                 if((count==1)&&(ch=='0')){
  56.                         ch='a';
  57.                 }               
  58.                 if(count>2){
  59.                         ch='a';
  60.                 }       
  61.                 if(count==2){               
  62.                         if((option=='1')&&(ch=='0')){
  63.                                 ch='0';
  64.                         }
  65.                         else{
  66.                                 ch='a';
  67.                         }
  68.                 }
  69.                 printf("\n");
  70.                 switch(ch){
  71.                         case '1': showcode(); break;
  72.                         case '2': openaccount(sequence); break;
  73.                         case '3': printf("3\n"); break;
  74.                         case '4': printf("4\n"); break;
  75.                         case '5': printf("5\n"); break;
  76.                         case '6': printf("6\n");break;
  77.                         case '7': printf("7\n");break;
  78.                         case '8': printf("8\n");break;
  79.                         case '9': printf("9\n");break;
  80.                         case '0': printf("break\n");break;
  81.                         default: printf("Unknown option!\n");
  82.                                  printf("\n");
  83.                 }
  84.         }while(ch!='0');
  85.         exit(0);
  86. }

  87. void showcode(void){
  88.         FILE *fp;
  89.         char name1[10], name2[10], name3[10];
  90.         char code1[4], code2[4], code3[4];
  91.         char buffer[20], citycode[4], choice;
  92.         char *name, *code;
  93.         int len, rmd;
  94.         if((fp=fopen("CityBranchCodes_Leyang_Hu_20215416.txt", "r"))==NULL){
  95.                 printf("Fail to open the file!\n");
  96.                 exit(EXIT_FAILURE);
  97.         }
  98.         printf("City\t\t\tcode\t\t\tCity\t\t\tcode\t\t\tCity\t\t\tcode\n");
  99.         printf("_____________________________________________________________________________________________________________________________\n");       
  100.         while(fgets(buffer, 20, fp)!=NULL){
  101.                 len=strlen(buffer);       
  102.                 buffer[len-1]='\0';
  103.                 code=strtok(buffer, "\t");
  104.                 name=strtok(NULL, "\t");
  105.                 if(strlen(code)==3){
  106.                         rmd=(atoi(code))%3;
  107.                         switch(rmd){
  108.                                 case 1:
  109.                                         strcpy(name1, name);
  110.                                         strcpy(code1, code);
  111.                                         break;
  112.                                 case 2:
  113.                                         strcpy(name2, name);
  114.                                         strcpy(code2, code);
  115.                                         break;
  116.                                 case 0: strcpy(name3, name);
  117.                                         strcpy(code3, code);
  118.                                         printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2, name3, code3);
  119.                                         break;
  120.                         }
  121.                 }
  122.         }
  123.         printf("%-10s\t\t%-10s\n", name1, code1);
  124.         printf("\n");
  125.        
  126.         fseek(fp, 0, SEEK_SET);
  127.         printf("Enter the city code: ");       
  128.         scanf("%s", citycode);
  129.         getchar();
  130.         printf("\n");
  131.         if((fp=fopen("CityBranchCodes_Leyang_Hu_20215416.txt", "r"))==NULL){
  132.                 printf("Fail to open the file!\n");
  133.                 exit(EXIT_FAILURE);
  134.         }
  135.         while(fgets(buffer, 20, fp)!=NULL){
  136.                 len=strlen(buffer);       
  137.                 buffer[len-1]='\0';
  138.                 code=strtok(buffer, "\t");
  139.                 name=strtok(NULL, "\t");               
  140.                 if((strcmp(code, citycode))==0){
  141.                         break;
  142.                 }
  143.         }
  144.         printf("Branch name\t\tcode\t\t\tBranch name\t\tcode\t\t\tBranch name\t\tcode\n");
  145.         printf("_____________________________________________________________________________________________________________________________\n");       
  146.         while(fgets(buffer, 20, fp)!=NULL){                       
  147.                 len=strlen(buffer);       
  148.                 buffer[len-1]='\0';
  149.                 code=strtok(buffer, "\t");
  150.                 name=strtok(NULL, "\t");
  151.                 if(strlen(code)==3){
  152.                         break;
  153.                 }
  154.                 rmd=(atoi(code))%3;
  155.                 switch(rmd){
  156.                         case 1:
  157.                                 strcpy(name1, name);
  158.                                 strcpy(code1, code);
  159.                                 break;
  160.                         case 2:
  161.                                 strcpy(name2, name);
  162.                                 strcpy(code2, code);
  163.                                 break;
  164.                         case 0: strcpy(name3, name);
  165.                                 strcpy(code3, code);
  166.                                 printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2, name3, code3);
  167.                                 break;
  168.                 }
  169.         }
  170.         switch(rmd){
  171.                 case 1:
  172.                         printf("%-10s\t\t%-10s\n", name1, code1);
  173.                         break;                       
  174.                 case 2:
  175.                         printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2);
  176.                         break;
  177.                 case 0:
  178.                         break;
  179.         }
  180.         fclose(fp);
  181.         printf("\n");
  182.         printf("Do you want to see more city and branch codes (Y/N): ");
  183.         scanf("%c", &choice);
  184.         getchar();
  185.         if(choice=='Y'){
  186.                                
  187.                 showcode();
  188.         }
  189.         printf("\n");
  190. }
  191.                        

  192. void openaccount(int sequence[10][15]){
  193.         struct Account *account=(struct Account*)malloc(sizeof(struct Account));       
  194.         int blc,i,j,number,len;
  195.         char ch, orig[5], seq[5];
  196.         FILE *fp, *ptr;
  197.         time_t t;
  198.         printf("Enter the name of the account holder: ");
  199.         while((ch=getchar())!='\n'){
  200.                 account->name[i]=ch;
  201.                 i++;               
  202.         }
  203.         printf("Enter the Id number of the account holder: ");
  204.         scanf("%s", account->id);
  205.         getchar();       
  206.         printf("Enter the address of the account holder: ");
  207.         i=0;       
  208.         while((ch=getchar())!='\n'){
  209.                 account->address[i]=ch;
  210.                 i++;               
  211.         }
  212.         printf("Enter city code: ");
  213.         scanf("%s", account->ccode);
  214.         getchar();
  215.         printf("Enter branch code: ");
  216.         scanf("%s", account->bcode);
  217.         getchar();
  218.         printf("Enter the Dob of the account holder: ");
  219.         scanf("%s", account->Dob);
  220.         getchar();
  221.         while(blc<1000){
  222.                 printf("Enter the initial balance of the account (not less that 1000 RMB): ");
  223.                 scanf("%d", &blc);
  224.                 if(blc<1000){
  225.                         printf("Initial deposit can't be less than 1000 RMB.\n");
  226.                 }
  227.         }
  228.         account->balance=blc;
  229.         time(&t);
  230.         account->Doo=localtime(&t);
  231.         if((fp=fopen("Account.txt", "a"))==NULL){
  232.                 printf("Fail to open the file!\n");
  233.                 exit(EXIT_FAILURE);
  234.         }
  235.         i=atoi(account->ccode);
  236.         j=atoi(account->bcode);       
  237.         number=(sequence[i][j])+1;
  238.         sequence[i][j]+=1;
  239.         if((ptr=fopen("Sequence.txt", "w"))==NULL){
  240.                 printf("Fail to open the file!\n");
  241.                 exit(EXIT_FAILURE);
  242.         }
  243.         fwrite(sequence, 150*sizeof(int), 1, ptr);
  244.         fclose(ptr);
  245.         sprintf(orig, "%d", number);
  246.         len=strlen(orig);
  247.         i=0;
  248.         for(j=0;j<5;j++){
  249.                 if(j>=(5-len)){
  250.                         seq[j]=orig[i];
  251.                         i++;
  252.                 }
  253.                 else{
  254.                         seq[j]='0';
  255.                 }
  256.         }
  257.         strcpy(account->accountnumber, account->ccode);
  258.         strcat(account->accountnumber, account->bcode);
  259.         strcat(account->accountnumber, seq);
  260.         account->accountnumber[10]='\0';
  261.         printf("The account number is: %s\n", account->accountnumber);
  262.         fwrite(account, sizeof(struct Account), 1, fp);
  263.         free(account);
  264.         fclose(fp);
  265.         printf("\n");
  266.         printf("Do you want to open more accounts?(Y/N): ");
  267.         scanf("%c", &ch);
  268.         getchar();
  269.         if(ch=='Y'){
  270.                 printf("\n");               
  271.                 openaccount(sequence);
  272.         }
  273.         printf("\n");
  274. }
复制代码





就是我每次在每个功能进行一次后在结尾询问是否要继续后, showcode的可以正确执行,但是openaccount的就不可以
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-12-23 10:40:42 | 显示全部楼层
解决了,就是直接在主程序菜单的switch的case 2里写一个do...while来控制是否进行循环
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-22 17:41:28 | 显示全部楼层
真的很疑惑,求助各位大佬!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-22 18:04:28 | 显示全部楼层

回帖奖励 +1 鱼币

本帖最后由 jackz007 于 2020-12-22 18:06 编辑

     楼主,把你的帖子编辑一下,一定要把代码贴进代码框,要不然,你贴出来的代码会缺很多东西,看看你的代码,是不是在某个位置起变成斜体了,因为你的部分代码被作为字体及其它版面描述符而被吸收掉了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-22 18:12:30 | 显示全部楼层
jackz007 发表于 2020-12-22 18:04
楼主,把你的帖子编辑一下,一定要把代码贴进代码框,要不然,你贴出来的代码会缺很多东西,看看你的 ...

是因为我设置了红色才会这样的吗?之前好像都没事的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-22 18:16:14 | 显示全部楼层
空羊羊 发表于 2020-12-22 18:12
是因为我设置了红色才会这样的吗?之前好像都没事的

    一定有事的,不信你发一下
  1. x[i][j]
复制代码

    看一下效果。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-22 18:19:19 | 显示全部楼层
x[i][j]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-12-22 18:20:40 | 显示全部楼层
jackz007 发表于 2020-12-22 18:04
楼主,把你的帖子编辑一下,一定要把代码贴进代码框,要不然,你贴出来的代码会缺很多东西,看看你的 ...

代码框不是那个发帖的时候下面那个大框吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-22 18:27:16 | 显示全部楼层
空羊羊 发表于 2020-12-22 18:20
代码框不是那个发帖的时候下面那个大框吗

      不是的,是这个框子的上边框有一个标识有 "<>" 的按钮,点一下就会出现代码框。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-22 19:00:49 | 显示全部楼层
jackz007 发表于 2020-12-22 18:27
不是的,是这个框子的上边框有一个标识有 "" 的按钮,点一下就会出现代码框。

okok
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-23 10:07:19 | 显示全部楼层

回帖奖励 +1 鱼币

不懂帮顶·
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-23 11:52:17 | 显示全部楼层

回帖奖励 +1 鱼币

解决了就把帖子改成已解决吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-4 06:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表