旨在让用户更新其详细信息的小程序。
在 get 函数和 get 函数中初始化的所有变量都在调用任何 set 函数之前调用:
int File::Getline1()
{
std::string filename = std::to_string(user1);
std::ifstream fin(filename + ".txt");
fin.getline (line1, 5);
fin.close();
user1 = (atoi(line1));
return user1;
}
int File::Getline2()
{
std::string filename = std::to_string(user1);
std::ifstream fin(filename + ".txt");
fin.getline (line1, 5);
fin.getline (line2, 5);
fin.close();
user2 = (atoi(line2));
return user2;
}
long long int File::Getline3()
{
std::string filename = std::to_string(user1);
std::ifstream fin(filename + ".txt");
fin.getline (line1, 5);
fin.getline (line2, 5);
fin.getline (line3, 20);
fin.close();
char* endptr = NULL;
return strtoll(line3, &endptr, 10);
}
int File::Getline4()
{
std::string filename = std::to_string(user1);
std::ifstream fin(filename + ".txt");
fin.getline (line1, 5);
fin.getline (line2, 5);
fin.getline (line3, 20);
fin.getline (line4, 5);
fin.close();
user4 = (atoi(line4));
return user4;
}
调用哪些集合函数取决于其他地方的一系列 if 语句,在 if 语句之后调用 SetFile()。
如果调用 Setline3_4,则所有内容都会正确写入文件。但是,如果调用 Setline1 或 Setline2,则所有内容都正确写入文件,但 user3除外,它写为“-3689348814741910324”,但 user1、user2 和 user4 都正确写入。
我不知道为什么(注意:所有用户变量都是整数,除了 user3 是一个 long long int - 我认为问题可能出在此处?)
void File::Setline1()
{
user5 = user1;
filename = std::to_string(user5);
remove((filename + ".txt").c_str());
std::cout << "Enter Line 1: ";
std::cin >> user1;
}
void File::Setline2()
{
std::cout << "Deposit (can be 0): ";
std::cin >> deposit;
std::cout << "\nWithdraw (can be 0): ";
std::cin >> withdraw;
user2 = ((user2 + deposit) - withdraw);
}
void File::Setline3_4()
{
std::cout << "Card Number: ";
std::cin >> user3;
std::cout << "Card Expiry date: ";
std::cin >> user4;
}
void File::Setfile()
{
std::string filename = std::to_string(user1);
std::ofstream fout(filename + ".txt");
fout << user1 << std::endl << user2 << std::endl << user3 << std::endl << user4 << std::endl;
fout.close();
}