佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 799|回复: 9

C Language: 想不到,卡着了。。。[已解决]

[复制链接]
发表于 18-10-2008 02:58 PM | 显示全部楼层 |阅读模式
Write a C menu program that contains 2 functions:

1) insert names
2) list of names

In the first function, user will be ask for how many names that he want to key in. Then, the user will key in numbers of name (depend on the answer for the first question). All names should be kept in external file (i.e:names.txt). For the second function, the program will list down all the names that have been keyed in. This can be done by fetching the names from the external file. (Even after the system was shut down, the names still exist in the external file).

Example of the program:

************************************
* Menu *
* 1) Insert name *
* 2) List all names *
* 3) Exit *
*************************************
Your selection: 1

//When the user press 1, the program will enter the insert name function

/* Insert name */

Number of name to key in: 2
Name 1: Asyraf
Name 2: Irwandi
Press ENTER to go to menu

//the user will brought back to menu interface

*************************************
* Menu *
* 1) Insert name *
* 2) List all names *
* 3) Exit *
*************************************
Your selection: 2

/*List all names function*/

List of name for today

Asyraf
Irwandi

Press ENTER to go to menu

//the user will brought back to menu interface

****************************************
* Menu *
* 1) Insert name *
* 2) List all names *
* 3) Exit *
*****************************************
Your selection: 3

//system shut down

