如何在C++中忽略cin的.txt作为输出文件



这是我的C++代码,我希望创建一个输出文件。为了创建输出文件,我请求用户在这个输入cin >> accnum ;中包含.txt,因此,当生成输出文件时,它将以纯文本形式显示accnum.txt。我的问题是,如果我必须输入.txt,我应该怎么做才能忽略文本文件中出现的.txt(这是为了生成txt文件,输入时没有.txt,就不会生成输出文件(。我已经放了一条评论线,请参考这条线。谢谢你的帮助!

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
double withdraw,deposit,number,transfer,num,amt;
string name,first_name,middle_name,last_name;
int x=0;
int option;
int A = 300;
int B = 500;    
int C,W,y;
string accnum;
ifstream infile;
ofstream outfile;

do {
cout <<"                          Welcome to ATM Machine                         "<<endl;
cout <<"nt****Menu****";
cout <<"nt*                 1.Creating a new account                     *";
cout <<"nt*                 2.Cash Deposit                               *";
cout <<"nt*                 3.Cash withdrawal                            *";
cout <<"nt*                 4.Fund transfer between two account          *";
cout <<"nt*                 5.Exit                                       *";
cout <<"nt*                                                              *";
cout <<"nt********";
cout <<"nt                  Please choose an option: ";
cin>>option;

switch(option){
case 1:
cout <<"Press 1 to Confirm" << endl << "Press 0 to exit " << endl ;
cin >> C ;
{
if( C == 1 )
{
cout << "Enter first name: " << endl;
cin >> first_name;
cout << "Enter middle name: " << endl;
cin >> middle_name;
cout << "Enter last name: " << endl;
cin >> last_name;

name = first_name + " " + middle_name + " " + last_name;
cout << "enter a 4 digit number : " << endl << "gentle reminder : please add .txt at the end of your number" << endl << "e.g 1234.txt" << endl; 
cin  >> accnum ; //how to ignore .txt
cout << "What is your primary balance: " << endl;
cin >> amt;
infile.open(name);
outfile.open(name);
cout << "your account has been created " << endl ;
outfile << "name" << name << endl;
outfile << "account no. : " << accnum << endl ; 
outfile << "current balance : RM" << amt << endl ;
}else {
cout << "Bye, have a nice day";
}
infile.close();
outfile.close();
}
break;

case 2:
cout<<"Deposit amount: "<<endl;
cin>>deposit;
if(deposit<10) {                  
cout<<"The smallest note acceptable is RM10."<<endl;
}else {
x=static_cast<int>(deposit)%10;
if(x!=0)
{                   
cout<<"Invalid deposit amount"<<endl;
cout<<"Example of deposit amount: RM10, RM20, RM50, RM320..."<<endl;
}else {
A+=deposit;
cout<<"current balance: RM"<< A <<endl;
}
}                     
break;

case 3:
cout<<"Amount you want to withdraw"<<endl;
cin>>withdraw;
if(A < withdraw || withdraw < 10) {    // A is balance 
cout <<"Failed to withdraw money! Please try again."<<endl;
}else {
A-=withdraw;
cout <<"current balance: "<< A <<endl;
}
break;

case 4:
infile.open("1212.txt");

infile >> x;
cout<<"Enter Details of transfering Account(Account A)"<<endl; 
cout<< "Your account number is:"<<endl; 
cin>>W;
cout<< "What is your name:"<<endl;
cin.ignore();
getline(cin,name);
cout<<"Enter Details of receiving Account(Account B)"<<endl;
cout<<"Enter account number:"<<endl;
cin>>W;
cout<<"Enter name:"<<endl;
cin.ignore();
getline(cin, name);
cout <<"your account balance is : " << x << endl << "how much do you want to transfer ? ";
cin >> num ;

outfile.open("1212.txt");
outfile << x - num ;
cout << "your account balance is : " << x - num ;


infile.close();
outfile.close();

infile.open("2121.txt");

infile >> y ;
outfile.open("2121.txt") ;
outfile << y + num ;

infile.close();
outfile.close();
break;

case 5:
cout<<"Thank you for using ATM machine. Bye, have a nice day !"<<endl;
return 0;

default:
if(option != 5) cout<<"Invalid option.Please try again!"<<endl;
break;
}
}while(option != 5);

system("pause"); 
return 0;
}

只需创建没有格式的文件,名称将完全是file_name内容,请注意,在windows中有保留名称,如CON

#include <iostream>
#include <fstream>
#include <string>

int main (void) {
std::string  file_name = "1234";
std::fstream f_handle;
f_handle.open(file_name, std::fstream::out);
if (f_handle.is_open()){
f_handle << "Some text" << std::endl;
f_handle.close();
}
f_handle.open(file_name, std::fstream::in);
if (f_handle.is_open()){
std::cout << f_handle.rdbuf() << std::endl;
f_handle.close();
}
return 0;
}

您询问了如何从accnum中删除.txt部分,这里有一种方法:

cin  >> accnum ; //how to ignore .txt
// check that the length/size of the entered accnum is at least 4 characters
// and
// that the last 4 characters is ".txt"
if(accnum.size() >= 4 && accnum.substr(accnum.size() - 4) == ".txt") {
// extract a substring from accnum without the last 4 characters
accnum = accnum.substr(0, accnum.size() - 4);
}

然而,我对用户应该实际将.txt添加到账号的要求表示怀疑,因为在ATM上看到这一点会非常令人困惑:

enter a 4 digit number :
gentle reminder : please add .txt at the end of your number"
e.g 1234.txt

它特别令人困惑,因为你唯一想对.txt做的就是删除它。我会考虑更改这个用户界面。

建议:

  • 让用户添加没有.txt的帐号
  • 不要将帐户信息保存在以帐户持有人名称为文件名的文件中,而是将其保存在名为accnum + ".txt"的文件中。这使得一个人有可能拥有多个账户——就像在现实生活中一样

最新更新