#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
這兩者之間的空間是不能在這裏看到的隨機字符。
哎...ü得到了...感謝的人..所有我需要做的就是分配50而不是5 .. 。thanx Als –
可能使用'setw'來設置'cin'可以讀取的字符數。 – xanatos
嘿,還有一個問題..如果我寫 > brand = new char; > model = new char; –