簡體   English   中英

如何在 C++ 中為 output 文件忽略 cin 的 .txt?

[英]How to ignore .txt for cin for output file in C++?

這是我的 C++ 代碼,我希望創建一個 output 文件。 為了創建 output 文件,我請求用戶在此輸入處包含.txt, cin >> accnum; ,因此,當 output 文件生成時,它將顯示 accnum.txt 為純文本。 我的問題是,我應該怎么做才能讓ignore.txt出現在文本文件中,前提是我必須輸入.txt(這是生成txt文件,輸入時沒有.txt,不會生成output文件)。 我已經放了一條評論線,請參考那條線。 謝謝你的幫助!

    #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;
    }

您詢問了如何從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"的文件中。 這使得一個人可以擁有多個帳戶 - 就像在現實生活中一樣。

只需創建沒有格式的文件,名稱將完全是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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM