用c++建置一個簡易的會員系統

Home Home
引用 | 編輯 f81513
2010-12-09 21:31
樓主
推文 x0
剛學c++沒多久
老師派了一個作業
是要使用c++建置一個簡易的會員系統

我的程式碼
不知道為什麼新增會員無法新增到檔案裡
拿原本就存在二元檔裡面的帳號密碼登入也沒有辦法

已經困惑了好多天也修改了好久了
但是就是不知道哪裡出錯了
請高手幫忙看一下
萬分感謝


#include <iostream>
#include <fstream>
using namespace::std;
struct memberRecordFormat
{
char account;//帳號
char password;//密碼
char name;//姓名
char cellPhone;//手機
};
void check (char* id ,memberRecordFormat memberfile ,int &counter )//看是不是檔案裡就有的帳號
{
int i ;
for ( i = 1 ; i <= counter ; i++ )
{
while ( strcmp ( id , memberfile.account ) == 0 )//看這個帳號有沒有重複
{
cout << "此帳號已有人使用\n";
cout << "請輸入帳號: ";
cin >> id ;
cout << endl;
}
}
}
int main()
{
memberRecordFormat memberfile;//新建的資料(檔案裡的)

fstream inFile( "file.dat", ios::in | ios:: out | ios::binary );//開檔
while( !inFile )//看有沒有開成功
{
cout << "File could not be opened" << endl;
system("pause");
exit( 1 );
}

int counter = -1;

while(!inFile.eof())//讀檔案裡面的資料
{
counter++;
inFile.read( reinterpret_cast< char * > ( &memberfile ), sizeof( memberRecordFormat ) );//把資料存進檔案中
}
inFile.close();//關檔


int first;//選擇第一步
int second;//選擇第二步

char id;//帳號
char pass;//密碼
char nam;//名字
char cell;//手機

cout << "請輸入:\n1.會員登入\n2.加入會員\n";
cin >> first;//輸入選擇
cout << endl;

if( first < 1 || first > 2 )//如果沒有選1或2時
{
while(first < 1 || first &g ..

訪客只能看到部份內容,免費 加入會員



獻花 x0
引用 | 編輯 totsi
2010-12-13 12:34
1樓
  
我c++也是初學,不過建議可以先參考一下附圖。
輸入之資料


讀回的寫出資料


所以不是沒寫出去,而是寫出去的值錯了。所以只要針對讀寫檔部分處理就好。

還有,結構取binary檔內的資料時,用的是data size。
但是你寫出的卻是NULL-terminate的資料,所以即使資料寫出去了,讀回來還是錯的。

所以建議用struct寫出去的,就用struct讀回來。
但要小心改版本時structure size會有機會不一致的問題。

獻花 x0