如何在C++;? 这是我的C++代码,我希望创建一个输出文件。为了创建输出文件,我请求用户在此输入中包含.txt,cin>>accnum,因此,当生成输出文件时,它将以纯文本形式显示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 <<"\n\t****Menu****"; cout <<"\n\t* 1.Creating a new account *"; cout <<"\n\t* 2.Cash Deposit *"; cout <<"\n\t* 3.Cash withdrawal *"; cout <<"\n\t* 4.Fund transfer between two account *"; cout <<"\n\t* 5.Exit *"; cout <<"\n\t* *"; cout <<"\n\t********"; cout <<"\n\t 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; } #包括 #包括 #包括 #包括 使用名称空间std; int main() { 双支取、存款、编号、转账、编号、金额; 字符串名、名字、中间名、姓氏; int x=0; int选项; INTA=300; int B=500; int C,W,y; 字符串accnum; 河流充填; 出流孔的直径; 做{ cout

如何在C++;? 这是我的C++代码,我希望创建一个输出文件。为了创建输出文件,我请求用户在此输入中包含.txt,cin>>accnum,因此,当生成输出文件时,它将以纯文本形式显示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 <<"\n\t****Menu****"; cout <<"\n\t* 1.Creating a new account *"; cout <<"\n\t* 2.Cash Deposit *"; cout <<"\n\t* 3.Cash withdrawal *"; cout <<"\n\t* 4.Fund transfer between two account *"; cout <<"\n\t* 5.Exit *"; cout <<"\n\t* *"; cout <<"\n\t********"; cout <<"\n\t 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; } #包括 #包括 #包括 #包括 使用名称空间std; int main() { 双支取、存款、编号、转账、编号、金额; 字符串名、名字、中间名、姓氏; int x=0; int选项; INTA=300; int B=500; int C,W,y; 字符串accnum; 河流充填; 出流孔的直径; 做{ cout,c++,fstream,C++,Fstream,您询问了如何从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 && ac

您询问了如何从
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
<> p>这尤其令人困惑,因为你想用<代码>。txt>代码>是要删除它。我会考虑改变这个用户界面。

建议:

  • 允许用户在不添加
    .txt
  • 与其将帐户信息保存在以帐户持有人姓名为文件名的文件中,不如将其保存在名为
    accnum+“.txt”
    的文件中。这使得一个人可以拥有多个帐户,就像在现实生活中一样

只需创建不带格式的文件,文件名将是
文件名
内容,请注意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;
}
#包括
#包括
#包括
内部主(空){
std::string file_name=“1234”;
std::fstream f_手柄;
打开(文件名,std::fstream::out);
if(f_handle.is_open()){

f_handle因此,要重新措辞,您希望用户能够输入“input”,并让您打开“input.txt”?是的,这需要重新措辞…@JosephLarson我无法判断您的重新措辞是否与原始请求相匹配:)@Findy有任何答案回答了您的问题吗?如果没有,请在t下的评论部分要求澄清他回答。