|
查看: 799|回复: 9
|
C Language: 想不到,卡着了。。。[已解决]
[复制链接]
|
|
|
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. -
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void main()
- {
- FILE * fp;
- int choice,num,i;
- char name[100];
- char output[100];
- start:
- printf("======MENU=====\n";
- printf("***************\n";
- printf("1. Insert names\n";
- printf("2. List names\n";
- printf("3. Exit\n";
- printf("***************\n";
- printf("Your selection: ";
- scanf("%d",&choice);
- if(choice==1)
- {
- if(!(fp = fopen("names.txt","r"))
- {
- fp = fopen("names.txt","w";
- }
- printf("Number of name to key in: ";
- scanf("%d",&num);
- if(num==1)
- {
- printf("Name = ";
- scanf("%s",&name);
- fp = fopen("names.txt","a";
- fprintf(fp,"%s",name);
- fclose(fp);
- }
-
- else
- {
- for(i=1;i<num;i++)
- {
- printf("Name %d =",i);
- scanf("%s",&name);
- fp = fopen("names.txt","a";
- fprintf(fp,"%s\n",name);
- fclose(fp);
- }
- printf("Name %d =",i);
- scanf("%s",&name);
- fp = fopen("names.txt","a";
- fprintf(fp,"%s",name);
- fclose(fp);
- }
- goto start;
- }
- if(choice==2)
- {
- if(!(fp = fopen("names.txt","r"))
- {
- printf("\nFile does not exist.";
- }
- else{
- fp=fopen("names.txt","r";
- while(!feof(fp)){
- fscanf(fp,"%s",&output);
- printf("%s\n",output);
- };
- fclose(fp);
- }
- }
- else
- exit(1);
- }
复制代码 我做不到Press ENTER to go to menu这一个部分。。。
想不到。。。
请大家帮我看看。。。
谢谢。。。
[ 本帖最后由 蜡笔小烦 于 19-10-2008 03:49 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 18-10-2008 03:54 PM
|
显示全部楼层
大概是這樣
最好不要用goto label
簡單的while就可以做到
還有就是exit(1)
放1不太好...
0以外代表EXIT_FAILURE
通常用在錯誤或者異常結束program的時候- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void main()
- {
- FILE * fp;
- int choice,num,i;
- char name[100];
- char output[100];
-
- while(1) {
- printf("======MENU=====\n");
- printf("***************\n");
- printf("1. Insert names\n");
- printf("2. List names\n");
- printf("3. Exit\n");
- printf("***************\n");
- printf("Your selection: ");
- scanf("%d",&choice);
- if(choice==1)
- {
- if(!(fp = fopen("names.txt","r")))
- {
- fp = fopen("names.txt","w");
- }
- printf("Number of name to key in: ");
- scanf("%d",&num);
- if(num==1)
- {
- printf("Name = ");
- scanf("%s",&name);
- fp = fopen("names.txt","a");
- fprintf(fp,"%s",name);
- fclose(fp);
- }
-
- else
- {
- for(i=1;i<num;i++)
- {
- printf("Name %d =",i);
- scanf("%s",&name);
- fp = fopen("names.txt","a");
- fprintf(fp,"%s\n",name);
- fclose(fp);
- }
- printf("Name %d =",i);
- scanf("%s",&name);
- fp = fopen("names.txt","a");
- fprintf(fp,"%s",name);
- fclose(fp);
- }
- } else if(choice==2) {
- if(!(fp = fopen("names.txt","r")))
- {
- printf("\nFile does not exist.");
- }
- else{
- fp=fopen("names.txt","r");
- while(!feof(fp)){
- fscanf(fp,"%s",&output);
- printf("%s\n",output);
- };
- fclose(fp);
- }
- puts("Press ENTER to go to menu.");
- while(getchar()!='\n');
- getchar();
- }
- else {
- exit(0);
- }
- }
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 18-10-2008 04:06 PM
|
显示全部楼层
system("cls"); to clear screen
system("pause>nul"); to pause screen
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <string.h>
- int main()
- {
- FILE * fp;
- int choice,num,i;
- char name[100];
- char output[100];
- start:
- system("cls"; // clear screen
- printf("======MENU=====\n";
- printf("***************\n";
- printf("1. Insert names\n";
- printf("2. List names\n";
- printf("3. Exit\n";
- printf("***************\n";
- printf("Your selection: ";
- scanf("%d",&choice);
- if(choice==1)
- {
- if(!(fp = fopen("names.txt","r"
- fp = fopen("names.txt","w";
- printf("Number of name to key in: ";
- scanf("%d",&num);
- if(num==1)
- {
- printf("Name = ";
- scanf("%s",&name);
- fp = fopen("names.txt","a";
- fprintf(fp,"%s",name);
- fclose(fp);
- }
- else
- {
- for(i=1;i
- {
- printf("Name %d =",i);
- scanf("%s",&name);
- fp = fopen("names.txt","a";
- fprintf(fp,"%s\n",name);
- fclose(fp);
- }
- printf("Name %d =",i);
- scanf("%s",&name);
- fp = fopen("names.txt","a";
- fprintf(fp,"%s",name);
- fclose(fp);
- }
- printf("\nPress ENTER to go to menu\n";
- system("pause>nul"; // pause
- goto start;
- }
- else if (choice==2)
- {
- if (!(fp = fopen("names.txt","r"
- {
- printf("\nFile does not exist.";
- }
- else
- {
- fp=fopen("names.txt","r";
- while(!feof(fp)){
- fscanf(fp,"%s",&output);
- printf("%s\n",output);
- };
- fclose(fp);
- }
- printf("\nPress ENTER to go to menu\n";
- system("pause>nul"; // pause
- }
- else
- exit(1);
- }
复制代码
[ 本帖最后由 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和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);
} |
|
|
|
|
|
|
|
|
|
|
发表于 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("names.txt","a");
- for(i=1;i<=num;i++)
- {
- printf("Name %d =",i);
- scanf("%s",&name);
- fprintf(fp,"%s\n",name);
- }
- 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
怎么会这样子呢? |
|
|
|
|
|
|
|
|
|
|
发表于 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:- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <string.h>
- int main()
- {
- FILE * fp;
- FILE * temp;
- int choice,num,i;
- char name[100];
- char output[100];
- start:
- system("cls"); // clear screen
- printf("======MENU=====\n");
- printf("***************\n");
- printf("1. Insert names\n");
- printf("2. List names\n");
- printf("3. Exit\n");
- printf("***************\n");
- printf("Your selection: ");
- scanf("%d",&choice);
- if(choice==1)
- {
- printf("Number of name to key in: ");
- scanf("%d",&num);
- if ( temp = fopen("names.txt","r"))
- {
- fp = fopen("names.txt","a");
- fprintf(fp,"\n"); // add new line to the file if the file already exist
- }
- else
- fp = fopen("names.txt","a");
- for(i=1;i<=num;i++)
- {
- printf("Name %d =",i);
- scanf("%s",&name);
- if ( i == num )
- fprintf(fp,"%s",name); // do not new line for last entry
- else
- fprintf(fp,"%s\n",name);
- }
- fclose(fp);
- printf("\nPress ENTER to go to menu\n");
- system("pause>nul"); // pause
- goto start;
- }
- else if (choice==2)
- {
- if (!(fp = fopen("names.txt","r")))
- {
- printf("\nFile does not exist.");
- }
- else
- {
- fp=fopen("names.txt","r");
- while(!feof(fp))
- {
- fscanf(fp,"%s",&output);
- printf("%s\n",&output);
- };
- fclose(fp);
- }
- printf("\nPress ENTER to go to menu\n");
- system("pause>nul"); // pause
- }
- else
- exit(1);
- }
复制代码
[ 本帖最后由 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 ...
长知识了~
谢谢!~ |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|