2011-10-29 86 views
3
#include <string.h> 
#include <conio.h> 
#include <fstream.h> 
#include <string.h> 



class mobile 
{ 
int id; 
float price; 
char *model,*brand; 
public: 
int getId() 
{ 
    return id; 
} 
float getPrice(){return price;} 
mobile() 
{ 
    brand = new char[5]; 
    model = new char[5]; 
    id = 0; 
    price = 0.0; 
} 
void addDetail() 
{ 
    cout<<"\nEnter the mobile ID ";cin>>id; 
    cout<<"\nEnter the Price of mobile ";cin>>price; 
    cout<<"\nEnter the Brand name of mobile ";cin>>brand; 
    cout<<"\nEnter the Model of mobile ";cin>>model; 
    ofstream file("database.txt",ios::app); 
    char c=' ',n='\n'; 
    file<<id<<c; 
    file<<price<<c; 
    file<<brand<<c; 
    file<<model<<n; 
    file.close(); 

} 

char* getbrand(){return brand;} 
char* getModel(){return model;} 
~mobile() 
{ 
    delete brand; 
    delete model; 
} 

}; 

void count() 
{ 
int counter = 0; 
char c; 
ifstream file("database.txt"); 
file.seekg(0,ios::beg); 
if(!file)cout<<"File not present"; 
while(file) 
{ 
    file.get(c); 
    if(c == '\n') 
    { 
     counter++; 
    } 
} 
cout<<"The no of entries "<<counter; 
file.close(); 
} 

void addMobile() 
{ 
char *model,*brand; 

mobile m; 
m.addDetail();  
} 

void EditMobile() 
{ 
fstream file; 
char* brand,*model; 
file.open("database.txt",ios::in); 
brand = new char; 
model = new char; 
char c; 
int id,pos; 
float price; 
float f; 
if(!file)cout<<"File not present"; 
else 
{ 


file>>id;cout<<"ID :"<<id<<endl; 
file>>price;cout<<"Price :"<<price<<endl; 
file>>brand;cout<<"Brand :"<<brand<<endl; 
file>>model;cout<<"Model :"<<model<<endl; 

} 

delete brand; 
delete model; 
file.close(); 

} 

void admin() 
{ 

char* userpassword; 
userpassword = new char; 
char* pass; 
pass = new char; 

cout<<"\n\nEnter the Password"; 
cin>>userpassword; 
fstream fin; 
fin.open("password.txt",ios::in | ios::out); 
if(!fin) 
cout<<"\n\nFile does not exist"; 
else 
{ 
fin>>pass; 
if(strcmp(userpassword,pass) == 0) 
{ 
    char ch; 

    count(); 
    clrscr(); 
    cout<<"\n\nAcces Granted!!\n\n\tWelcome"; 
    cout<<"\n\n1.Add a Mobile."; 
    cout<<"\n2.Edit a Mobile."; 
    cout<<"\n3.Delete a Mobile."; 
    cout<<"\n4.View the Database.\n\nEnter Your Choice "; 
    ch = getch(); 
    switch(ch) 
    { 
    case '1': 
     addMobile(); 
     break; 
    case '2': 
     EditMobile(); 
     break; 
    case '3': 
     //deleteMobile(); 
     break; 

    } 
} 
else 
{ 
    cout<<"\n\nAcces Denied"; 
    return; 
} 
} 

fin.close(); 
delete userpassword; 
} 
int main() 
{ 
char choice; 
clrscr(); 
cout<<"Welcome to Mobile Store management Program"; 
cout<<"\n1.Find a Mobile."; 
cout<<"\n2.Know Price."; 
cout<<"\n3.Know Description."; 
cout<<"\n4.Administrator Login.\n\nEnter Your Choice"; 
choice = getch(); 
switch(choice) 
{ 
case '1': 

    break; 
case '4': 
    admin(); 
    break; 
} 
getch(); 
return 0; 
} 

寫入文件現在在輸出I型:隨機字符輸入我在C++

Acces Granted!! 

     Welcome 

1.Add a Mobile. 
2.Edit a Mobile. 
3.Delete a Mobile. 
4.View the Database. 

Enter Your Choice 
Enter the mobile ID 1 

Enter the Price of mobile 123.34 

Enter the Brand name of mobile Nokia 

Enter the Model of mobile mynokiamobile 

和輸出我在database.txt文件得到的是:

1 123.339996 Nokia mynoki ile 

這兩者之間的空間是不能在這裏看到的隨機字符。

回答

4

您超出了分配給brandModel的內存的界限。

brand = new char[5]; 
model = new char[5]; 

brandmobile分配5個字節的內存,但你輸入長於。

您輸入的值是:
對於brand: 「諾基亞」,這是6字節長(5 + 1額外的字節是空終結符)&
對於model: 「mynokiamobile」,這是14字節長

這導致未定義的行爲
您需要增加這些freestore(堆)分配。

更好的C++方法使用起來會std::string代替字符數組,但我不知道這似乎是一個課外練習,也許你不能使用它們,這在我的理解是非常跛腳。

+0

哎...ü得到了...感謝的人..所有我需要做的就是分配50而不是5 .. 。thanx Als –

+0

可能使用'setw'來設置'cin'可以讀取的字符數。 – xanatos

+0

嘿,還有一個問題..如果我寫 > brand = new char; > model = new char; –

2

這是C++,你爲什麼使用C風格的字符串(brandmodel)? brandmodelchar秒的陣列(即,一個C風格串。):

brand = new char[5]; 
model = new char[5]; 

對於你存儲「諾基亞」 +的NUL終止「\ 0」的移動品牌,這是6個char小號。對於model,你正在進入「mynokiamobile」,這是超過5個字符。我也不明白你爲什麼要動態地分配固定大小的數組new,但這是無關緊要的。

底線,用std::string,使你的代碼更加C++ - 喜歡比C.

+0

好吧..但我想這 'brand = new char;' 'model = new char;' 動態分配多少字符作爲輸入.. –

+0

@PranayKhatri它不會那樣做。它會爲**單個** char'分配內存。 'std :: string'容器會監視需要多少內存來容納你的字符串/字符。 – AusCBloke

+0

它沒有在我的編譯器中分配單個字符實體...我試過它會分配你輸入的那麼多字節 –