【发布时间】:2017-03-27 09:45:45
【问题描述】:
我有这个程序:
using namespace std;
class Motherboards
{
char Board_Manufacturer_Name[50];
char Socket_Type[50];
char Chipset_Type[50];
char Board_Name[50];
char Feature1[500];
char Feature2[500];
char Feature3[500];
float Price;
public:
void getdata();
void displaydata();
};
void Motherboards::getdata()
{
cout << " Enter Manufacturer Name :\n";
cin.getline(Board_Manufacturer_Name, 50);
cout << "\n Enter the Socket type :\n";
cin.getline(Socket_Type, 50);
cout << "\n Enter the Chipset Type :\n";
cin.getline(Chipset_Type, 50);
cout << "\n Enter the Board Name :\n";
cin.getline(Board_Name, 50);
cout << "\n Enter 3 main features for this Board :\n";
cout << "\n1.) ";
cin.getline(Feature1, 500);
cout << "\n2.) ";
cin.getline(Feature2, 500);
cout << "\n3.) ";
cin.getline(Feature3, 500);
cout << "Price :\n";
cin >> Price;
cout << "\n";
}
void Motherboards::displaydata()
{
cout << " Manufacturer : " << Board_Manufacturer_Name << endl;
cout << " Socket : " << Socket_Type << endl;
cout << " Chipset : " << Chipset_Type << endl;
cout << " Board Name : " << Board_Name << endl;
cout << " Features :\n" << "1.) " << Feature1 << endl
<< "2.) " << Feature2 << endl
<< "3.) " << Feature3 << endl;
cout << " Price :\n" << Price;
cout << "\n";
}
int main()
{
int x;
system("cls");
Motherboards M;
fstream Infile;
Infile.open("MotherboardList.dat", ios::in | ios::out | ios::app);
if (!Infile)
{
cerr << "Error :";
}
cout << "Enter the details of the motherboard that you want to add :\n";
M.getdata();
Infile.write((char*)&M, sizeof(M));
cout << "Do you wanna confirm ?";
M.displaydata();
cin >> x;
cout << "Written :\n";
Infile.read((char*)&M, sizeof(M));
M.displaydata();
Infile.close();
return 0;
}
我得到这个输出:
为什么我的数据是用某种随机语言(可能是中文?)编写的,我需要更改什么才能真正正确地写入数据?
我希望文本文件是英文的。另外,我输入的每个数据后不应该有一个换行符吗?
【问题讨论】:
-
您是否尝试使用调试器单步执行您的代码?
-
是的,有没有都试过了
-
但是,您是否尝试过逐步完成您的代码,一步一步?通过观察变量的值?并寻找代码何时执行某项偏离您的预期?
-
是的,他们都是单独工作的
-
什么他们?逐步检查代码,这是行为不端的。那是调试——在开发过程中,你需要做很多事情,所以越早学习——从长远来看,你会做得更好。
标签: c++ c++11 visual-c++