After several key-in sessions, the data.txt files should able to list down all names from the first key-in session.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void main()
  5. {
  6. FILE * fp;
  7. int choice,num,i;
  8. char name[100];
  9. char output[100];

  10. start:
  11. printf("======MENU=====\n";
  12. printf("***************\n";
  13. printf("1. Insert names\n";
  14. printf("2. List names\n";
  15. printf("3. Exit\n";
  16. printf("***************\n";
  17. printf("Your selection: ";
  18. scanf("%d",&choice);
  19. if(choice==1)
  20. {
  21.   if(!(fp = fopen("names.txt","r"))
  22.   {
  23.    fp = fopen("names.txt","w";
  24.   }
  25.   printf("Number of name to key in: ";
  26.   scanf("%d",&num);
  27.   if(num==1)
  28.   {
  29.    printf("Name = ";
  30.    scanf("%s",&name);
  31.    fp = fopen("names.txt","a";
  32.    fprintf(fp,"%s",name);
  33.    fclose(fp);
  34.   }
  35.   
  36.   else
  37.   {
  38.    for(i=1;i<num;i++)
  39.    {
  40.    printf("Name %d =",i);
  41.    scanf("%s",&name);
  42.    fp = fopen("names.txt","a";
  43.    fprintf(fp,"%s\n",name);
  44.    fclose(fp);
  45.    }
  46.    printf("Name %d =",i);
  47.    scanf("%s",&name);
  48.    fp = fopen("names.txt","a";
  49.    fprintf(fp,"%s",name);
  50.    fclose(fp);
  51.   }
  52.   goto start;
  53. }
  54. if(choice==2)
  55. {
  56.   if(!(fp = fopen("names.txt","r"))
  57.   {
  58.    printf("\nFile does not exist.";
  59.   }
  60.   else{
  61.   fp=fopen("names.txt","r";
  62.   while(!feof(fp)){
  63.    fscanf(fp,"%s",&output);
  64.    printf("%s\n",output);
  65.   };
  66.   fclose(fp);
  67.   }
  68. }
  69. else
  70.   exit(1);
  71. }
复制代码
我做不到Press ENTER to go to menu这一个部分。。。
想不到。。。
请大家帮我看看。。。
谢谢。。。

[ 本帖最后由 蜡笔小烦 于 19-10-2008 03:49 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 18-10-2008 03:54 PM | 显示全部楼层
大概是這樣
最好不要用goto label
簡單的while就可以做到
還有就是exit(1)
放1不太好...
0以外代表EXIT_FAILURE
通常用在錯誤或者異常結束program的時候
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void main()
  5. {
  6.         FILE * fp;
  7.         int choice,num,i;
  8.         char name[100];
  9.         char output[100];
  10.        
  11.         while(1) {
  12.                 printf("======MENU=====\n");
  13.                 printf("***************\n");
  14.                 printf("1. Insert names\n");
  15.                 printf("2. List names\n");
  16.                 printf("3. Exit\n");
  17.                 printf("***************\n");
  18.                 printf("Your selection: ");
  19.                 scanf("%d",&choice);
  20.                 if(choice==1)
  21.                 {
  22.                   if(!(fp = fopen("names.txt","r")))
  23.                   {
  24.                    fp = fopen("names.txt","w");
  25.                   }
  26.                   printf("Number of name to key in: ");
  27.                   scanf("%d",&num);
  28.                   if(num==1)
  29.                   {
  30.                    printf("Name = ");
  31.                    scanf("%s",&name);
  32.                    fp = fopen("names.txt","a");
  33.                    fprintf(fp,"%s",name);
  34.                    fclose(fp);
  35.                   }
  36.                   
  37.                   else
  38.                   {
  39.                    for(i=1;i<num;i++)
  40.                    {
  41.                    printf("Name %d =",i);
  42.                    scanf("%s",&name);
  43.                    fp = fopen("names.txt","a");
  44.                    fprintf(fp,"%s\n",name);
  45.                    fclose(fp);
  46.                    }
  47.                    printf("Name %d =",i);
  48.                    scanf("%s",&name);
  49.                    fp = fopen("names.txt","a");
  50.                    fprintf(fp,"%s",name);
  51.                    fclose(fp);
  52.                   }
  53.                 } else if(choice==2) {
  54.                   if(!(fp = fopen("names.txt","r")))
  55.                   {
  56.                    printf("\nFile does not exist.");
  57.                   }
  58.                   else{
  59.                   fp=fopen("names.txt","r");
  60.                   while(!feof(fp)){
  61.                    fscanf(fp,"%s",&output);
  62.                    printf("%s\n",output);
  63.                   };
  64.                   fclose(fp);
  65.                   }
  66.                   puts("Press ENTER to go to menu.");
  67.                   while(getchar()!='\n');
  68.                   getchar();
  69.                 }
  70.                 else {
  71.                   exit(0);
  72.                 }
  73.         }
  74. }
复制代码
回复

使用道具 举报

发表于 18-10-2008 04:06 PM | 显示全部楼层
system("cls"); to clear screen
system("pause>nul"); to pause screen


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <string.h>

  5. int main()
  6. {
  7.     FILE * fp;
  8.     int choice,num,i;
  9.     char name[100];
  10.     char output[100];

  11.     start:
  12.     system("cls"; // clear screen
  13.     printf("======MENU=====\n";
  14.     printf("***************\n";
  15.     printf("1. Insert names\n";
  16.     printf("2. List names\n";
  17.     printf("3. Exit\n";
  18.     printf("***************\n";
  19.     printf("Your selection: ";
  20.     scanf("%d",&choice);

  21.     if(choice==1)
  22.     {
  23.         if(!(fp = fopen("names.txt","r"
  24.           fp = fopen("names.txt","w";

  25.         printf("Number of name to key in: ";
  26.         scanf("%d",&num);

  27.         if(num==1)
  28.         {
  29.             printf("Name = ";
  30.             scanf("%s",&name);
  31.             fp = fopen("names.txt","a";
  32.             fprintf(fp,"%s",name);
  33.             fclose(fp);
  34.         }
  35.         else
  36.         {
  37.             for(i=1;i
  38.             {
  39.                 printf("Name %d =",i);
  40.                 scanf("%s",&name);
  41.                 fp = fopen("names.txt","a";
  42.                 fprintf(fp,"%s\n",name);
  43.                 fclose(fp);
  44.             }

  45.             printf("Name %d =",i);
  46.             scanf("%s",&name);
  47.             fp = fopen("names.txt","a";
  48.             fprintf(fp,"%s",name);
  49.             fclose(fp);
  50.         }

  51.         printf("\nPress ENTER to go to menu\n";
  52.         system("pause>nul"; // pause

  53.         goto start;
  54.     }
  55.     else if (choice==2)
  56.     {
  57.         if (!(fp = fopen("names.txt","r"
  58.         {
  59.             printf("\nFile does not exist.";
  60.         }
  61.         else
  62.         {
  63.             fp=fopen("names.txt","r";
  64.             while(!feof(fp)){
  65.                 fscanf(fp,"%s",&output);
  66.                 printf("%s\n",output);
  67.             };
  68.             fclose(fp);
  69.         }

  70.         printf("\nPress ENTER to go to menu\n";
  71.         system("pause>nul"; // pause
  72.     }
  73.     else
  74.         exit(1);
  75. }
复制代码

[ 本帖最后由 solidx 于 18-10-2008 04:10 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 18-10-2008 05:36 PM | 显示全部楼层
原帖由 solidx 于 18-10-2008 04:06 PM 发表
system("cls"); to clear screen
system("pause>nul"); to pause screen


#include
#include
#include
#include

int main()
{
    FILE * fp;
    int choice,num,i;
    char name[100];
    c ...


我想问说:
为什么
txt file 里是这样的呢
jeff
andyjohn
joey

jeff和andy是第一次的input
john和joey是第二次的input
为什么他们会是在一起的呢?
我试过用\n来分开它们
可是没有效的说。。。
回复

使用道具 举报

 楼主| 发表于 18-10-2008 05:37 PM | 显示全部楼层
原帖由 cheng1986 于 18-10-2008 03:54 PM 发表
大概是這樣
最好不要用goto label
簡單的while就可以做到
還有就是exit(1)
放1不太好...
0以外代表EXIT_FAILURE
通常用在錯誤或者異常結束program的時候#include

原来是这样子
长知识了
谢谢!~
回复

使用道具 举报

发表于 18-10-2008 06:39 PM | 显示全部楼层

回复 4# 蜡笔小烦 的帖子

This is because you missed out the \n in the code below =)

if(num==1)
{
            printf("Name = ";
            scanf("%s",&name);
            fp = fopen("names.txt","a";
            fprintf(fp,"%s",name);
            fclose(fp);
}
回复

使用道具 举报

Follow Us
发表于 18-10-2008 06:41 PM | 显示全部楼层
and AVOID opening and closing the file stream in the for loops, it's rather expensive. You should open the file stream before the loop, and close the stream after the loop, like  below:

  1. fp = fopen("names.txt","a");

  2. for(i=1;i<=num;i++)
  3. {
  4.     printf("Name %d =",i);
  5.     scanf("%s",&name);
  6.     fprintf(fp,"%s\n",name);
  7. }

  8. fclose(fp);
复制代码
回复

使用道具 举报

 楼主| 发表于 18-10-2008 09:23 PM | 显示全部楼层
原帖由 solidx 于 18-10-2008 06:41 PM 发表
and AVOID opening and closing the file stream in the for loops, it's rather expensive. You should open the file stream before the loop, and close the stream after the loop, like  below:

fp = fopen( ...


以上的问题都解决了
只是出现了一个我不明白的问题

就是
我第一次input了shelly和andy
然后第二次input了apple和banana
结果list name的时候却出现
shelly
andy
apple
banana
banana

而在txt file里却是这样的:
shelly
andy
apple
banana

怎么会这样子呢?
回复

使用道具 举报


ADVERTISEMENT

发表于 19-10-2008 01:39 PM | 显示全部楼层
The problem is because fscanf and scanf function read space as delimiter(include the new line escape character, \n). Simple way to solve this is not to put \n at the end of the entry in the file. Anyway the problem still arise for input of string containing space(s), scanf will treat them as delimiters.

This is quite a brute way to solve this problem:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <string.h>

  5. int main()
  6. {
  7.     FILE * fp;
  8.     FILE * temp;
  9.     int choice,num,i;
  10.     char name[100];
  11.     char output[100];

  12.     start:
  13.     system("cls"); // clear screen
  14.     printf("======MENU=====\n");
  15.     printf("***************\n");
  16.     printf("1. Insert names\n");
  17.     printf("2. List names\n");
  18.     printf("3. Exit\n");
  19.     printf("***************\n");
  20.     printf("Your selection: ");
  21.     scanf("%d",&choice);

  22.     if(choice==1)
  23.     {
  24.         printf("Number of name to key in: ");
  25.         scanf("%d",&num);

  26.         if ( temp = fopen("names.txt","r"))
  27.         {
  28.             fp = fopen("names.txt","a");
  29.             fprintf(fp,"\n"); // add new line to the file if the file already exist
  30.         }
  31.         else
  32.             fp = fopen("names.txt","a");

  33.         for(i=1;i<=num;i++)
  34.         {
  35.             printf("Name %d =",i);
  36.             scanf("%s",&name);

  37.             if ( i == num )
  38.                 fprintf(fp,"%s",name); // do not new line for last entry
  39.             else
  40.                 fprintf(fp,"%s\n",name);
  41.         }

  42.         fclose(fp);

  43.         printf("\nPress ENTER to go to menu\n");
  44.         system("pause>nul"); // pause

  45.         goto start;
  46.     }
  47.     else if (choice==2)
  48.     {
  49.         if (!(fp = fopen("names.txt","r")))
  50.         {
  51.             printf("\nFile does not exist.");
  52.         }
  53.         else
  54.         {
  55.             fp=fopen("names.txt","r");
  56.             while(!feof(fp))
  57.             {
  58.                 fscanf(fp,"%s",&output);
  59.                 printf("%s\n",&output);
  60.             };
  61.             fclose(fp);
  62.         }

  63.         printf("\nPress ENTER to go to menu\n");
  64.         system("pause>nul"); // pause
  65.     }
  66.     else
  67.         exit(1);
  68. }
复制代码

[ 本帖最后由 solidx 于 19-10-2008 01:43 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 19-10-2008 03:48 PM | 显示全部楼层
原帖由 solidx 于 19-10-2008 01:39 PM 发表
The problem is because fscanf and scanf function read space as delimiter(include the new line escape character, \n). Simple way to solve this is not to put \n at the end of the entry in the file. Anyw ...


长知识了~
谢谢!~
回复

使用道具 举报

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

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-12-2025 06:32 PM , Processed in 0.129515 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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