#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;
}
现在在输出中输入:
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
这里的空格是随机字符,不能在这里看到。
答案 0 :(得分:4)
您超出了为brand
和Model
分配的内存范围。
brand = new char[5];
model = new char[5];
brand
和mobile
分配了5个字节的内存,但输入时间比此长。
您输入的值是:
对于brand
:“诺基亚”,6
字节长(5 + 1
附加字节为NULL终结符)&amp;
对于model
:“mynokiamobile”,14
字节长
这会导致未定义的行为 您需要增加这些freestore(堆)分配。
更好的C ++方法是使用 std::string 而不是字符数组,但我不知道这似乎是一项功课,也许你不允许使用它们,在我的理解中,这将是非常蹩脚的。
答案 1 :(得分:2)
这些字符串长度超过4个字符(加上所需的终止NULL
)。你真的不知道用户要输入多少个字母。使用std::string
代替char[5]
可能会解决您的问题。
当您重读文件时,您必须拆分空格。请参阅this question for how to tokenize/split a string in C++。
在分割输入字符串后,请参阅this C++ FAQ question for how to turn strings back into numeric types。
答案 2 :(得分:2)
这是C ++,为什么使用C样式字符串(brand
和model
)? brand
和model
是char
s的数组(即C样式字符串):
brand = new char[5];
model = new char[5];
对于移动品牌,您要存储“诺基亚”+ NUL终止符'\ 0',即6 char
秒。对于model
你进入“mynokiamobile”,这超过了5个字符。我也不明白你为什么用new
动态分配固定大小的数组,但那是无关紧要的。
底线,使用std::string
并使您的代码更像C ++ - 而不是C。