Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

我的作業 幫幫忙 ^^

0 views
Skip to first unread message

哀 唸不完啦

unread,
Jun 10, 2002, 7:33:52 AM6/10/02
to
這個程式是我期末的作業
主要的目的就是輸入姓名
能從通訊錄中找出學生的資料
因為這是我同學的 可是我不知道要怎麼改
希望高手幫個忙 ...

// test2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

typedef struct IDTAG {
char name[9];
char id[10];
} ID_STRUCT;

ID_struct idlist[44];

int sub(int *&ptr)
{
ptr[1] = 1;
return 1;
}

int main(int argc, char* argv[])
{
FILE * pf;
int i, j, bFound;
char str[255], name[12], id[10];
pf = fopen("D:\\CDLiou\\ds\\Cpp Ex\\test2\\NameList.txt", "r");
if (!pf)
return;
i = 0;
while (!feof(pf)) {
fgets(str, sizeof(str), pf);
sscanf(str, "%s %s", id, name);
strcpy(idlist[i].id, id);
strcpy(idlist[i].name, name);
printf("%d:%s\n", i, id);
++i;
}
fclose(pf);
//getch();
printf("Please enter name : ");
scanf("%s", str);
bFound = 0;
for (j=0; j<i; ++j) {
if (strcmp(idlist[j].name, str) == 0) {
bFound = 1;
break;
}
}
if (bFound) {
printf ("\nFound the name %s, id = %s\n", idlist[j].name, idlist[j].id);
}
getch();
//return 0;
}
--
* Origin: ★ 交通大學資訊科學系 BBS ★ <bbs.cis.nctu.edu.tw: 140.113.23.3>

哀 唸不完啦

unread,
Jun 10, 2002, 9:22:36 AM6/10/02
to
抱歉我講解錯誤 ....
這個程式主功能是針對已知的通訊錄 就是知道人數來寫的
老師要我們改寫成動態的 換句話說不管人數給幾個
輸入名字可以知道學號 輸入學號可以知道名字
而且可以連續輸入多筆資料 .....


==> 在 sushovah@cis_nctu (哀 唸不完啦) 的文章中提到:

哀 唸不完啦

unread,
Jun 10, 2002, 9:22:51 AM6/10/02
to

哀 唸不完啦

unread,
Jun 10, 2002, 9:23:32 AM6/10/02
to
抱歉我講解錯誤 ....
這個程式主功能是針對已知的通訊錄 就是知道人數來寫的
老師要我們改寫成動態的 換句話說不管人數給幾個
輸入名字可以知道學號 輸入學號可以知道名字
而且可以連續輸入多筆資料 .....

哀 唸不完啦

unread,
Jun 10, 2002, 9:23:12 AM6/10/02
to
抱歉我講解錯誤 ....
這個程式主功能是針對已知的通訊錄 就是知道人數來寫的
老師要我們改寫成動態的 換句話說不管人數給幾個
輸入名字可以知道學號 輸入學號可以知道名字
而且可以連續輸入多筆資料 .....

混仙

unread,
Jun 10, 2002, 7:57:28 PM6/10/02
to
※ 引述《sushov...@bbs.cis.nctu.edu.tw (哀 唸不完啦)》之銘言:

> 這個程式是我期末的作業
> 主要的目的就是輸入姓名
> 能從通訊錄中找出學生的資料
> 因為這是我同學的 可是我不知道要怎麼改
> 希望高手幫個忙 ...
> // test2.cpp : Defines the entry point for the console application.
> //
> #include "stdafx.h"
> #include <stdio.h>
> #include <stdlib.h>
> #include <conio.h>
> #include <string.h>
> typedef struct IDTAG {
> char name[9];
> char id[10];
> } ID_STRUCT;
> ID_struct idlist[44];
一般不是用 IDTAG idlist[44]; ??

> int sub(int *&ptr)
> {
> ptr[1] = 1;
> return 1;
> }
> int main(int argc, char* argv[])
> {
> FILE * pf;
> int i, j, bFound;
> char str[255], name[12], id[10];
> pf = fopen("D:\\CDLiou\\ds\\Cpp Ex\\test2\\NameList.txt", "r");
> if (!pf)
應該是說 if(pf == NULL) ㄅ ?

> return;
> i = 0;
> while (!feof(pf)) {
> fgets(str, sizeof(str), pf);
fgets會return string, 你是否要改用if(fgets == ....) 來檢查有無errorㄋ?

> sscanf(str, "%s %s", id, name);
你這個scanf好像不太對ㄅ.....

> strcpy(idlist[i].id, id);
> strcpy(idlist[i].name, name);
> printf("%d:%s\n", i, id);
> ++i;
> }
> fclose(pf);
> //getch();
> printf("Please enter name : ");
> scanf("%s", str);
> bFound = 0;
你下面所用的for loop我怎麼看的霧煞煞, 你可否將括號標對, 不然我覺得好像有錯
還有, getch()會return int value ㄅ???

> for (j=0; j<i; ++j) {
> if (strcmp(idlist[j].name, str) == 0) {
> bFound = 1;
> break;
> }
> }
> if (bFound) {
> printf ("\nFound the name %s, id = %s\n", idlist[j].name, idlist[j].id);
> }
> getch();
> //return 0;
> }
如果依你所說的, 輸入name 給 id, 輸入 id 給 name,
我不瞭的是, 為何id 會是char array??? (我是在美國學的, 不知與台灣所說有出入)
還有, 這個是C的,我大約先修改一下基本錯誤, 但因為不是很瞭解你codes究竟該如何
work, 所以我也就先如此幫你..
i hope it works....
--
一位很笨的Computer Science學生 ^^
--
[1;32m┌───── [33m◆ [37mKKCITY [33m◆ [32m─────┐ [34m┐┌┐┐┌┐┌─┐┌┬┐┌┬┐┐ ┌ [m
[1;32m│ [31m bbs.kkcity.com.tw [32m│ [37m├┘┐├┘┐│ │ │ └┬┘ [m
[1;32m└── [34m《 [0;37mFrom:204.210.31.149 [1;34m》 [32m──┘ [36m┘ ┘┘ ┘└─┘└┴┘ ┴ ┴ [m
0 new messages